aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code

Home Page:https://aws.amazon.com/cdk

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

aws-lambda-python-alpha: dependencies are skipped when using a custom bundler

trondhindenes opened this issue · comments

Describe the bug

I'm trying to use a custom docker image based on the SAM sam/build-python3 image to install a specific set of dependencies in my python-based lambda function. The default bundling functionality is far to coarse, so I need to use my own image.

My image ends with:

WORKDIR /var/dependencies
COPY pyproject* poetry*  ./
RUN \
  # Only install the lambda_function dependency group, skip the default "cdk" group
  poetry export --with lambda_function --without cdk --with-credentials --format requirements.txt --output requirements.txt &&\
  pip install -r requirements.txt -t .

CMD [ "python" ]

so, all dependencies are packaged in the folder /var/dependencies when the image build completes, which is also the current WORKDIR of the image.

However, there's no way for me to understand or control how bundling actually happens based on this image - which folders are picked up and used for constructing the uploaded zip file etc.

My cdk app snippet looks like this:

        log_shipper_function = python_alpha.PythonFunction(
            self, 'logShipperFunction',
            entry=log_shipper_function_folder,
            index='handler.py',
            runtime=aws_lambda.Runtime.PYTHON_3_12,
            log_retention=aws_logs.RetentionDays.FIVE_DAYS,
            bundling=python_alpha.BundlingOptions(
                image=cdk.DockerImage.from_build(
                    '.',
                    file='Dockerfile',
                    build_args={
                        'POETRY_HTTP_BASIC_PRIVATE_USERNAME': 'aws',
                        'POETRY_HTTP_BASIC_PRIVATE_PASSWORD': os.getenv('POETRY_HTTP_BASIC_PRIVATE_PASSWORD')
                    }
                )
            )
        )

The resulting function consists only of the source code files present in log_shipper_function_folder, not any dependencies in the build image.

My question is: How can I control/understand what folders are being picked up when building the zip?

Expected Behavior

a zip is built containing the source code files along with the dependencies in the image

Current Behavior

dependencies are skipped

Reproduction Steps

see above

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.127.0 (build 6c90efc)

Framework Version

No response

Node.js Version

20

OS

ubuntu 24

Language

Python

Language Version

Python 3.12

Other information

No response

Hey @trondhindenes , thanks for reaching out. this document about custom bundling in python CDK might be able to help explain how custom bundling happens. Please feel free to let me know if its helpful in resolving the issue

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.