Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(lambda): fromAsset exclude #26473

Merged
merged 4 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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