Skip to content

Commit 11122ec

Browse files
authored
Merge branch 'main' into test-image
2 parents 72f7dd6 + aea8d46 commit 11122ec

File tree

11 files changed

+83
-196
lines changed

11 files changed

+83
-196
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ jobs:
9090

9191
- uses: hashicorp/setup-terraform@v3
9292
with:
93-
terraform_version: 1.6.3
93+
terraform_version: 1.6.5
9494

9595
- name: Config Terraform plugin cache
9696
run: |

.tool-versions

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
python 3.11.6
2-
terraform 1.6.4
1+
python 3.12.1
2+
terraform 1.6.5
33
tflint 0.49.0

Dockerfile

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,22 @@
1-
ARG AMAZONLINUX_VERSION=2.0.20231101.0
2-
ARG ARCH=amd64
3-
FROM public.ecr.aws/amazonlinux/amazonlinux:${AMAZONLINUX_VERSION}-${ARCH} as base
4-
ENV LANG=en_US.UTF-8 \
5-
TZ=:/etc/localtime \
6-
PATH=/var/lang/bin:/usr/local/bin:/usr/bin/:/bin:/opt/bin \
7-
LD_LIBRARY_PATH=/var/lang/lib:/lib64:/usr/lib64:/var/runtime:/var/runtime/lib:/var/task:/var/task/lib:/opt/lib \
8-
LAMBDA_TASK_ROOT=/var/task \
9-
LAMBDA_RUNTIME_DIR=/var/runtime
10-
RUN yum -y update && \
11-
yum -y install shadow-utils && \
12-
yum clean all
1+
ARG AL_PROVIDED_VERSION=al2023.2023.12.06.10
2+
ARG ARCH=x86_64
3+
FROM public.ecr.aws/lambda/provided:${AL_PROVIDED_VERSION}-${ARCH} as base
4+
RUN dnf -y update && \
5+
dnf -y install shadow-utils && \
6+
dnf clean all
137

148
FROM base as builder
15-
RUN yum -y install yum-utils && \
16-
yum -y groupinstall "Development Tools" && \
17-
yum-builddep -y python3 && \
18-
yum clean all
9+
RUN dnf -y update && \
10+
dnf -y install gcc openssl-devel bzip2-devel libffi-devel xz-devel zlib-devel tar xz && \
11+
dnf clean all
1912

20-
ARG OPENSSL_VERSION=1.1.1s
21-
ARG OPENSSL_KEY=B8EF1A6BA9DA2D5C
22-
RUN cd "$(mktemp -d)" && \
23-
curl https://www.openssl.org/source/openssl-${OPENSSL_VERSION}.tar.gz.asc --remote-name && \
24-
curl https://www.openssl.org/source/openssl-${OPENSSL_VERSION}.tar.gz --remote-name && \
25-
gpg --keyserver hkps://keys.openpgp.org --recv-keys ${OPENSSL_KEY} && \
26-
gpg --verify openssl-${OPENSSL_VERSION}.tar.gz.asc openssl-${OPENSSL_VERSION}.tar.gz && \
27-
tar xf openssl-${OPENSSL_VERSION}.tar.gz && \
28-
cd openssl-${OPENSSL_VERSION} && \
29-
./config --prefix=/var/lang && \
30-
make -j "$(nproc)" && \
31-
make install
13+
ARG PYTHON_VERSION=3.12.1
3214

33-
ARG PYTHON_VERSION=3.11.6
34-
ARG PYTHON_KEY=64E628F8D684696D
3515
RUN cd "$(mktemp -d)" && \
36-
curl https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tar.xz.asc --remote-name && \
37-
curl https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tar.xz --remote-name && \
38-
gpg --keyserver hkps://keys.openpgp.org --recv-keys ${PYTHON_KEY} && \
39-
gpg --verify Python-${PYTHON_VERSION}.tar.xz.asc Python-${PYTHON_VERSION}.tar.xz && \
16+
curl -O https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tar.xz && \
4017
tar xf Python-${PYTHON_VERSION}.tar.xz && \
4118
cd Python-${PYTHON_VERSION} && \
42-
./configure --prefix=/var/lang --with-openssl=/var/lang --enable-optimizations --with-lto=full --with-system-ffi --with-computed-gotos --enable-loadable-sqlite-extensions && \
19+
./configure --prefix=/var/lang --enable-optimizations --with-lto=full --with-system-ffi --with-computed-gotos --enable-loadable-sqlite-extensions && \
4320
make -j "$(nproc)" && \
4421
make install
4522

@@ -52,21 +29,19 @@ RUN ln -s /var/lang/bin/python3 /var/lang/bin/python && \
5229
ln -s /var/lang/bin/pydoc3 /var/lang/bin/pydoc && \
5330
ln -s /var/lang/bin/python3-config /var/lang/bin/python-config
5431

55-
COPY lambda/lambda-entrypoint.sh /
56-
COPY lambda/install-rie.sh /
57-
COPY lambda/runtime /var/runtime
32+
WORKDIR /var/task
33+
COPY lambda /var/task
5834

5935
RUN ./install-rie.sh
6036

6137
RUN python3 -m pip install -U --no-cache-dir pip setuptools wheel && \
62-
python3 -m pip install --no-cache-dir --target /var/runtime awslambdaric boto3
38+
python3 -m pip install --no-cache-dir --target /var/task awslambdaric boto3
6339

64-
WORKDIR /var/task
6540
COPY src src
6641

6742
RUN /usr/sbin/useradd lambdauser -d /var/task
6843
USER lambdauser
6944

7045
ENV APP_VERSION=1.0.0
7146

72-
ENTRYPOINT [ "/lambda-entrypoint.sh", "src/lambda-poc/lambda_function.handler"]
47+
ENTRYPOINT ["/var/task/lambda-entrypoint.sh", "src/lambda-poc/lambda_function.handler"]

README.md

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11
# lambda-python-custom
22

3-
Use Python >= 3.10 on AWS Lambda
3+
Use Any Python Version on AWS Lambda
44

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.
139

1410
## Dockerfile
1511

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).
2018

2119
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.
2422

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.
2927
- Links "python3" to "python"
30-
- Curls the latest version of
28+
- Installs the latest version of
3129
the [AWS Lambda Runtime Interface Emulator](https://github.com/aws/aws-lambda-runtime-interface-emulator/)
3230
- Installs the latest versions of pip, setuptools, wheel,
3331
then [awslambdaric](https://github.com/aws/aws-lambda-python-runtime-interface-client) and boto3
3432
- runs as a non-root user (though this may not matter for running on Lambda)
3533

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.
3838

3939
## AWS Lambda Runtime Interface Emulator
4040

@@ -55,8 +55,7 @@ curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d
5555

5656
## Lambda Function Code
5757

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.
6059

6160
## AWS Infrastructure
6261

infra/tf/.terraform.lock.hcl

Lines changed: 30 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

infra/tf/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ terraform {
1010
required_providers {
1111
aws = {
1212
source = "hashicorp/aws"
13-
version = "5.27.0"
13+
version = "5.30.0"
1414
}
1515
}
1616
}

lambda/lambda-entrypoint.sh

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
#!/bin/sh
2-
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
32

43
if [ $# -ne 1 ]; then
54
echo "entrypoint requires the handler name to be the first argument" 1>&2
65
exit 142
76
fi
8-
export _HANDLER="$1"
97

10-
RUNTIME_ENTRYPOINT=/var/runtime/bootstrap
118
if [ -z "${AWS_LAMBDA_RUNTIME_API}" ]; then
12-
exec /usr/local/bin/aws-lambda-rie $RUNTIME_ENTRYPOINT
9+
exec /usr/local/bin/aws-lambda-rie /var/lang/bin/python -m awslambdaric --log-level "debug" "$@"
1310
else
14-
exec $RUNTIME_ENTRYPOINT
15-
fi
11+
exec /var/lang/bin/python -m awslambdaric "$@"
12+
fi

lambda/runtime/bootstrap

Lines changed: 0 additions & 19 deletions
This file was deleted.

lambda/runtime/bootstrap.py

Lines changed: 0 additions & 61 deletions
This file was deleted.

lambda/runtime/runtime-release

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)