From 030c41323b0ba8f328b383949d378279720e139c Mon Sep 17 00:00:00 2001 From: "k.goto" <24818752+go-to-k@users.noreply.github.com> Date: Wed, 26 Jul 2023 01:08:09 +0900 Subject: [PATCH] docs(lambda): fromAsset exclude (#26473) We discussed for the need to document `exclude` patterns using a negation in [the PR](https://github.com/aws/aws-cdk/pull/26365#issuecomment-1646085090). I also could not find documentation for the `exclude` property itself, so I added them. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- packages/aws-cdk-lib/aws-lambda/README.md | 29 +++++++++++++++++++ .../rosetta/aws_lambda/default.ts-fixture | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) 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';