You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(lambda-python): support for providing a custom bundling docker image (#18082)
This refactors the bundling process to match the NodeJs and Go Lambda functions and allows providing a custom bundling docker image.
Changes:
- refactor bundling to use `cdk.BundlingOptions`
- Use updated `Bundling` class
- Update tests to use updated `Bundling` class
Fixes#10298, #12949, #15391, #16234, #15306
BREAKING CHANGE: `assetHashType` and `assetHash` properties moved to new `bundling` property.
----
*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Copy file name to clipboardExpand all lines: packages/@aws-cdk/aws-lambda-python/README.md
+103-24
Original file line number
Diff line number
Diff line change
@@ -27,29 +27,61 @@ Define a `PythonFunction`:
27
27
```ts
28
28
newlambda.PythonFunction(this, 'MyFunction', {
29
29
entry: '/path/to/my/function', // required
30
-
runtime: Runtime.PYTHON_3_6, // required
30
+
runtime: Runtime.PYTHON_3_8, // required
31
31
index: 'my_index.py', // optional, defaults to 'index.py'
32
32
handler: 'my_exported_func', // optional, defaults to 'handler'
33
33
});
34
34
```
35
35
36
36
All other properties of `lambda.Function` are supported, see also the [AWS Lambda construct library](https://github.com/aws/aws-cdk/tree/master/packages/%40aws-cdk/aws-lambda).
37
37
38
-
## Module Dependencies
38
+
## Python Layer
39
39
40
-
If `requirements.txt` or `Pipfile` exists at the entry path, the construct will handle installing
41
-
all required modules in a [Lambda compatible Docker container](https://gallery.ecr.aws/sam/build-python3.7)
42
-
according to the `runtime` and with the Docker platform based on the target architecture of the Lambda function.
40
+
You may create a python-based lambda layer with `PythonLayerVersion`. If `PythonLayerVersion` detects a `requirements.txt`
41
+
or `Pipfile` or `poetry.lock` with the associated `pyproject.toml` at the entry path, then `PythonLayerVersion` will include the dependencies inline with your code in the
42
+
layer.
43
+
44
+
Define a `PythonLayerVersion`:
45
+
46
+
```ts
47
+
newlambda.PythonLayerVersion(this, 'MyLayer', {
48
+
entry: '/path/to/my/layer', // point this to your library's directory
49
+
})
50
+
```
51
+
52
+
A layer can also be used as a part of a `PythonFunction`:
53
+
54
+
```ts
55
+
newlambda.PythonFunction(this, 'MyFunction', {
56
+
entry: '/path/to/my/function',
57
+
runtime: Runtime.PYTHON_3_8,
58
+
layers: [
59
+
newlambda.PythonLayerVersion(this, 'MyLayer', {
60
+
entry: '/path/to/my/layer', // point this to your library's directory
61
+
}),
62
+
],
63
+
});
64
+
```
65
+
66
+
## Packaging
67
+
68
+
If `requirements.txt`, `Pipfile` or `poetry.lock` exists at the entry path, the construct will handle installing all required modules in a [Lambda compatible Docker container](https://gallery.ecr.aws/sam/build-python3.7) according to the `runtime` and with the Docker platform based on the target architecture of the Lambda function.
43
69
44
70
Python bundles are only recreated and published when a file in a source directory has changed.
45
71
Therefore (and as a general best-practice), it is highly recommended to commit a lockfile with a
46
-
list of all transitive dependencies and their exact versions.
47
-
This will ensure that when any dependency version is updated, the bundle asset is recreated and uploaded.
72
+
list of all transitive dependencies and their exact versions. This will ensure that when any dependency version is updated, the bundle asset is recreated and uploaded.
73
+
74
+
To that end, we recommend using [`pipenv`] or [`poetry`] which have lockfile support.
1. Infers the packaging type based on the files present.
82
+
2. If it sees a `Pipfile` or a `poetry.lock` file, it exports it to a compatible `requirements.txt` file with credentials (if they're available in the source files or in the bundling container).
83
+
3. Installs dependencies using `pip`.
84
+
4. Copies the dependencies into an asset that is bundled for the Lambda package.
53
85
54
86
**Lambda with a requirements.txt**
55
87
@@ -73,24 +105,71 @@ To that end, we recommend using [`pipenv`] or [`poetry`] which has lockfile supp
73
105
```plaintext
74
106
.
75
107
├── lambda_function.py # exports a function named 'handler'
76
-
├── pyproject.toml # has to be present at the entry path
77
-
├── poetry.lock # your poetry lock file
108
+
├── pyproject.toml # your poetry project definition
109
+
├── poetry.lock # your poetry lock file has to be present at the entry path
78
110
```
79
111
80
-
**Lambda Layer Support**
112
+
## Custom Bundling
81
113
82
-
You may create a python-based lambda layer with `PythonLayerVersion`. If `PythonLayerVersion` detects a `requirements.txt`
83
-
or `Pipfile` or `poetry.lock` with the associated `pyproject.toml` at the entry path, then `PythonLayerVersion` will include the dependencies inline with your code in the
84
-
layer.
114
+
Custom bundling can be performed by passing in additional build arguments that point to index URLs to private repos, or by using an entirely custom Docker images for bundling dependencies. The build args currently supported are:
115
+
116
+
-`PIP_INDEX_URL`
117
+
-`PIP_EXTRA_INDEX_URL`
118
+
-`HTTPS_PROXY`
119
+
120
+
Additional build args for bundling that refer to PyPI indexes can be specified as:
85
121
86
122
```ts
87
-
newlambda.PythonFunction(this, 'MyFunction', {
88
-
entry: '/path/to/my/function',
89
-
runtime: Runtime.PYTHON_3_6,
90
-
layers: [
91
-
newlambda.PythonLayerVersion(this, 'MyLayer', {
92
-
entry: '/path/to/my/layer', // point this to your library's directory
If using a custom Docker image for bundling, the dependencies are installed with `pip`, `pipenv` or `poetry` by using the `Packaging` class. A different bundling Docker image that is in the same directory as the function can be specified as:
136
+
137
+
```ts
138
+
const entry ='/path/to/function';
139
+
const image =DockerImage.fromBuild(entry);
140
+
141
+
newlambda.PythonFunction(this, 'function', {
142
+
entry,
143
+
runtime: Runtime.PYTHON_3_8,
144
+
bundling: { image },
145
+
});
146
+
```
147
+
148
+
## Custom Bundling with Code Artifact
149
+
150
+
To use a Code Artifact PyPI repo, the `PIP_INDEX_URL` for bundling the function can be customized (requires AWS CLI in the build environment):
0 commit comments