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

feat(lambda-python): bundle dependencies in a lambda layer #9582

Merged
merged 68 commits into from
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from 65 commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
b724a26
feat(aws-lambda-python): add support for placing python dependencies …
misterjoshua Aug 11, 2020
4b96baa
Add compatibleRuntimes to the lambda layer
misterjoshua Aug 11, 2020
fd3025d
Review changes and add pipenv support
misterjoshua Aug 11, 2020
f2f6fa3
Insert the pipenvHome directory into the bundling output directory an…
misterjoshua Aug 11, 2020
61a03b8
Try invoking pipenv via python -m pipenv
misterjoshua Aug 11, 2020
fdf70c4
README.md and a spelling fix
misterjoshua Aug 11, 2020
faa3db9
Integ tests for pipenv layers on each runtime and fix asset hash issues
misterjoshua Aug 11, 2020
a31d44c
Move pipenv install to an image that extends from the runtime bundlin…
misterjoshua Aug 14, 2020
2edf744
Trial installing dependencies in a docker build
misterjoshua Aug 16, 2020
a3b8fc5
Fix some comments and un-expose some functions we don't need to expose.
misterjoshua Aug 16, 2020
f52c25b
Add back my layer bundling test
misterjoshua Aug 16, 2020
f3a6e39
Merge branch 'master' into lambda-python-layers
misterjoshua Aug 17, 2020
236186b
Fix accidental disable of import order linting rule
misterjoshua Aug 17, 2020
ff8594f
Add #9763's convenient FROM build arg defaults
misterjoshua Aug 17, 2020
ec8f9cc
Apply suggestions from code review
misterjoshua Aug 19, 2020
d47cb34
Merge branch 'master' into lambda-python-layers
misterjoshua Aug 19, 2020
c742923
Install rsync if necessary
misterjoshua Aug 19, 2020
a7a1e4f
Change the default dependenciesLocation to LAYER
misterjoshua Aug 19, 2020
3a1ffe7
Merge branch 'master' into lambda-python-layers
misterjoshua Aug 21, 2020
628c38a
Merge branch 'master' into lambda-python-layers
misterjoshua Aug 24, 2020
5ff6199
Add an integ test that checks dependency removal
misterjoshua Aug 26, 2020
02208e2
Change the integ test to check a layer instead
misterjoshua Aug 26, 2020
12a367f
Merge branch 'master' into lambda-python-layers
misterjoshua Aug 26, 2020
53ca44e
Merge branch 'master' into lambda-python-layers
misterjoshua Aug 26, 2020
ba98c9c
Merge branch 'master' of github.com:aws/aws-cdk into lambda-python-la…
misterjoshua Aug 27, 2020
92f3658
Change the dependency removal integ to synth twice
misterjoshua Aug 27, 2020
5b95db6
Point the custom dockerfile name test to a real directory
misterjoshua Aug 27, 2020
0a9facf
Simplify the bundling code
misterjoshua Aug 27, 2020
3f9b271
Add PythonDependencyLayer and PythonSharedCodeLayer
misterjoshua Aug 27, 2020
b0b8f72
Fix compatibleRuntimes in PythonDependenciesLayer
misterjoshua Aug 27, 2020
dd9a3c3
Merge branch 'master' of github.com:aws/aws-cdk into lambda-python-la…
misterjoshua Aug 27, 2020
1fb4a5c
Merge layer constructs into PythonLayerVersion
misterjoshua Aug 27, 2020
c69216a
Inline docs and cleanup
misterjoshua Aug 27, 2020
bac8dee
Add readonly to bundling functions options
misterjoshua Aug 27, 2020
0aba325
Match up a code style
misterjoshua Aug 27, 2020
830ce8a
Merge branch 'master' of github.com:aws/aws-cdk into lambda-python-la…
misterjoshua Aug 27, 2020
257b657
Convert function to use the python layer for bundling dependencies
misterjoshua Aug 27, 2020
217be2a
Change layer test to compare readDirSync outputs to enhance error vis…
misterjoshua Aug 27, 2020
3be7e09
Add more tests
misterjoshua Aug 27, 2020
d0a9e40
Fix the PythonCodeLocalBundler rename I forgot to include. whoops.
misterjoshua Aug 27, 2020
b020cd7
Remove the dependency on @aws-cdk/assets
misterjoshua Aug 27, 2020
988b5ea
Decrease assetHash thrashing based on cwd
misterjoshua Aug 27, 2020
281d365
Remove DependenciesLocation, consolidate bundling
misterjoshua Aug 28, 2020
dbfc2af
Merge branch 'master' of github.com:aws/aws-cdk into lambda-python-la…
misterjoshua Aug 28, 2020
a940c85
Add more explanation for the two Dockerfiles
misterjoshua Aug 28, 2020
e7cce93
Restore more of the original code
misterjoshua Aug 28, 2020
f793c90
Update the readme
misterjoshua Aug 28, 2020
90290b9
Consolidate bundle*Layer fns into bundleLayer
misterjoshua Aug 28, 2020
942c615
Remove extraneous text fixtures
misterjoshua Aug 28, 2020
f0ac60f
Remove extraneous cdk.CopyOptions from PythonLayerVersionProps
misterjoshua Aug 28, 2020
acee7f9
Reword some of the readme, add missing semicolon.
misterjoshua Aug 28, 2020
3ecdaab
Consolidate bundle{Function,Layer} into bundle
misterjoshua Aug 29, 2020
faa0771
Fix typo in PythonLayerVersionProps docblock
misterjoshua Aug 29, 2020
d55cb8c
Add lambda.LayerVersionOptions and extend from it
misterjoshua Aug 29, 2020
9669e32
Add a test to cover layer runtime checking exceptions
misterjoshua Aug 29, 2020
dd2e1c5
Merge branch 'master' into lambda-python-layers
misterjoshua Sep 1, 2020
e04b07a
Update vpc test expected json
misterjoshua Sep 1, 2020
045556c
Merge branch 'master' into lambda-python-layers
misterjoshua Sep 1, 2020
191a013
Merge branch 'master' into lambda-python-layers
misterjoshua Sep 6, 2020
af8a79a
Merge branch 'master' into lambda-python-layers
misterjoshua Sep 18, 2020
d9e0f68
Fix variable destructuring oversight
misterjoshua Sep 18, 2020
8747b6b
Merge branch 'master' into lambda-python-layers
misterjoshua Sep 27, 2020
466cae6
Update integ expectations
misterjoshua Sep 27, 2020
f2c74d4
Merge branch 'master' of github.com:aws/aws-cdk into lambda-python-la…
misterjoshua Oct 6, 2020
1eca86b
fix: stacks have changed
misterjoshua Oct 6, 2020
9c2d272
Merge branch 'master' into lambda-python-layers
misterjoshua Oct 11, 2020
d4da14c
fix: duplicate identifier path
misterjoshua Oct 11, 2020
a82411d
Merge branch 'master' into lambda-python-layers
mergify[bot] Oct 12, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 28 additions & 1 deletion packages/@aws-cdk/aws-lambda-python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,38 @@ All other properties of `lambda.Function` are supported, see also the [AWS Lambd

### Module Dependencies

If `requirements.txt` exists at the entry path, the construct will handle installing
If `requirements.txt` or `Pipfile` exists at the entry path, the construct will handle installing
all required modules in a [Lambda compatible Docker container](https://hub.docker.com/r/amazon/aws-sam-cli-build-image-python3.7)
according to the `runtime`.

**Lambda with a requirements.txt**
```
.
├── lambda_function.py # exports a function named 'handler'
├── requirements.txt # has to be present at the entry path
```

**Lambda with a Pipfile**
```
.
├── lambda_function.py # exports a function named 'handler'
├── Pipfile # has to be present at the entry path
├── Pipfile.lock # your lock file
```

**Lambda Layer Support**

You may create a python-based lambda layer with `PythonLayerVersion`. If `PythonLayerVersion` detects a `requirements.txt`
or `Pipfile` at the entry path, then `PythonLayerVersion` will include the dependencies inline with your code in the
layer.

```ts
new lambda.PythonFunction(this, 'MyFunction', {
entry: '/path/to/my/function',
layers: [
new lambda.PythonLayerVersion(this, 'MyLayer', {
entry: '/path/to/my/layer', // point this to your library's directory
}),
],
});
```
18 changes: 18 additions & 0 deletions packages/@aws-cdk/aws-lambda-python/lib/Dockerfile.dependencies
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# The correct AWS SAM build image based on the runtime of the function will be
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only other questions I had here were,

  1. Why are we using rsync instead of cp? Will we ever copy from a URL? If not, is cp -r sufficient?
  2. The current sam image has caching disabled due to user permissions. At least that's what I've experienced in practice. Is there a way to enable it with just image args?

If cp is sufficient, and we can enable pip caching, then could we do away with these two Dockerfiles?

I suppose pipenv support is another reason to keep them. I just wanted to make sure these artifacts were absolutely necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Why are we using rsync instead of cp? Will we ever copy from a URL? If not, is cp -r sufficient?

I'm unsure as to why. Another PR #9763 added this after I started. Perhaps @adamelmore can answer your question.

  1. The current sam image has caching disabled due to user permissions. At least that's what I've experienced in practice. Is there a way to enable it with just image args?

If cp is sufficient, and we can enable pip caching, then could we do away with these two Dockerfiles?

I suppose pipenv support is another reason to keep them. I just wanted to make sure these artifacts were absolutely necessary.

It may be possible to expose a persistent location for the pip cache through a volume and dedicated directory in the user's project directory. But, Docker volume performance can be quite poor in macOS and Windows. Ultimately, the Dockerfiles were used to bake-in the pip dependencies to improve performance by only re-running pip when a change is detected in requirements.txt, Pipfile or Pipfile.lock. For these reasons, I'd tend to prefer the Dockerfile approach.

# passed as build arg. The default allows to do `docker build .` when testing.
ARG IMAGE=amazon/aws-sam-cli-build-image-python3.7
FROM $IMAGE

# Ensure rsync is installed
RUN yum -q list installed rsync &>/dev/null || yum install -y rsync

# Install pipenv so we can create a requirements.txt if we detect pipfile
RUN pip install pipenv

# Install the dependencies in a cacheable layer
WORKDIR /var/dependencies
COPY Pipfile* requirements.tx[t] ./
RUN [ -f 'Pipfile' ] && pipenv lock -r >requirements.txt; \
[ -f 'requirements.txt' ] && pip install -r requirements.txt -t .;

CMD [ "python" ]
68 changes: 52 additions & 16 deletions packages/@aws-cdk/aws-lambda-python/lib/bundling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ import * as path from 'path';
import * as lambda from '@aws-cdk/aws-lambda';
import * as cdk from '@aws-cdk/core';

/**
* Dependency files to exclude from the asset hash.
*/
export const DEPENDENCY_EXCLUDES = ['*.pyc'];

/**
* The location in the image that the bundler image caches dependencies.
*/
export const BUNDLER_DEPENDENCIES_CACHE = '/var/dependencies';

/**
* Options for bundling
*/
Expand All @@ -16,39 +26,65 @@ export interface BundlingOptions {
* The runtime of the lambda function
*/
readonly runtime: lambda.Runtime;

/**
* Output path suffix ('python' for a layer, '.' otherwise)
*/
readonly outputPathSuffix: string;
}

/**
* Produce bundled Lambda asset code
*/
export function bundle(options: BundlingOptions): lambda.AssetCode {
// Bundling image derived from runtime bundling image (AWS SAM docker image)
const image = cdk.BundlingDockerImage.fromAsset(__dirname, {
buildArgs: {
IMAGE: options.runtime.bundlingDockerImage.image,
},
});

let installer = options.runtime === lambda.Runtime.PYTHON_2_7 ? Installer.PIP : Installer.PIP3;
const { entry, runtime, outputPathSuffix } = options;

let hasRequirements = fs.existsSync(path.join(options.entry, 'requirements.txt'));
const hasDeps = hasDependencies(entry);

let depsCommand = chain([
hasRequirements ? `${installer} install -r requirements.txt -t ${cdk.AssetStaging.BUNDLING_OUTPUT_DIR}` : '',
`rsync -r . ${cdk.AssetStaging.BUNDLING_OUTPUT_DIR}`,
const depsCommand = chain([
hasDeps ? `rsync -r ${BUNDLER_DEPENDENCIES_CACHE}/. ${cdk.AssetStaging.BUNDLING_OUTPUT_DIR}/${outputPathSuffix}` : '',
`rsync -r . ${cdk.AssetStaging.BUNDLING_OUTPUT_DIR}/${outputPathSuffix}`,
]);

return lambda.Code.fromAsset(options.entry, {
// Determine which dockerfile to use. When dependencies are present, we use a
// Dockerfile that can create a cacheable layer. We can't use this Dockerfile
// if there aren't dependencies or the Dockerfile will complain about missing
// sources.
const dockerfile = hasDeps
? 'Dockerfile.dependencies'
: 'Dockerfile';

const image = cdk.BundlingDockerImage.fromAsset(entry, {
buildArgs: {
IMAGE: runtime.bundlingDockerImage.image,
},
file: path.join(__dirname, dockerfile),
});

return lambda.Code.fromAsset(entry, {
assetHashType: cdk.AssetHashType.BUNDLE,
exclude: DEPENDENCY_EXCLUDES,
bundling: {
image,
command: ['bash', '-c', depsCommand],
},
});
}

enum Installer {
PIP = 'pip',
PIP3 = 'pip3',
/**
* Checks to see if the `entry` directory contains a type of dependency that
* we know how to install.
*/
export function hasDependencies(entry: string): boolean {
if (fs.existsSync(path.join(entry, 'Pipfile'))) {
return true;
}

if (fs.existsSync(path.join(entry, 'requirements.txt'))) {
return true;
}

return false;
Comment on lines +79 to +87

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*personal preference/style

Suggested change
if (fs.existsSync(path.join(entry, 'Pipfile'))) {
return true;
}
if (fs.existsSync(path.join(entry, 'requirements.txt'))) {
return true;
}
return false;
const pipfileExists = fs.existsSync(path.join(entry, 'Pipfile'));
const requirementsFileExists = fs.existsSync(path.join(entry, 'requirements.txt'));
return pipfileExists || requirementsFileExists

}

function chain(commands: string[]): string {
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-lambda-python/lib/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ export class PythonFunction extends lambda.Function {
...props,
runtime,
code: bundle({
...props,
entry,
runtime,
entry,
outputPathSuffix: '.',
}),
handler: `${index.slice(0, -3)}.${handler}`,
});
Expand Down
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-lambda-python/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './function';
export * from './layer';
54 changes: 54 additions & 0 deletions packages/@aws-cdk/aws-lambda-python/lib/layer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import * as path from 'path';
import * as lambda from '@aws-cdk/aws-lambda';
import * as cdk from '@aws-cdk/core';
import { bundle } from './bundling';

/**
* Properties for PythonLayerVersion
*/
export interface PythonLayerVersionProps extends lambda.LayerVersionOptions {
/**
* The path to the root directory of the lambda layer.
*/
readonly entry: string;

/**
* The runtimes compatible with the python layer.
*
* @default - All runtimes are supported.
*/
readonly compatibleRuntimes?: lambda.Runtime[];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

/**
* A lambda layer version.
*
* @experimental
*/
export class PythonLayerVersion extends lambda.LayerVersion {
constructor(scope: cdk.Construct, id: string, props: PythonLayerVersionProps) {
const compatibleRuntimes = props.compatibleRuntimes ?? [lambda.Runtime.PYTHON_3_7];

// Ensure that all compatible runtimes are python
for (const runtime of compatibleRuntimes) {
if (runtime && runtime.family !== lambda.RuntimeFamily.PYTHON) {
throw new Error('Only `PYTHON` runtimes are supported.');
}
}

// Entry and defaults
const entry = path.resolve(props.entry);
// Pick the first compatibleRuntime to use for bundling or PYTHON_3_7
const runtime = compatibleRuntimes[0] ?? lambda.Runtime.PYTHON_3_7;

super(scope, id, {
...props,
compatibleRuntimes,
code: bundle({
entry,
runtime,
outputPathSuffix: 'python',
}),
});
}
}