1
1
# lambda-python-custom
2
2
3
- Use Python >= 3.10 on AWS Lambda
3
+ Use Any Python Version on AWS Lambda
4
4
5
- Currently, AWS Lambda only supports Python versions 3.7 - 3.9. This project shows how to use a newer version by creating
6
- a custom runtime. This is documented by AWS in several different places and is tedious to piece together what's truly
7
- required.
8
-
9
- Update: I've created a [ simpler example] ( simple-example ) that starts with the Debian-based official Python image and
10
- therefore doesn't require building it from source. The only dependency truly required is
11
- the [ awslambdaric] ( https://github.com/aws/aws-lambda-python-runtime-interface-client ) , and everything can be copied into
12
- the same directory in the image.
5
+ This project was created when AWS Lambda only supported Python versions 3.7 - 3.9, despite 3.10 and 3.11 having been
6
+ released for quite a while. Now AWS is again keeping up with Python versions, but this project shows how to use any
7
+ version by creating a custom runtime. The AWS documentation for how do this has improved but is still spread across
8
+ several different sites and pages.
13
9
14
10
## Dockerfile
15
11
16
- The Docker image is loosely based
17
- on [ the one used by AWS Lambda for Python 3.9] ( https://gallery.ecr.aws/lambda/python ) (see
18
- also [ here] ( https://github.com/aws/aws-lambda-base-images/tree/python3.9 ) ), and incorporates its default bootstrap files
19
- under [ /lambda] ( lambda ) . It's built via GitHub actions and deployed with Terraform.
12
+ The main Docker image is now based on the new
13
+ [ Amazon Linux 2023 Provided image for Lambda] ( https://gallery.ecr.aws/lambda/provided ) (also see
14
+ https://aws.amazon.com/blogs/compute/introducing-the-amazon-linux-2023-runtime-for-aws-lambda/ ). It's built via GitHub
15
+ actions and deployed with Terraform. This means a modern version of OpenSSL is available without having to build it from
16
+ source. However, the minimal image it's based on made verifying the Python source download more difficult
17
+ (see https://github.com/keithly/lambda-python-custom/issues/78 ).
20
18
21
19
The Dockerfile follows all the best practices I'm aware of. :) There are several ARGs for passing specific versions of
22
- the base image, OpenSSL, and Python, but I didn't attempt to pin every dependency. There's a tradeoff between
23
- reproducibility and convenience.
20
+ the base image and Python, but I didn't attempt to pin every dependency. There's a tradeoff between reproducibility and
21
+ convenience.
24
22
25
- - Starts with Amazon Linux 2 , creates a builder stage from it, copies build artifacts back into the base.
26
- - Builds OpenSSL and Python from source, checking pgp signatures. The python build options optimize the build for speed
27
- of execution. The dependencies and build options could no doubt be tweaked, but this is the simplest solution I found
28
- that makes a functional Python build.
23
+ - Starts with Amazon Linux 2023 , creates a builder stage from it, copies build artifacts back into the base.
24
+ - Builds Python from source The python build options optimize the build for speed of execution. The dependencies and
25
+ build options could no doubt be tweaked, but this is the simplest solution I found that makes a functional Python
26
+ build.
29
27
- Links "python3" to "python"
30
- - Curls the latest version of
28
+ - Installs the latest version of
31
29
the [ AWS Lambda Runtime Interface Emulator] ( https://github.com/aws/aws-lambda-runtime-interface-emulator/ )
32
30
- Installs the latest versions of pip, setuptools, wheel,
33
31
then [ awslambdaric] ( https://github.com/aws/aws-lambda-python-runtime-interface-client ) and boto3
34
32
- runs as a non-root user (though this may not matter for running on Lambda)
35
33
36
- Maybe it would make more sense to use the Python 3.9 lambda image as the base. Doing so would likely entail a different
37
- set of tradeoffs with trying to remove Python 3.9 and edit configs.
34
+ There's also a [ simpler example] ( simple-example ) that starts with the Debian-based official Python image and
35
+ therefore doesn't require building it from source. The only dependency truly required is
36
+ the [ awslambdaric] ( https://github.com/aws/aws-lambda-python-runtime-interface-client ) , and everything can be copied into
37
+ the same directory in the image.
38
38
39
39
## AWS Lambda Runtime Interface Emulator
40
40
@@ -55,8 +55,7 @@ curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d
55
55
56
56
## Lambda Function Code
57
57
58
- [ src/lambda-poc] ( src/lambda-poc ) contains a basic function that returns HTTP 200, printing the Python version and lambda
59
- event payload.
58
+ [ src/lambda-poc] ( src/lambda-poc ) contains a basic function that returns HTTP 200 and some json.
60
59
61
60
## AWS Infrastructure
62
61
0 commit comments