Reputation: 3594
I have a Lambda function that I'm trying to deploy using CodePipeline.
The Lambda function source code is in the same project as the Lambda CDK app. Since they are both in the same repository, the CDK app can reference the Lambda source code directly.
The docs suggest to use lambda.Code.fromAsset(path)
when the CDK app can reference the Lambda code directly so I started with the follow definition for my Lambda function.
const fn = new lambda.Function(this, "Fn", {
code: lambda.Code.fromAsset(path/to/lambda/source),
handler: "index.handler",
runtime: lambda.Runtime.NODEJS_10_X
});
In the CodePipeline, I use CodeBuild to run cdk synth
and synthesize the CloudFormation template.
The problem with defining my Lambda in this way is the CloudFormation template has parameters for the Lambda source code bucket and key which are seemingly named randomly e.g. AssetParameters3ffd...affdS3Bucket1CFF873D
When you run cdk deploy
the CLI knows what the parameters are and populates them with values.
In my case however, I'm not using cdk deploy
. I want to use CloudFormation in a CodePipeline.
That means I need to fill the parameters with the values for the Lambda source artifact which is output from my CodeBuild step. Getting the values is easy but I have no way of knowing what the parameters are named.
Next I tried using the following to define the parameter names myself.
const fn = new lambda.Function(this, "Fn", {
code: lambda.Code.fromCfnParameters({
bucketNameParam: new core_1.CfnParameter(this, "LambdaSourceBucket"),
objectKeyParam: new core_1.CfnParameter(this, "LambdaSourceKey"),
}),
handler: "index.handler",
runtime: lambda.Runtime.NODEJS_10_X
});
No luck. cdk synth
still modifies the parameter names in the template e.g. AppLambdaSourceBucket8B89D730
. That's better than before but I still can't be sure what the parameters will be named so I can assign them in my CodePipeline.
I can't use lambda.Code.fromBucket(bucket, key)
because the bucket and key are determined by the CodePipeline/CodeBuild.
I found this example which is doing almost the same thing as me except their CodePipeline is defined in the same CDK app as their Lambda function. That avoids the problem that I'm having because the Lambda definition and CodePipeline definition can both reference the same CfnParametersCode
instance.
Unfortunately I can't co-locate my CodePipeline definition with my Lambda like they've done in that example. I feel like this should still be possible without that.
What am I missing?
Upvotes: 3
Views: 1785
Reputation: 1605
I'm able to do this with new CfnParametersCode
(which should be equivalent to the factory method you're calling). Are you possibly creating this inside another construct and not at the stack level?
Upvotes: 3