Show / Hide Table of Contents

Class Code

Represents the Lambda Handler Code.

Inheritance
object
Code
AssetCode
AssetImageCode
CfnParametersCode
EcrImageCode
InlineCode
S3Code
S3CodeV2
Namespace: Amazon.CDK.AWS.Lambda
Assembly: Amazon.CDK.Lib.dll
Syntax (csharp)
public abstract class Code : DeputyBase
Syntax (vb)
Public MustInherit Class Code Inherits DeputyBase
Remarks

ExampleMetadata: infused

Examples
var myFunctionHandler = new Function(this, "MyFunction", new FunctionProps {
                Code = Code.FromAsset("resource/myfunction"),
                Runtime = Runtime.NODEJS_LATEST,
                Handler = "index.handler"
            });

            var eventRule = Trail.OnEvent(this, "MyCloudWatchEvent", new OnEventOptions {
                Target = new LambdaFunction(myFunctionHandler)
            });

            eventRule.AddEventPattern(new EventPattern {
                Account = new [] { "123456789012" },
                Source = new [] { "aws.s3" }
            });

Synopsis

Constructors

Code()

Represents the Lambda Handler Code.

Methods

Bind(Construct)

Called when the lambda or layer is initialized to allow this object to bind to the stack, add resources and have fun.

BindToResource(CfnResource, IResourceBindOptions?)

Called after the CFN function resource has been created to allow the code class to bind to it.

FromAsset(string, IAssetOptions?)

Loads the function code from a local disk path.

FromAssetImage(string, IAssetImageCodeProps?)

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

FromBucket(IBucket, string, string?)

Lambda handler code as an S3 object.

FromBucketV2(IBucket, string, IBucketOptions?)

Lambda handler code as an S3 object.

FromCfnParameters(ICfnParametersCodeProps?)

Creates a new Lambda source defined using CloudFormation parameters.

FromCustomCommand(string, string[], ICustomCommandOptions?)

Runs a command to build the code asset that will be used.

FromDockerBuild(string, IDockerBuildAssetOptions?)

Loads the function code from an asset created by a Docker build.

FromEcrImage(IRepository, IEcrImageCodeProps?)

Use an existing ECR image as the Lambda code.

FromInline(string)

Inline code for Lambda handler.

Constructors

Code()

Represents the Lambda Handler Code.

protected Code()
Remarks

ExampleMetadata: infused

Examples
var myFunctionHandler = new Function(this, "MyFunction", new FunctionProps {
                Code = Code.FromAsset("resource/myfunction"),
                Runtime = Runtime.NODEJS_LATEST,
                Handler = "index.handler"
            });

            var eventRule = Trail.OnEvent(this, "MyCloudWatchEvent", new OnEventOptions {
                Target = new LambdaFunction(myFunctionHandler)
            });

            eventRule.AddEventPattern(new EventPattern {
                Account = new [] { "123456789012" },
                Source = new [] { "aws.s3" }
            });

Methods

Bind(Construct)

Called when the lambda or layer is initialized to allow this object to bind to the stack, add resources and have fun.

public abstract ICodeConfig Bind(Construct scope)
Parameters
scope Construct

The binding scope.

Returns

ICodeConfig

Remarks

ExampleMetadata: infused

BindToResource(CfnResource, IResourceBindOptions?)

Called after the CFN function resource has been created to allow the code class to bind to it.

public virtual void BindToResource(CfnResource resource, IResourceBindOptions? options = null)
Parameters
resource CfnResource
options IResourceBindOptions
Remarks

Specifically it's required to allow assets to add metadata for tooling like SAM CLI to be able to find their origins.

FromAsset(string, IAssetOptions?)

Loads the function code from a local disk path.

public static AssetCode FromAsset(string path, IAssetOptions? options = null)
Parameters
path string

Either a directory with the Lambda code bundle or a .zip file.

options IAssetOptions

Either a directory with the Lambda code bundle or a .zip file.

Returns

AssetCode

Remarks

ExampleMetadata: infused

FromAssetImage(string, IAssetImageCodeProps?)

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

public static AssetImageCode FromAssetImage(string directory, IAssetImageCodeProps? props = null)
Parameters
directory string

the directory from which the asset must be created.

props IAssetImageCodeProps

properties to further configure the selected image.

Returns

AssetImageCode

Remarks

ExampleMetadata: infused

FromBucket(IBucket, string, string?)

Lambda handler code as an S3 object.

public static S3Code FromBucket(IBucket bucket, string key, string? objectVersion = null)
Parameters
bucket IBucket

The S3 bucket.

key string

The object key.

objectVersion string

Optional S3 object version.

Returns

S3Code

Remarks

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.

FromBucketV2(IBucket, string, IBucketOptions?)

Lambda handler code as an S3 object.

public static S3CodeV2 FromBucketV2(IBucket bucket, string key, IBucketOptions? options = null)
Parameters
bucket IBucket

The S3 bucket.

key string

The object key.

options IBucketOptions

Optional parameters for setting the code, current optional parameters to set here are 1.

Returns

S3CodeV2

Remarks

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.

FromCfnParameters(ICfnParametersCodeProps?)

Creates a new Lambda source defined using CloudFormation parameters.

public static CfnParametersCode FromCfnParameters(ICfnParametersCodeProps? props = null)
Parameters
props ICfnParametersCodeProps

optional construction properties of CfnParametersCode.

Returns

CfnParametersCode

a new instance of CfnParametersCode

Remarks

ExampleMetadata: infused

FromCustomCommand(string, string[], ICustomCommandOptions?)

Runs a command to build the code asset that will be used.

public static AssetCode FromCustomCommand(string output, string[] command, ICustomCommandOptions? options = null)
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 ICustomCommandOptions

options for the custom command, and other asset options -- but bundling options are not allowed.

Returns

AssetCode

Remarks

ExampleMetadata: infused

FromDockerBuild(string, IDockerBuildAssetOptions?)

Loads the function code from an asset created by a Docker build.

public static AssetCode FromDockerBuild(string path, IDockerBuildAssetOptions? options = null)
Parameters
path string

The path to the directory containing the Docker file.

options IDockerBuildAssetOptions

Docker build options.

Returns

AssetCode

Remarks

By default, the asset is expected to be located at /asset in the image.

FromEcrImage(IRepository, IEcrImageCodeProps?)

Use an existing ECR image as the Lambda code.

public static EcrImageCode FromEcrImage(IRepository repository, IEcrImageCodeProps? props = null)
Parameters
repository IRepository

the ECR repository that the image is in.

props IEcrImageCodeProps

properties to further configure the selected image.

Returns

EcrImageCode

Remarks

ExampleMetadata: infused

FromInline(string)

Inline code for Lambda handler.

public static InlineCode FromInline(string code)
Parameters
code string

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

Returns

InlineCode

LambdaInlineCode with inline code.

Remarks

ExampleMetadata: infused

Back to top Generated by DocFX