(aws-lambda.Function): Platform configuration in BundlingOptions is not respected #30748
Labels
@aws-cdk/aws-lambda
Related to AWS Lambda
bug
This issue is a bug.
closed-for-staleness
This issue was automatically closed because it hadn't received any attention in a while.
response-requested
Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.
Describe the bug
In TypeScript, I have a CDK Pipeline (
aws-cdk-lib/pipelines/CodePipeline
) with a CodeBuild synth step runningaws/codebuild/standard:7.0
. This pipeline bundles a Python Function (aws-cdk-lib/aws-lambda/Function
) targeting an ARM64 architecture.The Python code and its dependencies are installed and bundled with the following configuration:
My goal is for the
cdk synth
command to bundle the Python Lambda code assets using the Docker image that reflects myimage
andplatform
configurations specified in theBundlingOptions
withincode.bundling
.When I run
cdk synth
on my local machine (M1 / aarch64), the synthesizing process pulls thepublic.ecr.aws/sam/build-python3.12:latest
Docker image and bundles without any platform specification. The pip install logs show that packages are installed for my local architecture:For debugging purposes, I print out the architecture name of the Docker image using the
uname -m
command.However, when I run the same process through the CDK pipeline, the results differ:
In this case, the same
public.ecr.aws/sam/build-python3.12:latest
Docker image is pulled, but during installation, x86_64 specific pip wheels are used instead of the target ARM_64 architecture configured inBundlingOptions
.Although this does not cause immediate synthesizing or deployment failures, it leads to hidden issues that only surface during runtime. For example, when attempting to import
langchain_community.llms.Bedrock
in the deployed Python Lambda function, I encountered the following error:Expected Behavior
I expect that the
BundlingOptions
image and platform parameters are respected and enforced when bundling Lambda code assets, regardless of the platform it is executed on.Specifically, I expect that CodeBuild (running on x86_64) would pull the ARM_64 specific image
public.ecr.aws/sam/build-python3.12:latest-arm64
or build specifically for the target architecture.When running
cdk synth
locally on an M1 / aarch64 machine, I expect it to use the specified image and platform configuration. For instance, if I configure:and run
cdk synth
on my local M1 / aarch64 machine, I expect CDK to bundle usingpublic.ecr.aws/sam/build-python3.12:latest-x86_64
or with thedocker
command with the--platform
flag. Current evidence suggests this is not the case.Current Behavior
Currently, this is not the case. Regardless of the platform configured in
BundlingOptions.platform
, it only uses the genericpublic.ecr.aws/sam/build-python3.12:latest
Docker image and matches the machine's architecture. This causes downstream problems, as highlighted above.Reproduction Steps
This issue seems to be related to the fundamental
BundlingOptions
itself rather than CodeBuild or other services mentioned in this ticket.Step 1: Implement the following Lambda function in a simple TypeScript CDK app:
Step 2: Execute locally
cdk synth
to debug which platform is used.Step 3: Change the
lambdaArchitecture
constant tolambda.Architecture.X86_64
.Step 4: Execute locally
cdk synth
again and observe if the platform has changed.Possible Solution
There are two potential workarounds for this issue:
1/ Resolve the issue at CodeBuild:
Given that I can run
cdk synth
locally without a problem and directly deploy usingcdk deploy
(because my local architecture matches the target architecture, ARM_64), I can solve this problem by switching the CodeBuild runtime image to be compatible with ARM_64.Note that here I use the
aws/codebuild/amazonlinux2-aarch64-standard:3.0
image. However, this solution might not work well for a team of developers working on different platforms, as local bundling could create faulty Lambda code assets to deploy.2/ Configure architecture-specific bundling image:
In this case, I can configure the Docker image name based on the runtime and architecture configurations:
Then, pull it from the registry:
All the pieces together:
This, however, can cause other issues as highlighted in this Github issue:
Additional Information/Context
No response
CDK CLI Version
2.147.1
Framework Version
No response
Node.js Version
v20.13.1
OS
Mac M1
Language
TypeScript, Python
Language Version
Typescript (5.4.5) | Python (3.12)
Other information
This issue exhibits similar symptoms to those described in AWS CDK Issue #18696.
The text was updated successfully, but these errors were encountered: