diff --git a/packages/aws-cdk-lib/aws-lambda/README.md b/packages/aws-cdk-lib/aws-lambda/README.md index 3c5ed908b91c0..bded45e47e132 100644 --- a/packages/aws-cdk-lib/aws-lambda/README.md +++ b/packages/aws-cdk-lib/aws-lambda/README.md @@ -1134,3 +1134,32 @@ new lambda.Function(this, 'Lambda', { code: lambda.Code.fromAsset(path.join(__dirname, 'lambda-handler')), }); ``` + +## Exclude Patterns for Assets + +When using `lambda.Code.fromAsset(path)` an `exclude` property allows you to ignore particular files for assets by providing patterns for file paths to exclude. Note that this has no effect on `Assets` bundled using the `bundling` property. + +The `ignoreMode` property can be used with the `exclude` property to specify the file paths to ignore based on the [.gitignore specification](https://git-scm.com/docs/gitignore) or the [.dockerignore specification](https://docs.docker.com/engine/reference/builder/#dockerignore-file). The default behavior is to ignore file paths based on simple glob patterns. + +```ts +new lambda.Function(this, 'Function', { + code: lambda.Code.fromAsset(path.join(__dirname, 'my-python-handler'), { + exclude: ['*.ignore'], + ignoreMode: IgnoreMode.DOCKER, // Default is IgnoreMode.GLOB + }), + runtime: lambda.Runtime.PYTHON_3_9, + handler: 'index.handler', +}); +``` + +You can also write to include only certain files by using a negation. + +```ts +new lambda.Function(this, 'Function', { + code: lambda.Code.fromAsset(path.join(__dirname, 'my-python-handler'), { + exclude: ['*', '!index.py'], + }), + runtime: lambda.Runtime.PYTHON_3_9, + handler: 'index.handler', +}); +``` diff --git a/packages/aws-cdk-lib/rosetta/aws_lambda/default.ts-fixture b/packages/aws-cdk-lib/rosetta/aws_lambda/default.ts-fixture index f16a5ee930b18..e15c7498f0a44 100644 --- a/packages/aws-cdk-lib/rosetta/aws_lambda/default.ts-fixture +++ b/packages/aws-cdk-lib/rosetta/aws_lambda/default.ts-fixture @@ -1,7 +1,7 @@ // Fixture with packages imported, but nothing else import * as path from 'path'; import { Construct } from 'constructs'; -import { Aspects, CfnOutput, DockerImage, Duration, RemovalPolicy, Stack } from 'aws-cdk-lib'; +import { Aspects, CfnOutput, DockerImage, Duration, IgnoreMode, RemovalPolicy, Stack } from 'aws-cdk-lib'; import * as lambda from 'aws-cdk-lib/aws-lambda'; import * as iam from 'aws-cdk-lib/aws-iam'; import { LAMBDA_RECOGNIZE_VERSION_PROPS, LAMBDA_RECOGNIZE_LAYER_VERSION } from 'aws-cdk-lib/cx-api';