class CfnParametersCode
Language | Type name |
---|---|
![]() | Amazon.CDK.AWS.Lambda.CfnParametersCode |
![]() | github.com/aws/aws-cdk-go/awscdk/v2/awslambda#CfnParametersCode |
![]() | software.amazon.awscdk.services.lambda.CfnParametersCode |
![]() | aws_cdk.aws_lambda.CfnParametersCode |
![]() | aws-cdk-lib » aws_lambda » CfnParametersCode |
Extends
Code
Lambda code defined using 2 CloudFormation parameters.
Useful when you don't have access to the code of your Lambda from your CDK code, so you can't use Assets,
and you want to deploy the Lambda in a CodePipeline, using CloudFormation Actions -
you can fill the parameters using the #assign
method.
Example
const lambdaStack = new cdk.Stack(app, 'LambdaStack');
const lambdaCode = lambda.Code.fromCfnParameters();
new lambda.Function(lambdaStack, 'Lambda', {
code: lambdaCode,
handler: 'index.handler',
runtime: lambda.Runtime.NODEJS_LATEST,
});
// other resources that your Lambda needs, added to the lambdaStack...
const pipelineStack = new cdk.Stack(app, 'PipelineStack');
const pipeline = new codepipeline.Pipeline(pipelineStack, 'Pipeline', {
crossAccountKeys: true,
});
// add the source code repository containing this code to your Pipeline,
// and the source code of the Lambda Function, if they're separate
const cdkSourceOutput = new codepipeline.Artifact();
const cdkSourceAction = new codepipeline_actions.CodeCommitSourceAction({
repository: new codecommit.Repository(pipelineStack, 'CdkCodeRepo', {
repositoryName: 'CdkCodeRepo',
}),
actionName: 'CdkCode_Source',
output: cdkSourceOutput,
});
const lambdaSourceOutput = new codepipeline.Artifact();
const lambdaSourceAction = new codepipeline_actions.CodeCommitSourceAction({
repository: new codecommit.Repository(pipelineStack, 'LambdaCodeRepo', {
repositoryName: 'LambdaCodeRepo',
}),
actionName: 'LambdaCode_Source',
output: lambdaSourceOutput,
});
pipeline.addStage({
stageName: 'Source',
actions: [cdkSourceAction, lambdaSourceAction],
});
// synthesize the Lambda CDK template, using CodeBuild
// the below values are just examples, assuming your CDK code is in TypeScript/JavaScript -
// adjust the build environment and/or commands accordingly
const cdkBuildProject = new codebuild.Project(pipelineStack, 'CdkBuildProject', {
environment: {
buildImage: codebuild.LinuxBuildImage.STANDARD_7_0,
},
buildSpec: codebuild.BuildSpec.fromObject({
version: '0.2',
phases: {
install: {
commands: 'npm install',
},
build: {
commands: [
'npm run build',
'npm run cdk synth LambdaStack -- -o .',
],
},
},
artifacts: {
files: 'LambdaStack.template.yaml',
},
}),
});
const cdkBuildOutput = new codepipeline.Artifact();
const cdkBuildAction = new codepipeline_actions.CodeBuildAction({
actionName: 'CDK_Build',
project: cdkBuildProject,
input: cdkSourceOutput,
outputs: [cdkBuildOutput],
});
// build your Lambda code, using CodeBuild
// again, this example assumes your Lambda is written in TypeScript/JavaScript -
// make sure to adjust the build environment and/or commands if they don't match your specific situation
const lambdaBuildProject = new codebuild.Project(pipelineStack, 'LambdaBuildProject', {
environment: {
buildImage: codebuild.LinuxBuildImage.STANDARD_7_0,
},
buildSpec: codebuild.BuildSpec.fromObject({
version: '0.2',
phases: {
install: {
commands: 'npm install',
},
build: {
commands: 'npm run build',
},
},
artifacts: {
files: [
'index.js',
'node_modules/**/*',
],
},
}),
});
const lambdaBuildOutput = new codepipeline.Artifact();
const lambdaBuildAction = new codepipeline_actions.CodeBuildAction({
actionName: 'Lambda_Build',
project: lambdaBuildProject,
input: lambdaSourceOutput,
outputs: [lambdaBuildOutput],
});
pipeline.addStage({
stageName: 'Build',
actions: [cdkBuildAction, lambdaBuildAction],
});
// finally, deploy your Lambda Stack
pipeline.addStage({
stageName: 'Deploy',
actions: [
new codepipeline_actions.CloudFormationCreateUpdateStackAction({
actionName: 'Lambda_CFN_Deploy',
templatePath: cdkBuildOutput.atPath('LambdaStack.template.yaml'),
stackName: 'LambdaStackDeployedName',
adminPermissions: true,
parameterOverrides: lambdaCode.assign(lambdaBuildOutput.s3Location),
extraInputs: [
lambdaBuildOutput,
],
}),
],
});
Initializer
new CfnParametersCode(props?: CfnParametersCodeProps)
Parameters
- props
Cfn
Parameters Code Props
Properties
Name | Type | Description |
---|---|---|
bucket | string | |
is | boolean | Determines whether this Code is inline code or not. |
object | string |
bucketNameParam
Type:
string
isInline
Type:
boolean
Determines whether this Code is inline code or not.
objectKeyParam
Type:
string
Methods
Name | Description |
---|---|
assign(location) | Create a parameters map from this instance's CloudFormation parameters. |
bind(scope) | Called when the lambda or layer is initialized to allow this object to bind to the stack, add resources and have fun. |
bind | Called after the CFN function resource has been created to allow the code class to bind to it. |
assign(location)
public assign(location: Location): { [string]: any }
Parameters
- location
Location
— the location of the object in S3 that represents the Lambda code.
Returns
{ [string]: any }
Create a parameters map from this instance's CloudFormation parameters.
It returns a map with 2 keys that correspond to the names of the parameters defined in this Lambda code,
and as values it contains the appropriate expressions pointing at the provided S3 location
(most likely, obtained from a CodePipeline Artifact by calling the artifact.s3Location
method).
The result should be provided to the CloudFormation Action
that is deploying the Stack that the Lambda with this code is part of,
in the parameterOverrides
property.
bind(scope)
public bind(scope: Construct): CodeConfig
Parameters
- scope
Construct
Returns
Called when the lambda or layer is initialized to allow this object to bind to the stack, add resources and have fun.
bindToResource(_resource, _options?)
public bindToResource(_resource: CfnResource, _options?: ResourceBindOptions): void
Parameters
- _resource
Cfn
Resource - _options
Resource
Bind Options
Called after the CFN function resource has been created to allow the code class to bind to it.
Specifically it's required to allow assets to add metadata for tooling like SAM CLI to be able to find their origins.