java.lang.Object
software.amazon.jsii.JsiiObject
software.amazon.awscdk.services.lambda.Code
All Implemented Interfaces:
software.amazon.jsii.JsiiSerializable
Direct Known Subclasses:
AssetCode, AssetImageCode, CfnParametersCode, EcrImageCode, InlineCode, S3Code, S3CodeV2

@Generated(value="jsii-pacmak/1.104.0 (build e79254c)", date="2024-11-22T02:24:03.880Z") @Stability(Stable) public abstract class Code extends software.amazon.jsii.JsiiObject
Represents the Lambda Handler Code.

Example:

 import software.amazon.awscdk.services.signer.*;
 SigningProfile signingProfile = SigningProfile.Builder.create(this, "SigningProfile")
         .platform(Platform.AWS_LAMBDA_SHA384_ECDSA)
         .build();
 CodeSigningConfig codeSigningConfig = CodeSigningConfig.Builder.create(this, "CodeSigningConfig")
         .signingProfiles(List.of(signingProfile))
         .build();
 Function.Builder.create(this, "Function")
         .codeSigningConfig(codeSigningConfig)
         .runtime(Runtime.NODEJS_18_X)
         .handler("index.handler")
         .code(Code.fromAsset(join(__dirname, "lambda-handler")))
         .build();
 
  • Constructor Details

    • Code

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

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

      @Stability(Stable) protected Code()
  • Method Details

    • fromAsset

      @Stability(Stable) @NotNull public static AssetCode fromAsset(@NotNull String path, @Nullable AssetOptions options)
      Loads the function code from a local disk path.

      Parameters:
      path - Either a directory with the Lambda code bundle or a .zip file. This parameter is required.
      options -
    • fromAsset

      @Stability(Stable) @NotNull public static AssetCode fromAsset(@NotNull String path)
      Loads the function code from a local disk path.

      Parameters:
      path - Either a directory with the Lambda code bundle or a .zip file. This parameter is required.
    • fromAssetImage

      @Stability(Stable) @NotNull public static AssetImageCode fromAssetImage(@NotNull String directory, @Nullable AssetImageCodeProps props)
      Create an ECR image from the specified asset and bind it as the Lambda code.

      Parameters:
      directory - the directory from which the asset must be created. This parameter is required.
      props - properties to further configure the selected image.
    • fromAssetImage

      @Stability(Stable) @NotNull public static AssetImageCode fromAssetImage(@NotNull String directory)
      Create an ECR image from the specified asset and bind it as the Lambda code.

      Parameters:
      directory - the directory from which the asset must be created. This parameter is required.
    • fromBucket

      @Stability(Stable) @NotNull public static S3Code fromBucket(@NotNull IBucket bucket, @NotNull String key, @Nullable String objectVersion)
      Lambda handler code as an S3 object.

      Parameters:
      bucket - The S3 bucket. This parameter is required.
      key - The object key. This parameter is required.
      objectVersion - Optional S3 object version.
    • fromBucket

      @Stability(Stable) @NotNull public static S3Code fromBucket(@NotNull IBucket bucket, @NotNull String key)
      Lambda handler code as an S3 object.

      Parameters:
      bucket - The S3 bucket. This parameter is required.
      key - The object key. This parameter is required.
    • fromBucketV2

      @Stability(Stable) @NotNull public static S3CodeV2 fromBucketV2(@NotNull IBucket bucket, @NotNull String key, @Nullable BucketOptions options)
      Lambda handler code as an S3 object.

      Parameters:
      bucket - The S3 bucket. This parameter is required.
      key - The object key. This parameter is required.
      options - Optional parameters for setting the code, current optional parameters to set here are 1.
    • fromBucketV2

      @Stability(Stable) @NotNull public static S3CodeV2 fromBucketV2(@NotNull IBucket bucket, @NotNull String key)
      Lambda handler code as an S3 object.

      Parameters:
      bucket - The S3 bucket. This parameter is required.
      key - The object key. This parameter is required.
    • fromCfnParameters

      @Stability(Stable) @NotNull public static CfnParametersCode fromCfnParameters(@Nullable CfnParametersCodeProps props)
      Creates a new Lambda source defined using CloudFormation parameters.

      Parameters:
      props - optional construction properties of CfnParametersCode.
      Returns:
      a new instance of CfnParametersCode
    • fromCfnParameters

      @Stability(Stable) @NotNull public static CfnParametersCode fromCfnParameters()
      Creates a new Lambda source defined using CloudFormation parameters.

      Returns:
      a new instance of CfnParametersCode
    • fromCustomCommand

      @Stability(Stable) @NotNull public static AssetCode fromCustomCommand(@NotNull String output, @NotNull List<String> command, @Nullable CustomCommandOptions options)
      Runs a command to build the code asset that will be used.

      Parameters:
      output - Where the output of the command will be directed, either a directory or a .zip file with the output Lambda code bundle * For example, if you use the command to run a build script (e.g., [ 'node', 'bundle_code.js' ]), and the build script generates a directory /my/lambda/code containing code that should be ran in a Lambda function, then output should be set to /my/lambda/code. This parameter is required.
      command - The command which will be executed to generate the output, for example, [ 'node', 'bundle_code.js' ]. This parameter is required.
      options - options for the custom command, and other asset options -- but bundling options are not allowed.
    • fromCustomCommand

      @Stability(Stable) @NotNull public static AssetCode fromCustomCommand(@NotNull String output, @NotNull List<String> command)
      Runs a command to build the code asset that will be used.

      Parameters:
      output - Where the output of the command will be directed, either a directory or a .zip file with the output Lambda code bundle * For example, if you use the command to run a build script (e.g., [ 'node', 'bundle_code.js' ]), and the build script generates a directory /my/lambda/code containing code that should be ran in a Lambda function, then output should be set to /my/lambda/code. This parameter is required.
      command - The command which will be executed to generate the output, for example, [ 'node', 'bundle_code.js' ]. This parameter is required.
    • fromDockerBuild

      @Stability(Stable) @NotNull public static AssetCode fromDockerBuild(@NotNull String path, @Nullable DockerBuildAssetOptions options)
      Loads the function code from an asset created by a Docker build.

      By default, the asset is expected to be located at /asset in the image.

      Parameters:
      path - The path to the directory containing the Docker file. This parameter is required.
      options - Docker build options.
    • fromDockerBuild

      @Stability(Stable) @NotNull public static AssetCode fromDockerBuild(@NotNull String path)
      Loads the function code from an asset created by a Docker build.

      By default, the asset is expected to be located at /asset in the image.

      Parameters:
      path - The path to the directory containing the Docker file. This parameter is required.
    • fromEcrImage

      @Stability(Stable) @NotNull public static EcrImageCode fromEcrImage(@NotNull IRepository repository, @Nullable EcrImageCodeProps props)
      Use an existing ECR image as the Lambda code.

      Parameters:
      repository - the ECR repository that the image is in. This parameter is required.
      props - properties to further configure the selected image.
    • fromEcrImage

      @Stability(Stable) @NotNull public static EcrImageCode fromEcrImage(@NotNull IRepository repository)
      Use an existing ECR image as the Lambda code.

      Parameters:
      repository - the ECR repository that the image is in. This parameter is required.
    • fromInline

      @Stability(Stable) @NotNull public static InlineCode fromInline(@NotNull String code)
      Inline code for Lambda handler.

      Parameters:
      code - The actual handler code (limited to 4KiB). This parameter is required.
      Returns:
      LambdaInlineCode with inline code.
    • bind

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

      Parameters:
      scope - The binding scope. This parameter is required.
    • bindToResource

      @Stability(Stable) public void bindToResource(@NotNull CfnResource _resource, @Nullable ResourceBindOptions _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.

      Parameters:
      _resource - This parameter is required.
      _options -
    • bindToResource

      @Stability(Stable) public void bindToResource(@NotNull CfnResource _resource)
      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.

      Parameters:
      _resource - This parameter is required.