Skip to content
v2.274.0

S3CodeV2 Class ​

Lambda code from an S3 archive.

With option to set KMSKey for encryption.

Example ​

ts
// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
import { aws_lambda as lambda } from 'aws-cdk-lib';
import { aws_s3 as s3 } from 'aws-cdk-lib';
import { aws_kms as interfaces_kms } from 'aws-cdk-lib/interfaces';

declare const bucket: s3.Bucket;
declare const keyRef: interfaces_kms.IKeyRef;
const s3CodeV2 = new lambda.S3CodeV2(bucket, 'key', /* all optional props */ {
  objectVersion: 'objectVersion',
  s3ObjectStorageMode: lambda.S3ObjectStorageMode.COPY,
  sourceKMSKey: keyRef,
});
python
# The code below shows an example of how to instantiate this type.
# The values are placeholders you should change.
from aws_cdk import aws_lambda as lambda_
from aws_cdk import aws_s3 as s3
from aws_cdk.interfaces import aws_kms as interfaces_kms

# bucket: s3.Bucket
# key_ref: interfaces_kms.IKeyRef

s3_code_v2 = lambda_.S3CodeV2(bucket, "key",
    object_version="objectVersion",
    s3_object_storage_mode=lambda_.S3ObjectStorageMode.COPY,
    source_kms_key=key_ref
)
java
// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
import software.amazon.awscdk.services.lambda.*;
import software.amazon.awscdk.services.s3.*;
import software.amazon.awscdk.interfaces.kms.*;

Bucket bucket;
IKeyRef keyRef;

S3CodeV2 s3CodeV2 = S3CodeV2.Builder.create(bucket, "key")
        .objectVersion("objectVersion")
        .s3ObjectStorageMode(S3ObjectStorageMode.COPY)
        .sourceKMSKey(keyRef)
        .build();
csharp
// Install: dotnet add package Amazon.CDK.Lib
// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
using Amazon.CDK.AWS.Lambda;
using Amazon.CDK.AWS.S3;
using Amazon.CDK.Interfaces.KMS;

Bucket bucket;
IKeyRef keyRef;

var s3CodeV2 = new S3CodeV2(bucket, "key", new BucketOptions {
    ObjectVersion = "objectVersion",
    S3ObjectStorageMode = S3ObjectStorageMode.COPY,
    SourceKMSKey = keyRef
});
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
import "github.com/aws/aws-cdk-go/awscdk"
import "github.com/aws/aws-cdk-go/awscdk"
import "github.com/aws/aws-cdk-go/awscdk"

var bucket Bucket
var keyRef IKeyRef

s3CodeV2 := awscdk.Aws_lambda.NewS3CodeV2(bucket, jsii.String("key"), &BucketOptions{
	ObjectVersion: jsii.String("objectVersion"),
	S3ObjectStorageMode: awscdk.*Aws_lambda.S3ObjectStorageMode_COPY,
	SourceKMSKey: keyRef,
})

Initializer ​

ts
new S3CodeV2(bucket: IBucket, key: string, options?: BucketOptions)
python
S3CodeV2(bucket: IBucket, key: str, options: typing.Optional[BucketOptions] = None)
java
new S3CodeV2(IBucket bucket, String key, BucketOptions options)
csharp
new S3CodeV2(IBucket bucket, string key, BucketOptions? options = null)
go
NewS3CodeV2(Bucket IBucket, Key *string, Options BucketOptions)

Properties ​

isInlinebooleanis_inlineboolisInlineBooleanIsInlineboolIsInline*boolrequired#
Determines whether this Code is inline code or not.

Methods ​

bind(_scope: Construct): CodeConfigdef bind(self, _scope: Construct) -> CodeConfigpublic CodeConfig bind(Construct _scope)public CodeConfig Bind(Construct _scope)func Bind(_scope Construct) 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: CfnResource, _options?: ResourceBindOptions): voiddef bind_to_resource(self, resource: CfnResource, _options: typing.Optional[ResourceBindOptions] = None) -> Nonepublic void bindToResource(CfnResource resource, ResourceBindOptions _options)public void BindToResource(CfnResource resource, ResourceBindOptions? _options = null)func BindToResource(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.

fromAsset(path: string, options?: AssetOptions): AssetCode@staticmethod def from_asset(path: str, options: typing.Optional[AssetOptions] = None) -> AssetCodepublic static AssetCode fromAsset(String path, AssetOptions options)public static AssetCode FromAsset(string path, AssetOptions? options = null)func FromAsset(Path *string, Options AssetOptions) AssetCodestatic#
Loads the function code from a local disk path.
1 parameter

Parameters

  • pathpathpathpathPath — Either a directory with the Lambda code bundle or a .zip file.
fromAssetImage(directory: string, props?: AssetImageCodeProps): AssetImageCode@staticmethod def from_asset_image(directory: str, props: typing.Optional[AssetImageCodeProps] = None) -> AssetImageCodepublic static AssetImageCode fromAssetImage(String directory, AssetImageCodeProps props)public static AssetImageCode FromAssetImage(string directory, AssetImageCodeProps? props = null)func FromAssetImage(Directory *string, Props AssetImageCodeProps) AssetImageCodestatic#
Create an ECR image from the specified asset and bind it as the Lambda code.
2 parameters

Parameters

  • directorydirectorydirectorydirectoryDirectory — the directory from which the asset must be created.
  • propspropspropspropsProps — properties to further configure the selected image.
fromBucket(bucket: IBucket, key: string, objectVersion?: string): S3Code@staticmethod def from_bucket(bucket: IBucket, key: str, object_version: typing.Optional[str] = None) -> S3Codepublic static S3Code fromBucket(IBucket bucket, String key, String objectVersion)public static S3Code FromBucket(IBucket bucket, string key, string? objectVersion = null)func FromBucket(Bucket IBucket, Key *string, ObjectVersion *string) S3Codestatic#
Lambda handler code as an S3 object.
notes · 3 parameters

Note: If objectVersion is not defined, the lambda will not be updated automatically if the code in the bucket is updated. This is because CDK/Cloudformation does not track changes on the source S3 Bucket. It is recommended to either use S3Code.fromAsset() instead or set objectVersion.

Parameters

  • bucketbucketbucketbucketBucket — The S3 bucket.
  • keykeykeykeyKey — The object key.
  • objectVersionobject_versionobjectVersionobjectVersionObjectVersion — Optional S3 object version.
fromBucketV2(bucket: IBucket, key: string, options?: BucketOptions): S3CodeV2 // chainable@staticmethod def from_bucket_v2(bucket: IBucket, key: str, options: typing.Optional[BucketOptions] = None) -> S3CodeV2public static S3CodeV2 fromBucketV2(IBucket bucket, String key, BucketOptions options)public static S3CodeV2 FromBucketV2(IBucket bucket, string key, BucketOptions? options = null)func FromBucketV2(Bucket IBucket, Key *string, Options BucketOptions) S3CodeV2static#
Lambda handler code as an S3 object.
notes · 3 parameters

Note: If options.objectVersion is not defined, the lambda will not be updated automatically if the code in the bucket is updated. This is because CDK/Cloudformation does not track changes on the source S3 Bucket. It is recommended to either use S3Code.fromAsset() instead or set objectVersion.

Parameters

  • bucketbucketbucketbucketBucket — The S3 bucket.
  • keykeykeykeyKey — The object key.
  • optionsoptionsoptionsoptionsOptions — Optional parameters for setting the code, current optional parameters to set here are 1.
fromCfnParameters(props?: CfnParametersCodeProps): CfnParametersCode@staticmethod def from_cfn_parameters(props: typing.Optional[CfnParametersCodeProps] = None) -> CfnParametersCodepublic static CfnParametersCode fromCfnParameters(CfnParametersCodeProps props)public static CfnParametersCode FromCfnParameters(CfnParametersCodeProps? props = null)func FromCfnParameters(Props CfnParametersCodeProps) CfnParametersCodestatic#
Creates a new Lambda source defined using CloudFormation parameters.
1 parameter · returns

Parameters

  • propspropspropspropsProps — optional construction properties of CfnParametersCode.

Returns: a new instance of CfnParametersCode

fromCustomCommand(output: string, command: string[], options?: CustomCommandOptions): AssetCode@staticmethod def from_custom_command(output: str, command: typing.List[str], options: typing.Optional[CustomCommandOptions] = None) -> AssetCodepublic static AssetCode fromCustomCommand(String output, List<String> command, CustomCommandOptions options)public static AssetCode FromCustomCommand(string output, string[] command, CustomCommandOptions? options = null)func FromCustomCommand(Output *string, Command *[]*string, Options CustomCommandOptions) AssetCodestatic#
Runs a command to build the code asset that will be used.
3 parameters

Parameters

  • outputoutputoutputoutputOutput — 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.
  • commandcommandcommandcommandCommand — The command which will be executed to generate the output, for example, [ 'node', 'bundle_code.js' ].
  • optionsoptionsoptionsoptionsOptions — options for the custom command, and other asset options -- but bundling options are not allowed.
fromDockerBuild(path: string, options?: DockerBuildAssetOptions): AssetCode@staticmethod def from_docker_build(path: str, options: typing.Optional[DockerBuildAssetOptions] = None) -> AssetCodepublic static AssetCode fromDockerBuild(String path, DockerBuildAssetOptions options)public static AssetCode FromDockerBuild(string path, DockerBuildAssetOptions? options = null)func FromDockerBuild(Path *string, Options DockerBuildAssetOptions) AssetCodestatic#
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.

2 parameters

Parameters

  • pathpathpathpathPath — The path to the directory containing the Docker file.
  • optionsoptionsoptionsoptionsOptions — Docker build options.
fromEcrImage(repository: IRepository, props?: EcrImageCodeProps): EcrImageCode@staticmethod def from_ecr_image(repository: IRepository, props: typing.Optional[EcrImageCodeProps] = None) -> EcrImageCodepublic static EcrImageCode fromEcrImage(IRepository repository, EcrImageCodeProps props)public static EcrImageCode FromEcrImage(IRepository repository, EcrImageCodeProps? props = null)func FromEcrImage(Repository IRepository, Props EcrImageCodeProps) EcrImageCodestatic#
Use an existing ECR image as the Lambda code.
2 parameters

Parameters

  • repositoryrepositoryrepositoryrepositoryRepository — the ECR repository that the image is in.
  • propspropspropspropsProps — properties to further configure the selected image.
fromInline(code: string): InlineCode@staticmethod def from_inline(code: str) -> InlineCodepublic static InlineCode fromInline(String code)public static InlineCode FromInline(string code)func FromInline(Code *string) InlineCodestatic#
Inline code for Lambda handler.
1 parameter · returns

Parameters

  • codecodecodecodeCode — The actual handler code (the resulting zip file cannot exceed 4MB).

Returns: LambdaInlineCode with inline code.