@Generated(value="jsii-pacmak/1.67.0 (build 2c027f5)", date="2022-09-19T20:26:40.412Z") @Stability(value=Stable) public class CfnParametersCode extends Code
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 {@link #assign} method.
Example:
Stack lambdaStack = new Stack(app, "LambdaStack");
CfnParametersCode lambdaCode = Code.fromCfnParameters();
Function.Builder.create(lambdaStack, "Lambda")
.code(lambdaCode)
.handler("index.handler")
.runtime(Runtime.NODEJS_14_X)
.build();
// other resources that your Lambda needs, added to the lambdaStack...
Stack pipelineStack = new Stack(app, "PipelineStack");
Pipeline pipeline = new Pipeline(pipelineStack, "Pipeline");
// add the source code repository containing this code to your Pipeline,
// and the source code of the Lambda Function, if they're separate
Artifact cdkSourceOutput = new Artifact();
CodeCommitSourceAction cdkSourceAction = CodeCommitSourceAction.Builder.create()
.repository(Repository.Builder.create(pipelineStack, "CdkCodeRepo")
.repositoryName("CdkCodeRepo")
.build())
.actionName("CdkCode_Source")
.output(cdkSourceOutput)
.build();
Artifact lambdaSourceOutput = new Artifact();
CodeCommitSourceAction lambdaSourceAction = CodeCommitSourceAction.Builder.create()
.repository(Repository.Builder.create(pipelineStack, "LambdaCodeRepo")
.repositoryName("LambdaCodeRepo")
.build())
.actionName("LambdaCode_Source")
.output(lambdaSourceOutput)
.build();
pipeline.addStage(StageOptions.builder()
.stageName("Source")
.actions(List.of(cdkSourceAction, lambdaSourceAction))
.build());
// 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
Project cdkBuildProject = Project.Builder.create(pipelineStack, "CdkBuildProject")
.environment(BuildEnvironment.builder()
.buildImage(LinuxBuildImage.UBUNTU_14_04_NODEJS_10_1_0)
.build())
.buildSpec(BuildSpec.fromObject(Map.of(
"version", "0.2",
"phases", Map.of(
"install", Map.of(
"commands", "npm install"),
"build", Map.of(
"commands", List.of("npm run build", "npm run cdk synth LambdaStack -- -o ."))),
"artifacts", Map.of(
"files", "LambdaStack.template.yaml"))))
.build();
Artifact cdkBuildOutput = new Artifact();
CodeBuildAction cdkBuildAction = CodeBuildAction.Builder.create()
.actionName("CDK_Build")
.project(cdkBuildProject)
.input(cdkSourceOutput)
.outputs(List.of(cdkBuildOutput))
.build();
// 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
Project lambdaBuildProject = Project.Builder.create(pipelineStack, "LambdaBuildProject")
.environment(BuildEnvironment.builder()
.buildImage(LinuxBuildImage.UBUNTU_14_04_NODEJS_10_1_0)
.build())
.buildSpec(BuildSpec.fromObject(Map.of(
"version", "0.2",
"phases", Map.of(
"install", Map.of(
"commands", "npm install"),
"build", Map.of(
"commands", "npm run build")),
"artifacts", Map.of(
"files", List.of("index.js", "node_modules/**/*")))))
.build();
Artifact lambdaBuildOutput = new Artifact();
CodeBuildAction lambdaBuildAction = CodeBuildAction.Builder.create()
.actionName("Lambda_Build")
.project(lambdaBuildProject)
.input(lambdaSourceOutput)
.outputs(List.of(lambdaBuildOutput))
.build();
pipeline.addStage(StageOptions.builder()
.stageName("Build")
.actions(List.of(cdkBuildAction, lambdaBuildAction))
.build());
// finally, deploy your Lambda Stack
pipeline.addStage(StageOptions.builder()
.stageName("Deploy")
.actions(List.of(
CloudFormationCreateUpdateStackAction.Builder.create()
.actionName("Lambda_CFN_Deploy")
.templatePath(cdkBuildOutput.atPath("LambdaStack.template.yaml"))
.stackName("LambdaStackDeployedName")
.adminPermissions(true)
.parameterOverrides(lambdaCode.assign(lambdaBuildOutput.getS3Location()))
.extraInputs(List.of(lambdaBuildOutput))
.build()))
.build());
| Modifier and Type | Class and Description |
|---|---|
static class |
CfnParametersCode.Builder
A fluent builder for
CfnParametersCode. |
| Modifier | Constructor and Description |
|---|---|
|
CfnParametersCode() |
|
CfnParametersCode(CfnParametersCodeProps props) |
protected |
CfnParametersCode(software.amazon.jsii.JsiiObject.InitializationMode initializationMode) |
protected |
CfnParametersCode(software.amazon.jsii.JsiiObjectRef objRef) |
| Modifier and Type | Method and Description |
|---|---|
Map<String,Object> |
assign(Location location)
Create a parameters map from this instance's CloudFormation parameters.
|
CodeConfig |
bind(software.constructs.Construct scope)
Called when the lambda or layer is initialized to allow this object to bind to the stack, add resources and have fun.
|
String |
getBucketNameParam() |
Boolean |
getIsInline()
Determines whether this Code is inline code or not.
|
String |
getObjectKeyParam() |
bindToResource, bindToResource, fromAsset, fromAsset, fromAssetImage, fromAssetImage, fromBucket, fromBucket, fromCfnParameters, fromCfnParameters, fromDockerBuild, fromDockerBuild, fromEcrImage, fromEcrImage, fromInlinejsiiAsyncCall, jsiiAsyncCall, jsiiCall, jsiiCall, jsiiGet, jsiiGet, jsiiSet, jsiiStaticCall, jsiiStaticCall, jsiiStaticGet, jsiiStaticGet, jsiiStaticSet, jsiiStaticSetprotected CfnParametersCode(software.amazon.jsii.JsiiObjectRef objRef)
protected CfnParametersCode(software.amazon.jsii.JsiiObject.InitializationMode initializationMode)
@Stability(value=Stable)
public CfnParametersCode(@Nullable
CfnParametersCodeProps props)
props - @Stability(value=Stable) public CfnParametersCode()
@Stability(value=Stable) @NotNull public Map<String,Object> assign(@NotNull Location location)
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.
location - the location of the object in S3 that represents the Lambda code. This parameter is required.@Stability(value=Stable) @NotNull public CodeConfig bind(@NotNull software.constructs.Construct scope)
@Stability(value=Stable) @NotNull public String getBucketNameParam()
@Stability(value=Stable) @NotNull public Boolean getIsInline()
@Stability(value=Stable) @NotNull public String getObjectKeyParam()
Copyright © 2022. All rights reserved.