Skip to content

Commit b6e4340

Browse files
authored
Merge pull request #12 from fok666/feature/lambda-python-for-amazonlinux2
feature/lambda python for amazonlinux2
2 parents 106f937 + 244ab42 commit b6e4340

4 files changed

Lines changed: 126 additions & 13 deletions

File tree

.github/workflows/build-and-release.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ jobs:
3232
- name: Set up Docker Buildx
3333
uses: docker/setup-buildx-action@v3
3434

35+
- name: Determine Dockerfile
36+
id: dockerfile
37+
run: |
38+
if [[ "${{ matrix.python-version }}" == "3.10" || "${{ matrix.python-version }}" == "3.11" ]]; then
39+
echo "path=Dockerfile.al2" >> $GITHUB_OUTPUT
40+
else
41+
echo "path=Dockerfile.al2023" >> $GITHUB_OUTPUT
42+
fi
43+
3544
- name: Log in to Container Registry
3645
if: github.event_name != 'pull_request'
3746
uses: docker/login-action@v3
@@ -57,6 +66,7 @@ jobs:
5766
uses: docker/build-push-action@v6
5867
with:
5968
context: .
69+
file: ${{ steps.dockerfile.outputs.path }}
6070
platforms: linux/amd64
6171
build-args: |
6272
PYTHON_VERSION=${{ matrix.python-version }}
@@ -82,6 +92,15 @@ jobs:
8292
- name: Set up Docker Buildx
8393
uses: docker/setup-buildx-action@v3
8494

95+
- name: Determine Dockerfile
96+
id: dockerfile
97+
run: |
98+
if [[ "${{ matrix.python-version }}" == "3.10" || "${{ matrix.python-version }}" == "3.11" ]]; then
99+
echo "path=Dockerfile.al2" >> $GITHUB_OUTPUT
100+
else
101+
echo "path=Dockerfile.al2023" >> $GITHUB_OUTPUT
102+
fi
103+
85104
- name: Log in to Container Registry
86105
if: github.event_name != 'pull_request'
87106
uses: docker/login-action@v3
@@ -107,6 +126,7 @@ jobs:
107126
uses: docker/build-push-action@v6
108127
with:
109128
context: .
129+
file: ${{ steps.dockerfile.outputs.path }}
110130
platforms: linux/arm64
111131
build-args: |
112132
PYTHON_VERSION=${{ matrix.python-version }}

Dockerfile.al2

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
ARG PYTHON_VERSION=3.11
2+
FROM amazonlinux:2
3+
4+
# Install base dependencies
5+
RUN yum -y install git zip tar gzip gcc gcc-c++ cmake make \
6+
wget openssl11-devel bzip2-devel libffi-devel \
7+
zlib-devel xz-devel sqlite-devel readline-devel xz && \
8+
yum clean all
9+
10+
# Accept Python version as build arg (supports 3.10, 3.11)
11+
ARG PYTHON_VERSION
12+
13+
# Install Python - build from source for AL2
14+
RUN cd /tmp && \
15+
wget https://www.python.org/ftp/python/${PYTHON_VERSION}.0/Python-${PYTHON_VERSION}.0.tar.xz && \
16+
tar xf Python-${PYTHON_VERSION}.0.tar.xz && \
17+
cd Python-${PYTHON_VERSION}.0 && \
18+
./configure --enable-optimizations --with-ensurepip=install && \
19+
make -j$(nproc) && \
20+
make altinstall && \
21+
cd / && \
22+
rm -rf /tmp/Python-${PYTHON_VERSION}.0*
23+
24+
# Create symlink for easier access
25+
RUN ln -sf /usr/local/bin/python${PYTHON_VERSION} /usr/local/bin/python3 && \
26+
ln -sf /usr/local/bin/pip${PYTHON_VERSION} /usr/local/bin/pip3
27+
28+
# Upgrade pip
29+
RUN python3 -m pip install --upgrade pip
30+
31+
ADD package.sh /
32+
RUN chmod +x /package.sh
33+
34+
ENTRYPOINT ["/package.sh"]

Dockerfile.al2023

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
ARG PYTHON_VERSION=3.13
2+
FROM amazonlinux:2023
3+
4+
# Install base dependencies
5+
RUN dnf -y install git zip tar gzip gcc gcc-c++ cmake \
6+
wget openssl-devel bzip2-devel libffi-devel \
7+
zlib-devel xz-devel sqlite-devel readline-devel xz && \
8+
dnf clean all
9+
10+
# Accept Python version as build arg (supports 3.10, 3.11, 3.12, 3.13, 3.14)
11+
ARG PYTHON_VERSION
12+
13+
# Install Python - either from dnf or build from source
14+
RUN if dnf list available python${PYTHON_VERSION} 2>/dev/null; then \
15+
echo "Installing Python ${PYTHON_VERSION} from dnf..."; \
16+
dnf -y install python${PYTHON_VERSION} python${PYTHON_VERSION}-pip python${PYTHON_VERSION}-devel && \
17+
dnf clean all; \
18+
else \
19+
echo "Building Python ${PYTHON_VERSION} from source..."; \
20+
cd /tmp && \
21+
wget https://www.python.org/ftp/python/${PYTHON_VERSION}.0/Python-${PYTHON_VERSION}.0.tar.xz && \
22+
tar xf Python-${PYTHON_VERSION}.0.tar.xz && \
23+
cd Python-${PYTHON_VERSION}.0 && \
24+
./configure --enable-optimizations --with-ensurepip=install && \
25+
make -j$(nproc) && \
26+
make altinstall && \
27+
cd / && \
28+
rm -rf /tmp/Python-${PYTHON_VERSION}.0*; \
29+
fi
30+
31+
# Create symlink for easier access
32+
RUN if [ -f /usr/local/bin/python${PYTHON_VERSION} ]; then \
33+
ln -sf /usr/local/bin/python${PYTHON_VERSION} /usr/local/bin/python3; \
34+
ln -sf /usr/local/bin/pip${PYTHON_VERSION} /usr/local/bin/pip3; \
35+
elif [ -f /usr/bin/python${PYTHON_VERSION} ]; then \
36+
ln -sf /usr/bin/python${PYTHON_VERSION} /usr/local/bin/python3; \
37+
ln -sf /usr/bin/pip${PYTHON_VERSION} /usr/local/bin/pip3; \
38+
fi
39+
40+
# Upgrade pip (skip if pip not available as module)
41+
RUN python3 -m pip install --upgrade pip 2>/dev/null || \
42+
python3 -m ensurepip && python3 -m pip install --upgrade pip
43+
44+
ADD package.sh /
45+
RUN chmod +x /package.sh
46+
47+
ENTRYPOINT ["/package.sh"]

readme.md

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@ Simply run the build script with no arguments:
3030
- Reads from `requirements.txt` in the current directory
3131
- Builds for both x86_64 and arm64 architectures
3232
- Creates single combined archive per architecture
33+
- Automatically uses the correct Amazon Linux version for Lambda compatibility
3334

3435
**Result**: `combined-python3.14-x86_64.zip` and `combined-python3.14-aarch64.zip` in `./output/`
3536

37+
> **Note**: This tool uses Amazon Linux 2 for Python 3.10-3.11 and Amazon Linux 2023 for Python 3.12+ to ensure compatibility with [AWS Lambda Python runtimes](https://docs.aws.amazon.com/lambda/latest/dg/lambda-python.html).
38+
3639
### Customizing the Build
3740

3841
```bash
@@ -131,15 +134,21 @@ docker run --rm \
131134

132135
## Supported Python Versions
133136

134-
| Python Version | Amazon Linux 2023 | Installation Method | Status |
135-
|----------------|-------------------|---------------------|--------|
136-
| 3.10 | ✅ Available | dnf package | Supported |
137-
| 3.11 | ✅ Available | dnf package | Supported |
138-
| 3.12 | ✅ Available | dnf package | Supported |
139-
| 3.13 | ✅ Available | dnf package | Supported |
140-
| 3.14 | ⚙️ Built from source | Compiled with optimizations | Supported (Default) |
137+
This project automatically uses the appropriate Amazon Linux version based on [AWS Lambda Python runtime requirements](https://docs.aws.amazon.com/lambda/latest/dg/lambda-python.html):
138+
139+
| Python Version | Base OS | Installation Method | Lambda Runtime Compatibility |
140+
|----------------|---------|---------------------|------------------------------|
141+
| 3.10 | Amazon Linux 2 | Built from source | ✅ python3.10 runtime |
142+
| 3.11 | Amazon Linux 2 | Built from source | ✅ python3.11 runtime |
143+
| 3.12 | Amazon Linux 2023 | dnf package | ✅ python3.12 runtime |
144+
| 3.13 | Amazon Linux 2023 | dnf package | ✅ python3.13 runtime |
145+
| 3.14 | Amazon Linux 2023 | Built from source | ⚙️ Future runtime (Default) |
141146

142-
**Note**: Python 3.14 is automatically compiled from source when building the Docker image, which takes longer but ensures compatibility with the latest Python version.
147+
**Key Points:**
148+
- **Python 3.10 & 3.11**: Use Amazon Linux 2 (matches Lambda runtime environment)
149+
- **Python 3.12 & 3.13**: Use Amazon Linux 2023 (matches Lambda runtime environment)
150+
- **Python 3.14**: Uses Amazon Linux 2023, compiled from source for latest features
151+
- All versions are built with development headers for compiling binary packages
143152

144153
## Output Formats
145154

@@ -235,7 +244,9 @@ Build for Python 3.10 with specific requirements:
235244

236245
```
237246
.
238-
├── Dockerfile # Multi-version, multi-arch capable Docker image
247+
├── Dockerfile.al2 # Amazon Linux 2 image (Python 3.10-3.11)
248+
├── Dockerfile.al2023 # Amazon Linux 2023 image (Python 3.12-3.14)
249+
├── Dockerfile # Default (points to AL2023)
239250
├── package.sh # Packaging script with requirements.txt support
240251
├── build-multiarch.sh # Helper script for multi-arch builds
241252
├── requirements.example.txt
@@ -244,11 +255,12 @@ Build for Python 3.10 with specific requirements:
244255

245256
## How It Works
246257

247-
1. **Base Image**: Uses Amazon Linux 2023 for Lambda runtime compatibility
258+
1. **Base Image Selection**: Automatically chooses Amazon Linux 2 (for Python 3.10-3.11) or Amazon Linux 2023 (for Python 3.12+) to match AWS Lambda runtime environments
248259
2. **Python Installation**: Installs specified Python version (3.10-3.14) with development headers
249-
3. **Package Installation**: Uses pip with `--target` flag to install packages
250-
4. **Packaging**: Creates zip archives with proper Lambda Layer structure
251-
5. **Multi-arch**: Leverages Docker buildx for platform-specific builds
260+
3. **Package Installation**: Uses pip with `--target` flag to install packages with proper dependencies
261+
4. **Packaging**: Creates zip archives with proper Lambda Layer structure (`python/lib/pythonX.Y/site-packages/`)
262+
5. **Multi-arch**: Leverages Docker buildx for platform-specific builds (x86_64 and arm64)
263+
6. **Runtime Compatibility**: Ensures binary compatibility with AWS Lambda execution environments
252264

253265
## Troubleshooting
254266

0 commit comments

Comments
 (0)