# S3Code

Lambda code from an S3 archive.

Extends: [Code](/cdk/api/v2/docs/aws-cdk-lib.aws_lambda.Code.html) › S3Code
Obtainable from: [Code.fromBucket](/cdk/api/v2/docs/aws-cdk-lib.aws_lambda.Code.html#frombucket)

- Source: [`aws-lambda/lib/code.ts`](https://github.com/aws/aws-cdk/blob/v2.277.0/packages/aws-cdk-lib/aws-lambda/lib/code.ts#L321)

## Example

TypeScript:

```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';

declare const bucket: s3.Bucket;
const s3Code = new lambda.S3Code(bucket, 'key', /* all optional props */ 'objectVersion');
```

Python:

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

# bucket: s3.Bucket

s3_code = lambda_.S3Code(bucket, "key", "objectVersion")
```

Java:

```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.*;

Bucket bucket;

S3Code s3Code = new S3Code(bucket, "key", "objectVersion");
```

C# / .NET:

```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;

Bucket bucket;

var s3Code = new S3Code(bucket, "key", "objectVersion");
```

Golang:

```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"

var bucket Bucket

s3Code := awscdk.Aws_lambda.NewS3Code(bucket, jsii.String("key"), jsii.String("objectVersion"))
```

## Initializer

TypeScript:

```ts
new S3Code(bucket: IBucket, key: string, objectVersion?: string)
```

Python:

```python
S3Code(bucket: IBucket, key: str, object_version: typing.Optional[str] = None)
```

Java:

```java
new S3Code(IBucket bucket, String key, String objectVersion)
```

C# / .NET:

```csharp
new S3Code(IBucket bucket, string key, string? objectVersion = null)
```

Golang:

```go
NewS3Code(Bucket IBucket, Key *string, ObjectVersion *string)
```

## 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 // chainable` (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` (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.
