Skip to content

Commit 4b37157

Browse files
authored
fix(lambda-python): bundling with poetry is broken (#21945)
It looks like something was changed in the base image and there is no longer write access to the `/tmp` directory which causes bundling with poetry to fail (see linked issue). This PR updates the Dockerfile to create a new cache location for both `pip` and `poetry` and switches to using a virtualenv for python so that it is no longer using root. To test this I executed the `integ.function.poetry` integration test both before (to reproduce the error) and after the fix. I'm actually not sure why our integration tests didn't start failing in the pipeline. The only thing I can think of is that we are caching the docker images and it just hasn't pulled down a newer one that has this issue. fixes #21867 ---- ### All Submissions: * [ ] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent bad426e commit 4b37157

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

packages/@aws-cdk/aws-lambda-python/lib/Dockerfile

+25
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,35 @@ ARG PIP_INDEX_URL
77
ARG PIP_EXTRA_INDEX_URL
88
ARG HTTPS_PROXY
99

10+
# Create a new location for the pip cache
11+
# Ensure all users can write to pip cache
12+
RUN mkdir /tmp/pip-cache && \
13+
chmod -R 777 /tmp/pip-cache
14+
15+
# set the cache location
16+
ENV PIP_CACHE_DIR=/tmp/pip-cache
17+
18+
# create a new virtualenv for python to use
19+
# so that it isn't using root
20+
RUN python -m venv /usr/app/venv
21+
ENV PATH="/usr/app/venv/bin:$PATH"
22+
1023
# Upgrade pip (required by cryptography v3.4 and above, which is a dependency of poetry)
1124
RUN pip install --upgrade pip
1225

26+
1327
# pipenv 2022.4.8 is the last version with Python 3.6 support
1428
RUN pip install pipenv==2022.4.8 poetry
1529

30+
# Create a new location for the poetry cache
31+
# Ensure all users can write to poetry cache
32+
RUN mkdir /tmp/poetry-cache && \
33+
chmod -R 777 /tmp/poetry-cache
34+
35+
# set the poetry cache
36+
ENV POETRY_CACHE_DIR=/tmp/poetry-cache
37+
38+
# create non root user and change allow execute command for non root user
39+
RUN /sbin/useradd -u 1000 user && chmod 711 /
40+
1641
CMD [ "python" ]

packages/@aws-cdk/aws-lambda-python/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"@aws-cdk/assertions": "0.0.0",
7676
"@aws-cdk/cdk-build-tools": "0.0.0",
7777
"@aws-cdk/integ-runner": "0.0.0",
78+
"@aws-cdk/integ-tests": "0.0.0",
7879
"@aws-cdk/pkglint": "0.0.0",
7980
"@types/jest": "^27.5.2"
8081
},

packages/@aws-cdk/aws-lambda-python/test/integ.function.poetry.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
// disabling update workflow because we don't want to include the assets in the snapshot
2-
// python bundling changes the asset hash pretty frequently
3-
/// !cdk-integ pragma:disable-update-workflow
41
import * as path from 'path';
52
import { Runtime } from '@aws-cdk/aws-lambda';
63
import { App, CfnOutput, Stack, StackProps } from '@aws-cdk/core';
4+
import { IntegTest } from '@aws-cdk/integ-tests';
75
import { Construct } from 'constructs';
86
import * as lambda from '../lib';
97

@@ -35,5 +33,13 @@ class TestStack extends Stack {
3533
}
3634

3735
const app = new App();
38-
new TestStack(app, 'cdk-integ-lambda-python');
36+
const testCase = new TestStack(app, 'cdk-integ-lambda-python');
37+
38+
new IntegTest(app, 'poetry', {
39+
testCases: [testCase],
40+
// disabling update workflow because we don't want to include the assets in the snapshot
41+
// python bundling changes the asset hash pretty frequently
42+
stackUpdateWorkflow: false,
43+
});
44+
3945
app.synth();

0 commit comments

Comments
 (0)