class Code
Language | Type name |
---|---|
.NET | Amazon.CDK.AWS.Lambda.Code |
Go | github.com/aws/aws-cdk-go/awscdk/v2/awslambda#Code |
Java | software.amazon.awscdk.services.lambda.Code |
Python | aws_cdk.aws_lambda.Code |
TypeScript (source) | aws-cdk-lib » aws_lambda » Code |
Implemented by
Asset
, Asset
, Cfn
, Ecr
, Inline
, S3
, S3
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
Name | Description |
---|---|
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. |
static from | Loads the function code from a local disk path. |
static from | Create an ECR image from the specified asset and bind it as the Lambda code. |
static from | Lambda handler code as an S3 object. |
static from | Lambda handler code as an S3 object. |
static from | Creates a new Lambda source defined using CloudFormation parameters. |
static from | Runs a command to build the code asset that will be used. |
static from | Loads the function code from an asset created by a Docker build. |
static from | Use an existing ECR image as the Lambda code. |
static from | Inline code for Lambda handler. |
bind(scope)
public bind(scope: Construct): CodeConfig
Parameters
- scope
Construct
— The binding scope.
Returns
Called when the lambda or layer is initialized to allow this object to bind to the stack, add resources and have fun.
ToResource(_resource, _options?)
bindpublic 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.
Asset(path, options?)
static frompublic static fromAsset(path: string, options?: AssetOptions): AssetCode
Parameters
- path
string
— Either a directory with the Lambda code bundle or a .zip file. - options
Asset
Options
Returns
Loads the function code from a local disk path.
AssetImage(directory, props?)
static frompublic static fromAssetImage(directory: string, props?: AssetImageCodeProps): AssetImageCode
Parameters
- directory
string
— the directory from which the asset must be created. - props
Asset
— properties to further configure the selected image.Image Code Props
Returns
Create an ECR image from the specified asset and bind it as the Lambda code.
Bucket(bucket, key, objectVersion?)
static frompublic 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
Lambda handler code as an S3 object.
BucketV2(bucket, key, options?)
static frompublic static fromBucketV2(bucket: IBucket, key: string, options?: BucketOptions): S3CodeV2
Parameters
- bucket
IBucket
— The S3 bucket. - key
string
— The object key. - options
Bucket
— Optional parameters for setting the code, current optional parameters to set here are 1.Options
Returns
Lambda handler code as an S3 object.
CfnParameters(props?)
static frompublic static fromCfnParameters(props?: CfnParametersCodeProps): CfnParametersCode
Parameters
- props
Cfn
— optional construction properties ofParameters Code Props CfnParametersCode
.
Returns
Creates a new Lambda source defined using CloudFormation parameters.
CustomCommand(output, command, options?)
static frompublic static fromCustomCommand(output: string, command: string[], options?: CustomCommandOptions): AssetCode
Parameters
- output
string
— 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
. - command
string[]
— The command which will be executed to generate the output, for example, [ 'node', 'bundle_code.js' ]. - options
Custom
— options for the custom command, and other asset options -- but bundling options are not allowed.Command Options
Returns
Runs a command to build the code asset that will be used.
DockerBuild(path, options?)
static frompublic static fromDockerBuild(path: string, options?: DockerBuildAssetOptions): AssetCode
Parameters
- path
string
— The path to the directory containing the Docker file. - options
Docker
— Docker build options.Build Asset Options
Returns
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.
EcrImage(repository, props?)
static frompublic static fromEcrImage(repository: IRepository, props?: EcrImageCodeProps): EcrImageCode
Parameters
- repository
IRepository
— the ECR repository that the image is in. - props
Ecr
— properties to further configure the selected image.Image Code Props
Returns
Use an existing ECR image as the Lambda code.
Inline(code)
static frompublic static fromInline(code: string): InlineCode
Parameters
- code
string
— The actual handler code (limited to 4KiB).
Returns
Inline code for Lambda handler.