aws-cdk-lib.aws_lambda.Code

class Code

LanguageType name
.NETAmazon.CDK.AWS.Lambda.Code
Gogithub.com/aws/aws-cdk-go/awscdk/v2/awslambda#Code
Javasoftware.amazon.awscdk.services.lambda.Code
Pythonaws_cdk.aws_lambda.Code
TypeScript (source)aws-cdk-lib » aws_lambda » Code

Implemented by AssetCode, AssetImageCode, CfnParametersCode, EcrImageCode, InlineCode, S3Code

Represents the Lambda Handler Code.

Example

import * as signer from 'aws-cdk-lib/aws-signer';

const signingProfile = new signer.SigningProfile(this, 'SigningProfile', {
  platform: signer.Platform.AWS_LAMBDA_SHA384_ECDSA,
});

const codeSigningConfig = new lambda.CodeSigningConfig(this, 'CodeSigningConfig', {
  signingProfiles: [signingProfile],
});

new lambda.Function(this, 'Function', {
  codeSigningConfig,
  runtime: lambda.Runtime.NODEJS_18_X,
  handler: 'index.handler',
  code: lambda.Code.fromAsset(path.join(__dirname, 'lambda-handler')),
});

Initializer

new Code()

Methods

NameDescription
bind(scope)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?)Called after the CFN function resource has been created to allow the code class to bind to it.
static fromAsset(path, options?)Loads the function code from a local disk path.
static fromAssetImage(directory, props?)Create an ECR image from the specified asset and bind it as the Lambda code.
static fromBucket(bucket, key, objectVersion?)Lambda handler code as an S3 object.
static fromCfnParameters(props?)Creates a new Lambda source defined using CloudFormation parameters.
static fromDockerBuild(path, options?)Loads the function code from an asset created by a Docker build.
static fromEcrImage(repository, props?)Use an existing ECR image as the Lambda code.
static fromInline(code)Inline code for Lambda handler.

bind(scope)

public bind(scope: Construct): CodeConfig

Parameters

  • scope Construct — The binding scope.

Returns

  • CodeConfig

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 CfnResource
  • _options ResourceBindOptions

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.


static fromAsset(path, options?)

public static fromAsset(path: string, options?: AssetOptions): AssetCode

Parameters

  • path string — Either a directory with the Lambda code bundle or a .zip file.
  • options AssetOptions

Returns

  • AssetCode

Loads the function code from a local disk path.


static fromAssetImage(directory, props?)

public static fromAssetImage(directory: string, props?: AssetImageCodeProps): AssetImageCode

Parameters

  • directory string — the directory from which the asset must be created.
  • props AssetImageCodeProps — properties to further configure the selected image.

Returns

  • AssetImageCode

Create an ECR image from the specified asset and bind it as the Lambda code.


static fromBucket(bucket, key, objectVersion?)

public static fromBucket(bucket: IBucket, key: string, objectVersion?: string): S3Code

Parameters

  • bucket IBucket — The S3 bucket.
  • key string — The object key.
  • objectVersion string — Optional S3 object version.

Returns

  • S3Code

Lambda handler code as an S3 object.


static fromCfnParameters(props?)

public static fromCfnParameters(props?: CfnParametersCodeProps): CfnParametersCode

Parameters

  • props CfnParametersCodeProps — optional construction properties of CfnParametersCode.

Returns

  • CfnParametersCode

Creates a new Lambda source defined using CloudFormation parameters.


static fromDockerBuild(path, options?)

public static fromDockerBuild(path: string, options?: DockerBuildAssetOptions): AssetCode

Parameters

  • path string — The path to the directory containing the Docker file.
  • options DockerBuildAssetOptions — Docker build options.

Returns

  • AssetCode

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.


static fromEcrImage(repository, props?)

public static fromEcrImage(repository: IRepository, props?: EcrImageCodeProps): EcrImageCode

Parameters

  • repository IRepository — the ECR repository that the image is in.
  • props EcrImageCodeProps — properties to further configure the selected image.

Returns

  • EcrImageCode

Use an existing ECR image as the Lambda code.


static fromInline(code)

public static fromInline(code: string): InlineCode

Parameters

  • code string — The actual handler code (limited to 4KiB).

Returns

  • InlineCode

Inline code for Lambda handler.