Skip to content

Commit

Permalink
docs(lambda): fromAsset exclude (aws#26473)
Browse files Browse the repository at this point in the history
We discussed for the need to document `exclude` patterns using a negation in [the PR](aws#26365 (comment)).

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*
  • Loading branch information
go-to-k authored and bmoffatt committed Jul 28, 2023
1 parent f8dcf53 commit 030c413
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
29 changes: 29 additions & 0 deletions packages/aws-cdk-lib/aws-lambda/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
});
```
2 changes: 1 addition & 1 deletion packages/aws-cdk-lib/rosetta/aws_lambda/default.ts-fixture
Original file line number Diff line number Diff line change
@@ -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';
Expand Down

0 comments on commit 030c413

Please sign in to comment.