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

lambda: default architecture X86_64 isn't used for cross-platform docker builds #31048

Closed
tabrezm opened this issue Aug 7, 2024 · 7 comments · Fixed by #31053
Closed

lambda: default architecture X86_64 isn't used for cross-platform docker builds #31048

tabrezm opened this issue Aug 7, 2024 · 7 comments · Fixed by #31053
Labels
@aws-cdk/aws-lambda Related to AWS Lambda bug This issue is a bug. effort/small Small work item – less than a day of effort p2

Comments

@tabrezm
Copy link

tabrezm commented Aug 7, 2024

Describe the bug

The current documented behavior for the architecture prop on lambda_.DockerImageFunction is:

architecture: The system architectures compatible with this lambda function. Default: Architecture.X86_64

However, I found running cdk deploy on my Mac would not target amd64. This results in the following error message when invoking the deployed Lambda function:

Error: fork/exec /lambda-entrypoint.sh: exec format error Runtime.InvalidEntrypoint

I was able to resolve this by explicitly setting:

lambda_.DockerImageFunction(self, ..., architecture=lambda_.Architecture.X86_64)

Another quirk I found was that specifying --platform in build_args is silently ignored. You can only specify this setting via the platform property.

code=lambda_.DockerImageCode.from_image_asset(
    directory=...,
    build_args={"--platform": "linux/amd64"},  # doesn't work
    platform=ecr_assets.Platform.LINUX_X86_64,  # works
),

Expected Behavior

  1. The lambda_.DockerImageFunction() default architecture of lambda_.Architecture.X86_64 is passed to docker build
  2. Allow passing --platform in build_args or raise an error

Current Behavior

The default value either seems to not be set correctly, or it's not passed correctly to docker build.

Reproduction Steps

Follow the hello world example here.

lambda_.DockerImageFunction(
            self,
            "HelloWorldLambda",
            code=lambda_.DockerImageCode.from_image_asset(
                directory=...,
                # build_args={"--platform": "linux/amd64"},
                # platform=ecr_assets.DockerPlatform.LINUX_X86_64,
            ),
            # architecture=lambda_.Architecture.X86_64,
        )

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.151.0 (build b8289e2)

Framework Version

No response

Node.js Version

v20.16.0

OS

macOS

Language

Python

Language Version

No response

Other information

No response

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

pahud commented Aug 7, 2024

Looks like you are trying to build a lambda docker-compatible function of X86_64 architecture from a ARM64 Mac laptop.

The architecture of DockerImageFunction is used for:

  1. pass correct architecture for the L1 CfnFunction props, which defaults to X86_64. If your target lambda arch is X86_64, it's fine to leave it undefined.
  2. pass additional --platform argument for docker build if specified, especially for cross-platform building(per this PR). Looks like this would be required for cross-platform building.

Can you just specify architecture for DockerImageFunction and let me know if it works for you? I guess we probably need to improve the doc with another PR.

@pahud pahud added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. p2 effort/small Small work item – less than a day of effort and removed needs-triage This issue or PR still needs to be triaged. labels Aug 7, 2024
@tabrezm
Copy link
Author

tabrezm commented Aug 7, 2024

Thanks for your reply.

Can you just specify architecture for DockerImageFunction and let me know if it works for you? I guess we probably need to improve the doc with another PR.

Yes, I confirmed this works here (I didn't have any props on from_image_asset specified at the time):

I was able to resolve this by explicitly setting:

lambda_.DockerImageFunction(self, ..., architecture=lambda_.Architecture.X86_64)

Is that what you were asking about?

@pahud
Copy link
Contributor

pahud commented Aug 7, 2024

Yes in your case as I presumed architecture would be the only required option. You don't need to specify --platform because behind the scene according to this PR, it would pass the platform argument all the way to docker build.

I'm glad this works for you. I will cut a tiny PR to improve the doc.

Did I miss anything else?

@tabrezm
Copy link
Author

tabrezm commented Aug 7, 2024

Yes in your case as I presumed architecture would be the only required option. You don't need to specify --platform because behind the scene according to this PR, it would pass the platform argument all the way to docker build.

I'm glad this works for you. I will cut a tiny PR to improve the doc.

Did I miss anything else?

It's more of a nit, but having better fail-fast validation here would've be helpful since there are a few AWS samples still out there that have build_args={"--platform": "linux/amd64"} which seem to be superseded with the newer(?) explicit platform prop:

  1. Allow passing --platform in build_args or raise an error

@pahud
Copy link
Contributor

pahud commented Aug 7, 2024

Yes we need to address them with clearer doc. For this issue, I will submit a PR shortly. If you see other similar issues that deserve more clarifying or even a code fix, please raise a new issue for us. Thank you.

@pahud pahud removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Aug 7, 2024
@pahud pahud changed the title aws_lambda: default architecture X86_64 isn't used for docker builds lambda: default architecture X86_64 isn't used for cross-platform docker builds Aug 7, 2024
@mergify mergify bot closed this as completed in #31053 Aug 28, 2024
@mergify mergify bot closed this as completed in 2a3279d Aug 28, 2024
Copy link

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

1 similar comment
Copy link

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 28, 2024
xazhao pushed a commit to xazhao/aws-cdk that referenced this issue Sep 12, 2024
… bundling (aws#31053)

### Issue # (if applicable)

Closes aws#31048

### Reason for this change

Per discussed aws#31048 (comment), the `architecture` would still be required when bundling `X86_64` DockerImageFunction from a `arm64` machine like a Macbook with arm64 chip as it explicitly pass the `--platform` argument to `docker build`, which is required in this use case.


### Description of changes



### Description of how you validated changes



### Checklist
- [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
@aws-cdk/aws-lambda Related to AWS Lambda bug This issue is a bug. effort/small Small work item – less than a day of effort p2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants