aws-sam-cli icon indicating copy to clipboard operation
aws-sam-cli copied to clipboard

Bug: Build for arm64 from x86 machine

Open DanielRasho opened this issue 8 months ago • 2 comments

Description:

As the title suggests, I'm trying to build AWS Lambda images for AWS Graviton 2, which uses an ARM64 architecture, from my x86 machine, but I've been unsuccessful so far. Every time I try sam build, I encounter the following message:

image with reference sha256:9772b24dae18e816e254ba3dedb8d3c35370c1a5b26f0a4fe5e5d1fbf998953f was found but its platform (linux/amd64) does not match the specified platform (linux/arm64)

Im building using Go and DockerImages and following this guide I found. I encountered a similar problem in the past, when using plain docker, and as this Stackoverflow thread suggests, multiplatform/builds may be the solution but I haven't find such feature on AWS SAM documentation. Correct me if I'm mistaken.

Steps to reproduce:

This are my build files so far :

Dockerimage

FROM public.ecr.aws/docker/library/golang:1.21 as build-image
ARG ENTRY_POINT
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN GOARCH=arm64 GOOS=linux go build -tags lambda.norpc -o lambda-handler ${ENTRY_POINT}
FROM public.ecr.aws/lambda/provided:al2023
COPY --from=build-image /src/lambda-handler .
ENTRYPOINT ./lambda-handler

Lambdas general configuration*

  Function:
    Timeout: 10
    MemorySize: 384
    Environment:
      Variables:
        ASSETS_FOLDER: !Ref AssetsFolder
        DB_URI: !Ref MongoURI
        DB_NAME: !Ref DBName
        RUNNING_MODE: !Ref RunningMode
        LOGGING_LEVEL: !Ref LambdaLoggingLevel
        LOGGING_PRETTY: !Ref LambdaPrettyLogs
    Architectures:
      - !Ref LambdaArchitecture
    Tags:
      Project: Bitacora
    LoggingConfig: 
      LogFormat: JSON
      LogGroup: !Ref LambdasLogGroup
      ApplicationLogLevel: DEBUG
    Tracing: Active

Specific function configuration*

  CheckHealthFunction:
    Type: AWS::Serverless::Function
    Properties:
      PackageType: Image
      Events:
        CatchAll:
          Type: Api
          Properties:
            Path: /health
            Method: GET
            RestApiId: !Ref BitacoraAPI

Observed result:

Error: failed to get destination image "sha256:33125dcab31437e4a17aba5109c3bc9780410874b58075d477fd23104bd947ea": image with reference sha256:33125dcab31437e4a17aba5109c3bc9780410874b58075d477fd23104bd947ea was found but its platform (linux/amd64) does not match the specified platform (linux/arm64)

Expected result:

Building without problems for arm64 architectures

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: Manjaro Linux
  2. sam --version: SAM CLI, version 1.132.0
  3. AWS region: us-east-1
{
  "version": "1.132.0",
  "system": {
    "python": "3.12.8",
    "os": "Linux-6.12.20-2-MANJARO-x86_64-with-glibc2.40"
  },
  "additional_dependencies": {
    "docker_engine": "28.0.4",
    "aws_cdk": "Not available",
    "terraform": "Not available"
  },
  "available_beta_feature_env_vars": [
    "SAM_CLI_BETA_FEATURES",
    "SAM_CLI_BETA_BUILD_PERFORMANCE",
    "SAM_CLI_BETA_TERRAFORM_SUPPORT",
    "SAM_CLI_BETA_RUST_CARGO_LAMBDA"
  ]
}

Add --debug flag to command you are running

DanielRasho avatar Apr 06 '25 04:04 DanielRasho

Thank you for reporting the issue. sam build depends on aws-lambda-builder for the brain behind building processes. Please feel free to raise an issue in that package as well. We will look into the issue accordingly.

vicheey avatar May 09 '25 23:05 vicheey

Actually the functions of type Image are built directly by SAM CLI using docker and they don't use aws-lambda-builders. So in this case, the problem might be that the build process is being triggered mentioning the other architecture, but not completely enforcing it.

There are two things onto this:

  1. You need to make sure that you have activated the correct support to build and run Docker images of a different architecture (so you can correctly build/run the arm64 Docker image. On the SAM CLI docs, this is is mentioned on the page that mentions how to install Docker ("Linux" section) and it's:
    docker run --rm --privileged multiarch/qemu-user-static --reset -p yes.
    
  2. For some reason it might be happening that sam build is not passing the platform correctly to docker build, even though it is supposed to be passed as part of the code. We might have to investigate more why this error is being thrown. If you run the previous command, maybe you can try to be explicit on asking for the arm64 version of the images if possible (like FROM public.ecr.aws/lambda/provided:al2023-arm64).

Another alternative is to build your function just using Docker docker build --platform 'linux/arm64' -t hello-world-sam:latest, and update your template to point to that already built image instead of building it with SAM CLI:

  CheckHealthFunction:
    Type: AWS::Serverless::Function
    Properties:
      PackageType: Image
      ImageUri: hello-world-sam:latest
      ...

valerena avatar May 27 '25 23:05 valerena

I'm not the original creator of this issue but I'm running into basically the error and hoping to keep this issue moving. The main difference is that I'm on an ARM based computer (Mac M4) and trying to build an x86-based image instead of the opposite (x86 building an ARM image).

{
  "version": "1.138.0",
  "system": {
    "python": "3.13.3",
    "os": "macOS-15.5-arm64-arm-64bit-Mach-O"
  },
  "additional_dependencies": {
    "docker_engine": "28.3.0",
    "aws_cdk": "Not available",
    "terraform": "Not available"
  },
  "available_beta_feature_env_vars": [
    "SAM_CLI_BETA_FEATURES",
    "SAM_CLI_BETA_BUILD_PERFORMANCE",
    "SAM_CLI_BETA_TERRAFORM_SUPPORT",
    "SAM_CLI_BETA_PACKAGE_PERFORMANCE",
    "SAM_CLI_BETA_RUST_CARGO_LAMBDA"
  ]
}

I've also confirmed Rosetta 2 is installed and working

> arch -x86_64 bash -c "echo test"
test

Here's my template definition:

...
Resources:
  DOCToPDFFunction:
    Type: AWS::Serverless::Function
    Properties:
      Architectures:
        - x86_64
      FunctionName: doc_to_pdf
      Description: Converts doc files to pdf
      PackageType: Image
      Timeout: 180
      MemorySize: 3008 # Per https://www.npmjs.com/package/@shelf/aws-lambda-libreoffice/v/7.3.0
      Policies: AWSLambdaExecute
      CodeUri: ./functions/doc-to-pdf/
    Metadata:
      DockerContext: ./functions/doc-to-pdf/
      Dockerfile: Dockerfile
...

If I try to run the docker build directly, it succeeds: docker buildx build --platform linux/amd64 --load -t client-portal-doc-to-pdf .

Yet running through sam build fails with error (full debug log is later in this comment)

failed to export image: no match for platform in manifest sha256:b98866070951d2d32a1dde2b4be7c95fbeb94f3c1b20388d8f5e60fbe05273b8: not found

Build Failed

Regarding the above suggestions from @valerena ,

docker run --rm --privileged multiarch/qemu-user-static --reset -p yes

This is failing on my machine:

Unable to find image 'multiarch/qemu-user-static:latest' locally
latest: Pulling from multiarch/qemu-user-static
205dae5015e7: Download complete
30c9c93f40b9: Pulling fs layer
816739e52091: Pulling fs layer
30abb83a18eb: Pulling fs layer
0657daef200b: Pulling fs layer
docker: failed to extract layer (application/vnd.docker.image.rootfs.diff.tar.gzip sha256:205dae5015e78dd8c4d302e3db4eb31576fac715b46d099fe09680ba28093a7a) to overlayfs as "extract-355570003-xjj0 sha256:b64792c17e4ad443d16b218afb3a8f5d03ca0f4ec49b11c1a7aebe17f6c3c1d2": failed to get stream processor for application/vnd.docker.image.rootfs.diff.tar.gzip: fork/exec /usr/bin/unpigz: exec format error

Though I'm seeing online that this shouldn't be needed on Mac running Docker Desktop anyway?

And the other suggestion

Another alternative is to build your function just using Docker docker build --platform 'linux/arm64' -t hello-world-sam:latest, and update your template to point to that already built image instead of building it with SAM CLI:

I'm not sure exactly how the template should look after adding this field, but only adding the ImageUri field under Properties and setting to client-portal-doc-to-pdf:latest (which I successfully built using the command above) then running sam build gives the same error

failed to export image: no match for platform in manifest sha256:b98866070951d2d32a1dde2b4be7c95fbeb94f3c1b20388d8f5e60fbe05273b8: not found

Build Failed

If I add ImageUri to Properties, remove DockerContext and Dockerfile from Metadata, and then run sam build, it "succeeds" but doesn't seem to have actually built anything, and attempting to run the Lambda after using sam local invoke "DOCToPDFFunction" gives me

> sam local invoke "DOCToPDFFunction"
Invoking Container created from client-portal-doc-to-pdf:latest
Local image was not found.
Removing rapid images for repo client-portal-doc-to-pdf
Building image.........
Failed to build Docker Image
NoneType: None
Error: Error building docker image: NotFound: parent snapshot sha256:aa82110d512563bb761d08e583d3b43957ae54c2d7ef51382c1b66facbbeab30 does not exist: not found

Not sure why it's not finding the local image since I can see it in my Docker Desktop

Image

Here's the full debug log of me running sam build off of my original lambda template (The one fully included earlier in this comment)

> sam build --debug
2025-07-10 15:57:49,218 | Config file location: /Users/aidan/client-portal/serverless/filePreviewThumbnail/samconfig.toml
2025-07-10 15:57:49,219 | Loading configuration values from [default.['build'].parameters] (env.command_name.section) in config file at
'/Users/aidan/client-portal/serverless/filePreviewThumbnail/samconfig.toml'...
2025-07-10 15:57:49,220 | Configuration values successfully loaded.
2025-07-10 15:57:49,220 | Configuration values are: {}
2025-07-10 15:57:49,222 | Using SAM Template at /Users/aidan/client-portal/serverless/filePreviewThumbnail/template.yaml
2025-07-10 15:57:49,232 | Using config file: samconfig.toml, config environment: default
2025-07-10 15:57:49,232 | Expand command line arguments to:
2025-07-10 15:57:49,233 | --template_file=/Users/aidan/client-portal/serverless/filePreviewThumbnail/template.yaml --mount_with=READ --build_dir=.aws-sam/build --cache_dir=.aws-sam/cache
2025-07-10 15:57:49,248 | 'build' command is called
2025-07-10 15:57:49,251 | No Parameters detected in the template
2025-07-10 15:57:49,257 | There is no customer defined id or cdk path defined for resource CreateThumbnailStateMachine, so we will use the resource logical id as the resource id
2025-07-10 15:57:49,258 | There is no customer defined id or cdk path defined for resource DOCToPDFFunction, so we will use the resource logical id as the resource id
2025-07-10 15:57:49,258 | 0 stacks found in the template
2025-07-10 15:57:49,258 | No Parameters detected in the template
2025-07-10 15:57:49,264 | There is no customer defined id or cdk path defined for resource CreateThumbnailStateMachine, so we will use the resource logical id as the resource id
2025-07-10 15:57:49,264 | There is no customer defined id or cdk path defined for resource DOCToPDFFunction, so we will use the resource logical id as the resource id
2025-07-10 15:57:49,264 | 2 resources found in the stack
2025-07-10 15:57:49,264 | Found Serverless function with name='DOCToPDFFunction' and ImageUri='None'
2025-07-10 15:57:49,265 | --base-dir is not presented, adjusting uri ./functions/doc-to-pdf/ relative to /Users/aidan/client-portal/serverless/filePreviewThumbnail/template.yaml
2025-07-10 15:57:49,265 | --base-dir is not presented, adjusting uri . relative to /Users/aidan/client-portal/serverless/filePreviewThumbnail/template.yaml
2025-07-10 15:57:49,267 | 2 resources found in the stack
2025-07-10 15:57:49,267 | Found Serverless function with name='DOCToPDFFunction' and ImageUri='None'
2025-07-10 15:57:49,268 | Error occurred while trying to track an event: Event 'BuildFunctionRuntime' does not accept value 'None'.
2025-07-10 15:57:49,268 | Instantiating build definitions
2025-07-10 15:57:49,269 | Unique function build definition found, adding as new (Function Build Definition: BuildDefinition(None, /Users/aidan/client-portal/serverless/filePreviewThumbnail, Image, ,
8a02999b-b2b4-4477-aa44-d13b752332c8, {'DockerContext': '/Users/aidan/client-portal/serverless/filePreviewThumbnail/functions/doc-to-pdf', 'Dockerfile': 'Dockerfile'}, {}, x86_64, []), Function:
Function({'function_id': 'DOCToPDFFunction', 'name': 'DOCToPDFFunction', 'functionname': 'doc_to_pdf', 'runtime': None, 'memory': 3008, 'timeout': 180, 'handler': None, 'imageuri': None,
'packagetype': 'Image', 'imageconfig': None, 'codeuri': '/Users/aidan/client-portal/serverless/filePreviewThumbnail', 'environment': None, 'rolearn': None, 'layers': [], 'events': None, 'metadata':
{'DockerContext': '/Users/aidan/client-portal/serverless/filePreviewThumbnail/functions/doc-to-pdf', 'Dockerfile': 'Dockerfile', 'SamResourceId': 'DOCToPDFFunction'}, 'inlinecode': None,
'codesign_config_arn': None, 'architectures': ['x86_64'], 'function_url_config': None, 'function_build_info': <FunctionBuildInfo.BuildableImage: ('BuildableImage', 'Regular IMAGE function which can
be build with SAM CLI')>, 'stack_path': '', 'runtime_management_config': None, 'logging_config': None}))
2025-07-10 15:57:49,270 | Building codeuri: /Users/aidan/client-portal/serverless/filePreviewThumbnail runtime: None architecture: x86_64 functions: DOCToPDFFunction
2025-07-10 15:57:49,271 | Building to following folder /Users/aidan/client-portal/serverless/filePreviewThumbnail/.aws-sam/build/DOCToPDFFunction
2025-07-10 15:57:49,271 | Building image for DOCToPDFFunction function
2025-07-10 15:57:49,274 | Setting DockerBuildArgs for DOCToPDFFunction function
2025-07-10 15:57:50,245 | Failed building function DOCToPDFFunction
Step 1/5 : FROM public.ecr.aws/shelf/lambda-libreoffice-base:7.6-node20-x86_64
 ---> b98866070951
Step 2/5 : COPY app.js package.json /var/task/
failed to export image: no match for platform in manifest sha256:b98866070951d2d32a1dde2b4be7c95fbeb94f3c1b20388d8f5e60fbe05273b8: not found

Build Failed
2025-07-10 15:57:50,247 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2025-07-10 15:57:50,344 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2025-07-10 15:57:50,345 | Sending Telemetry: {'metrics': [{'commandRun': {'requestId': '59d079b2-72ff-4489-bdd5-5fbaa1f39edc', 'installationId': 'b14df382-f1d0-466b-8f31-0082da260c00', 'sessionId':
'559ca92e-fead-4635-beb4-23d4b87e2ac5', 'executionEnvironment': 'CLI', 'ci': False, 'pyversion': '3.13.3', 'samcliVersion': '1.138.0', 'awsProfileProvided': False, 'debugFlagProvided': True,
'region': '', 'commandName': 'sam build', 'metricSpecificAttributes': {'projectType': 'CFN', 'gitOrigin': None, 'projectName': '4abbcef8c113a2e14bbe1fc42c559978db24385b9c4a9d25c33d256c90cdc52f',
'initialCommit': None}, 'duration': 1015, 'exitReason': 'DockerBuildFailed', 'exitCode': 1}}]}
2025-07-10 15:57:50,345 | Unable to find Click Context for getting session_id.
2025-07-10 15:57:50,346 | Sending Telemetry: {'metrics': [{'events': {'requestId': '4612f9e6-52a8-4b33-ba17-590ffd6634b1', 'installationId': 'b14df382-f1d0-466b-8f31-0082da260c00', 'sessionId':
'559ca92e-fead-4635-beb4-23d4b87e2ac5', 'executionEnvironment': 'CLI', 'ci': False, 'pyversion': '3.13.3', 'samcliVersion': '1.138.0', 'commandName': 'sam build', 'metricSpecificAttributes':
{'events': [{'event_name': 'SamConfigFileExtension', 'event_value': '.toml', 'thread_id': '94de15b927bd4733bdb7fdb8bafd1fbc', 'time_stamp': '2025-07-10 19:57:49.218', 'exception_name': None},
{'event_name': 'SamConfigFileExtension', 'event_value': '.toml', 'thread_id': 'db01ebbaa24740db88c2c0909d9e48e4', 'time_stamp': '2025-07-10 19:57:49.232', 'exception_name': None}]}}}]}
2025-07-10 15:57:50,634 | Telemetry response: 200
2025-07-10 15:57:50,637 | Telemetry response: 200
Error: failed to export image: no match for platform in manifest sha256:b98866070951d2d32a1dde2b4be7c95fbeb94f3c1b20388d8f5e60fbe05273b8: not found

Hope this helps, happy to provide more information as needed

aidanscode avatar Jul 10 '25 20:07 aidanscode