Skip to content
v2.273.0

InlineCode Class ​

Lambda code from an inline string.

Example ​

ts
const layer = new lambda.LayerVersion(stack, 'MyLayer', {
  code: lambda.Code.fromAsset(path.join(__dirname, 'layer-code')),
  compatibleRuntimes: [lambda.Runtime.NODEJS_LATEST],
  license: 'Apache-2.0',
  description: 'A layer to test the L2 construct',
});

// To grant usage by other AWS accounts
layer.addPermission('remote-account-grant', { accountId: awsAccountId });

// To grant usage to all accounts in some AWS Ogranization
// layer.grantUsage({ accountId: '*', organizationId });

new lambda.Function(stack, 'MyLayeredLambda', {
  code: new lambda.InlineCode('foo'),
  handler: 'index.handler',
  runtime: lambda.Runtime.NODEJS_LATEST,
  layers: [layer],
});
python
layer = lambda_.LayerVersion(stack, "MyLayer",
    code=lambda_.Code.from_asset(path.join(__dirname, "layer-code")),
    compatible_runtimes=[lambda_.Runtime.NODEJS_LATEST],
    license="Apache-2.0",
    description="A layer to test the L2 construct"
)

# To grant usage by other AWS accounts
layer.add_permission("remote-account-grant", account_id=aws_account_id)

# To grant usage to all accounts in some AWS Ogranization
# layer.grantUsage({ accountId: '*', organizationId });

lambda_.Function(stack, "MyLayeredLambda",
    code=lambda_.InlineCode("foo"),
    handler="index.handler",
    runtime=lambda_.Runtime.NODEJS_LATEST,
    layers=[layer]
)
java
LayerVersion layer = LayerVersion.Builder.create(stack, "MyLayer")
        .code(Code.fromAsset(join(__dirname, "layer-code")))
        .compatibleRuntimes(List.of(Runtime.NODEJS_LATEST))
        .license("Apache-2.0")
        .description("A layer to test the L2 construct")
        .build();

// To grant usage by other AWS accounts
layer.addPermission("remote-account-grant", LayerVersionPermission.builder().accountId(awsAccountId).build());

// To grant usage to all accounts in some AWS Ogranization
// layer.grantUsage({ accountId: '*', organizationId });

// To grant usage to all accounts in some AWS Ogranization
// layer.grantUsage({ accountId: '*', organizationId });
Function.Builder.create(stack, "MyLayeredLambda")
        .code(new InlineCode("foo"))
        .handler("index.handler")
        .runtime(Runtime.NODEJS_LATEST)
        .layers(List.of(layer))
        .build();
csharp
// Install: dotnet add package Amazon.CDK.Lib
var layer = new LayerVersion(stack, "MyLayer", new LayerVersionProps {
    Code = Code.FromAsset(Join(__dirname, "layer-code")),
    CompatibleRuntimes = new [] { Runtime.NODEJS_LATEST },
    License = "Apache-2.0",
    Description = "A layer to test the L2 construct"
});

// To grant usage by other AWS accounts
layer.AddPermission("remote-account-grant", new LayerVersionPermission { AccountId = awsAccountId });

// To grant usage to all accounts in some AWS Ogranization
// layer.grantUsage({ accountId: '*', organizationId });

// To grant usage to all accounts in some AWS Ogranization
// layer.grantUsage({ accountId: '*', organizationId });
new Function(stack, "MyLayeredLambda", new FunctionProps {
    Code = new InlineCode("foo"),
    Handler = "index.handler",
    Runtime = Runtime.NODEJS_LATEST,
    Layers = new [] { layer }
});
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
layer := lambda.NewLayerVersion(stack, jsii.String("MyLayer"), &LayerVersionProps{
	Code: lambda.Code_FromAsset(path.join(__dirname, jsii.String("layer-code"))),
	CompatibleRuntimes: []Runtime{
		lambda.Runtime_NODEJS_LATEST(),
	},
	License: jsii.String("Apache-2.0"),
	Description: jsii.String("A layer to test the L2 construct"),
})

// To grant usage by other AWS accounts
layer.addPermission(jsii.String("remote-account-grant"), &LayerVersionPermission{
	AccountId: awsAccountId,
})

// To grant usage to all accounts in some AWS Ogranization
// layer.grantUsage({ accountId: '*', organizationId });

// To grant usage to all accounts in some AWS Ogranization
// layer.grantUsage({ accountId: '*', organizationId });
lambda.NewFunction(stack, jsii.String("MyLayeredLambda"), &FunctionProps{
	Code: lambda.NewInlineCode(jsii.String("foo")),
	Handler: jsii.String("index.handler"),
	Runtime: lambda.Runtime_NODEJS_LATEST(),
	Layers: []ILayerVersion{
		layer,
	},
})

Initializer ​

ts
new InlineCode(code: string)
python
InlineCode(code: str)
java
new InlineCode(String code)
csharp
new InlineCode(string code)
go
NewInlineCode(Code *string)

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@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 // chainable@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.