Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aws-lambda: DockerImageCode.fromImageAsset not caching as-expected #29240

Closed
mccauleyp opened this issue Feb 23, 2024 · 3 comments
Closed

aws-lambda: DockerImageCode.fromImageAsset not caching as-expected #29240

mccauleyp opened this issue Feb 23, 2024 · 3 comments
Labels
@aws-cdk/aws-lambda Related to AWS Lambda bug This issue is a bug. p2 response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.

Comments

@mccauleyp
Copy link

mccauleyp commented Feb 23, 2024

Describe the bug

I have a simple Dockerfile:

FROM public.ecr.aws/lambda/python:3.11

# Copy requirements.txt separately and run `pip install` first to facilitate build caching
COPY requirements.txt .
RUN pip install --upgrade pip && \
    pip install -r ./requirements.txt

COPY handlers ${LAMBDA_TASK_ROOT}/handlers

If I build this outside of CDK, the caching works as-expected in that the RUN pip install command only runs if requirements.txt changes, otherwise the cache is used.

When I build it inside CDK, e.g.:

        self.my_function = aws_lambda.DockerImageFunction(
            scope,
            "MyLambda",
            code=aws_lambda.DockerImageCode.from_image_asset(
                platform=aws_ecr_assets.Platform.LINUX_AMD64,
                directory="/path/to/dockerfile/dir/,
                cmd=["handlers.entrypoint.handler"],
            ),
        )

The pip install re-runs any time the code changes, event if requirements.txt is unchanged. I'm guessing that's related to the Dockerfile, requirements, and app code all being part of the same new asset, e.g.:

- cdk.out/
- - asset.___/
- - - handlers/
- - - Dockerfile
- - - requirements.txt

Is it possible to refactor the CDK code to allow caching? Is there a way for me to merge two assets in a single build, so the Dockerfile and requirements.txt don't get duplicated in a new hash?

Expected Behavior

CDK docker build caches in the same way as outside of CDK.

Current Behavior

CDK build requires rerunning lengthy steps.

Reproduction Steps

See above.

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.128.0

Framework Version

No response

Node.js Version

v20.11.0

OS

OSX

Language

Python

Language Version

No response

Other information

No response

@mccauleyp mccauleyp added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Feb 23, 2024
@github-actions github-actions bot added the @aws-cdk/aws-lambda Related to AWS Lambda label Feb 23, 2024
@pahud
Copy link
Contributor

pahud commented Feb 26, 2024

The pip install re-runs any time the code changes

What is the code you mentioned? Is it the python code in your docker directory or the CDK code?

I just tested this

    new lambda.DockerImageFunction(this, 'MyLamsbda', {
      code: lambda.DockerImageCode.fromImageAsset(
        path.join(__dirname, '../docker.d/python.d'),
      ),
      memorySize: 256,
    })

And my Dockerfile like

FROM public.ecr.aws/lambda/python:3.11

# Copy requirements.txt separately and run `pip install` first to facilitate build caching
COPY requirements.txt .
RUN pip install --upgrade pip && \
    pip install -r ./requirements.txt

# COPY handlers ${LAMBDA_TASK_ROOT}/handlers
COPY index.py ${LAMBDA_TASK_ROOT}/index.py

I modified the index.py and I saw the pip install is actually cached.

#7 [3/4] RUN pip install --upgrade pip &&     pip install -r ./requirements.txt
#7 CACHED

@pahud pahud added p2 response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. and removed needs-triage This issue or PR still needs to be triaged. labels Feb 26, 2024
@mccauleyp
Copy link
Author

@pahud Thanks for looking into this! I was talking about the Python code, and you tested it in the way I intended. I'm still seeing my CDK builds not use the cache while a regular build does, but your test suggests that it's probably something specific to my environment, so I'll close this and try to sort that out. Thanks!

Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-lambda Related to AWS Lambda bug This issue is a bug. p2 response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.
Projects
None yet
Development

No branches or pull requests

2 participants