# S3CodeV2

Lambda code from an S3 archive.

With option to set KMSKey for encryption.

Extends: Code › S3CodeV2

- Source: [`aws-lambda/lib/code.ts`](https://github.com/aws/aws-cdk/blob/v2.275.0/packages/aws-cdk-lib/aws-lambda/lib/code.ts#L349)
- Type last changed: v2.271.0 · 25 Sep 2026

## 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,
});
```

## Initializer

```ts
new S3CodeV2(bucket: IBucket, key: string, options?: BucketOptions)
```

## Properties

- `isInline`: boolean — Determines whether this Code is inline code or not.

## Methods

- `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): void` — 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` (static) — Loads the function code from a local disk path.

  **Parameters**

  - `path` — Either a directory with the Lambda code bundle or a .zip file.

- `fromAssetImage(directory: string, props?: AssetImageCodeProps): AssetImageCode` (static) — 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.
  - `props` — properties to further configure the selected image.

- `fromBucket(bucket: IBucket, key: string, objectVersion?: string): S3Code` (static) — Lambda handler code as an S3 object.

  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**

  - `bucket` — The S3 bucket.
  - `key` — The object key.
  - `objectVersion` — Optional S3 object version.

- `fromBucketV2(bucket: IBucket, key: string, options?: BucketOptions): S3CodeV2 // chainable` (static) — Lambda handler code as an S3 object.

  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**

  - `bucket` — The S3 bucket.
  - `key` — The object key.
  - `options` — Optional parameters for setting the code, current optional parameters to set here are 1.

- `fromCfnParameters(props?: CfnParametersCodeProps): CfnParametersCode` (static) — Creates a new Lambda source defined using CloudFormation parameters.

  **Parameters**

  - `props` — optional construction properties of `CfnParametersCode`.

  **Returns:** a new instance of `CfnParametersCode`

- `fromCustomCommand(output: string, command: string[], options?: CustomCommandOptions): AssetCode` (static) — 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`.
  - `command` — The command which will be executed to generate the output, for example, [ 'node', 'bundle_code.js' ].
  - `options` — options for the custom command, and other asset options -- but bundling options are not allowed.

- `fromDockerBuild(path: string, options?: DockerBuildAssetOptions): AssetCode` (static) — 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.
  - `options` — Docker build options.

- `fromEcrImage(repository: IRepository, props?: EcrImageCodeProps): EcrImageCode` (static) — Use an existing ECR image as the Lambda code.

  **Parameters**

  - `repository` — the ECR repository that the image is in.
  - `props` — properties to further configure the selected image.

- `fromInline(code: string): InlineCode` (static) — Inline code for Lambda handler.

  **Parameters**

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

  **Returns:** `LambdaInlineCode` with inline code.
