Class CfnParametersCode

java.lang.Object
software.amazon.jsii.JsiiObject
software.amazon.awscdk.services.lambda.Code
software.amazon.awscdk.services.lambda.CfnParametersCode
All Implemented Interfaces:
software.amazon.jsii.JsiiSerializable

@Generated(value="jsii-pacmak/1.98.0 (build 00b106d)", date="2024-05-02T15:58:28.704Z") @Stability(Stable) public class 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:

 Stack lambdaStack = new Stack(app, "LambdaStack");
 CfnParametersCode lambdaCode = Code.fromCfnParameters();
 Function.Builder.create(lambdaStack, "Lambda")
         .code(lambdaCode)
         .handler("index.handler")
         .runtime(Runtime.NODEJS_LATEST)
         .build();
 // other resources that your Lambda needs, added to the lambdaStack...
 Stack pipelineStack = new Stack(app, "PipelineStack");
 Pipeline pipeline = Pipeline.Builder.create(pipelineStack, "Pipeline")
         .crossAccountKeys(true)
         .build();
 // 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.STANDARD_7_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.STANDARD_7_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());
 
  • Constructor Details

    • CfnParametersCode

      protected CfnParametersCode(software.amazon.jsii.JsiiObjectRef objRef)
    • CfnParametersCode

      protected CfnParametersCode(software.amazon.jsii.JsiiObject.InitializationMode initializationMode)
    • CfnParametersCode

      @Stability(Stable) public CfnParametersCode(@Nullable CfnParametersCodeProps props)
      Parameters:
      props -
    • CfnParametersCode

      @Stability(Stable) public CfnParametersCode()
  • Method Details

    • assign

      @Stability(Stable) @NotNull public Map<String,Object> assign(@NotNull Location location)
      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.

      Parameters:
      location - the location of the object in S3 that represents the Lambda code. This parameter is required.
    • bind

      @Stability(Stable) @NotNull public CodeConfig bind(@NotNull 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.

      Specified by:
      bind in class Code
      Parameters:
      scope - This parameter is required.
    • getBucketNameParam

      @Stability(Stable) @NotNull public String getBucketNameParam()
    • getIsInline

      @Stability(Stable) @NotNull public Boolean getIsInline()
      Determines whether this Code is inline code or not.
    • getObjectKeyParam

      @Stability(Stable) @NotNull public String getObjectKeyParam()