aws-sam-cli: Bug: Unable to run sam local invoke on Apple Silicon
Description:
I am trying to setup a rust lambda function and to test, I am running sam local invoke. This fails with:
START RequestId: b2283bbf-ccb9-4863-ba4f-e81bc28e36ad Version: $LATEST
13 May 2024 21:35:39,195 [ERROR] (rapid) Init failed error=Runtime exited with error: signal: illegal instruction InvokeID=
13 May 2024 21:35:39,199 [ERROR] (rapid) Invoke failed error=Runtime exited with error: signal: illegal instruction InvokeID=26f73d6f-5680-4e27-9663-00bef9017d5e
13 May 2024 21:35:39,211 [ERROR] (rapid) Invoke DONE failed: Sandbox.Failure
The same command works fine on an Intel Mac.
Steps to reproduce:
On an Apple Silicon (e.g. M2) machine, setup a Rust lambda:
- Run
sam init - Select
AWS Quick Start Templates - Select
Hello World Example - Select
rust (provided.al2) - Select
Nfor the following options. - The app config will therefore be:
-----------------------
Generating application:
-----------------------
Name: sam-app
Runtime: rust (provided.al2)
Architectures: x86_64
Dependency Manager: cargo
Application Template: hello-world
Output Directory: .
Configuration file: sam-app/samconfig.toml
- Run
sam build<— this succeeds - Run
sam local invoke
Observed result:
2024-05-13 23:46:14,648 | Config file location: /Users/nataizya/Documents/Playground/Rust/SAM/sam-app/samconfig.toml
2024-05-13 23:46:14,651 | Loading configuration values from [default.['local', 'invoke'].parameters]
(env.command_name.section) in config file at '/Users/nataizya/Documents/Playground/Rust/SAM/sam-app/samconfig.toml'...
2024-05-13 23:46:14,652 | Configuration values successfully loaded.
2024-05-13 23:46:14,652 | Configuration values are: {'stack_name': 'sam-app'}
2024-05-13 23:46:14,656 | Using SAM Template at
/Users/nataizya/Documents/Playground/Rust/SAM/sam-app/.aws-sam/build/template.yaml
2024-05-13 23:46:14,702 | Using config file: samconfig.toml, config environment: default
2024-05-13 23:46:14,703 | Expand command line arguments to:
2024-05-13 23:46:14,704 | --template_file=/Users/nataizya/Documents/Playground/Rust/SAM/sam-app/.aws-sam/build/template.yaml
--no_event --layer_cache_basedir=/Users/nataizya/.aws-sam/layers-pkg --container_host=localhost
--container_host_interface=127.0.0.1
2024-05-13 23:46:14,705 | local invoke command is called
2024-05-13 23:46:14,708 | No Parameters detected in the template
2024-05-13 23:46:14,727 | Sam customer defined id is more priority than other IDs. Customer defined id for resource
HelloWorldFunction is HelloWorldFunction
2024-05-13 23:46:14,728 | There is no customer defined id or cdk path defined for resource ServerlessRestApi, so we will use
the resource logical id as the resource id
2024-05-13 23:46:14,729 | 0 stacks found in the template
2024-05-13 23:46:14,730 | No Parameters detected in the template
2024-05-13 23:46:14,746 | Sam customer defined id is more priority than other IDs. Customer defined id for resource
HelloWorldFunction is HelloWorldFunction
2024-05-13 23:46:14,746 | There is no customer defined id or cdk path defined for resource ServerlessRestApi, so we will use
the resource logical id as the resource id
2024-05-13 23:46:14,747 | 2 resources found in the stack
2024-05-13 23:46:14,748 | Found Serverless function with name='HelloWorldFunction' and CodeUri='HelloWorldFunction'
2024-05-13 23:46:14,748 | --base-dir is not presented, adjusting uri HelloWorldFunction relative to
/Users/nataizya/Documents/Playground/Rust/SAM/sam-app/.aws-sam/build/template.yaml
2024-05-13 23:46:14,779 | Found one Lambda function with name 'HelloWorldFunction'
2024-05-13 23:46:14,779 | Invoking bootstrap (provided.al2)
2024-05-13 23:46:14,780 | No environment variables found for function 'HelloWorldFunction'
2024-05-13 23:46:14,780 | Loading AWS credentials from session with profile 'None'
2024-05-13 23:46:14,788 | Resolving code path. Cwd=/Users/nataizya/Documents/Playground/Rust/SAM/sam-app/.aws-sam/build,
CodeUri=/Users/nataizya/Documents/Playground/Rust/SAM/sam-app/.aws-sam/build/HelloWorldFunction
2024-05-13 23:46:14,789 | Resolved absolute path to code is
/Users/nataizya/Documents/Playground/Rust/SAM/sam-app/.aws-sam/build/HelloWorldFunction
2024-05-13 23:46:14,790 | Resolving code path. Cwd=/Users/nataizya/Documents/Playground/Rust/SAM/sam-app/.aws-sam/build,
CodeUri=/Users/nataizya/Documents/Playground/Rust/SAM/sam-app/.aws-sam/build/HelloWorldFunction
2024-05-13 23:46:14,790 | Resolved real code path to
/Users/nataizya/Documents/Playground/Rust/SAM/sam-app/.aws-sam/build/HelloWorldFunction
2024-05-13 23:46:14,791 | Code /Users/nataizya/Documents/Playground/Rust/SAM/sam-app/.aws-sam/build/HelloWorldFunction is not
a zip/jar file
2024-05-13 23:46:20,664 | Local image is up-to-date
2024-05-13 23:46:20,685 | Checking free port on 127.0.0.1:5726
2024-05-13 23:46:20,695 | Using local image: public.ecr.aws/lambda/provided:al2-rapid-x86_64.
2024-05-13 23:46:20,697 | Mounting /Users/nataizya/Documents/Playground/Rust/SAM/sam-app/.aws-sam/build/HelloWorldFunction as
/var/task:ro,delegated, inside runtime container
2024-05-13 23:46:20,949 | Starting a timer for 3 seconds for function 'HelloWorldFunction'
2024-05-13 23:46:20,953 | Getting lock for the key localhost-5726
2024-05-13 23:46:20,955 | Waiting to retrieve the lock (localhost-5726) to start invocation
2024-05-13 23:46:21,068 | Getting lock for the key localhost-5726
2024-05-13 23:46:21,072 | Waiting to retrieve the lock (localhost-5726) to start invocation
START RequestId: 2d02aba6-328d-4a67-b94f-91b4058b407e Version: $LATEST
13 May 2024 21:46:21,114 [ERROR] (rapid) Init failed error=Runtime exited with error: signal: illegal instruction InvokeID=
13 May 2024 21:46:21,118 [ERROR] (rapid) Invoke failed error=Runtime exited with error: signal: illegal instruction InvokeID=3bde647e-a338-4ff7-b860-7e24e6d8ff10
13 May 2024 21:46:21,120 [ERROR] (rapid) Invoke DONE failed: Sandbox.Failure
2024-05-13 23:46:21,122 | Failed to deserialize response from RIE, returning the raw response as is
2024-05-13 23:46:22,157 | [Container state] OOMKilled False
2024-05-13 23:46:22,288 | Cleaning all decompressed code dirs
2024-05-13 23:46:22,291 | Telemetry endpoint configured to be
https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2024-05-13 23:46:22,404 | Telemetry endpoint configured to be
https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2024-05-13 23:46:22,406 | Sending Telemetry: {'metrics': [{'commandRun': {'requestId': '88d70caf-438d-43c5-bdb9-cd8b910a8459',
'installationId': '200cdfea-e657-47bc-954f-69c7ded8a763', 'sessionId': 'eab61e43-2617-4a4b-8b64-239884235d30',
'executionEnvironment': 'CLI', 'ci': False, 'pyversion': '3.8.13', 'samcliVersion': '1.116.0', 'awsProfileProvided': False,
'debugFlagProvided': True, 'region': '', 'commandName': 'sam local invoke', 'metricSpecificAttributes': {'projectType': 'CFN',
'gitOrigin': None, 'projectName': '593ab2ca51e925b9f6c2f258bc55ed5926cf6d2c78239a685d65907e4ec7edd3', 'initialCommit': None},
'duration': 7588, 'exitReason': 'success', 'exitCode': 0}}]}
2024-05-13 23:46:22,407 | Unable to find Click Context for getting session_id.
2024-05-13 23:46:22,411 | Sending Telemetry: {'metrics': [{'events': {'requestId': 'a5e10d43-7cbc-43b6-be11-7dabe9d0a741',
'installationId': '200cdfea-e657-47bc-954f-69c7ded8a763', 'sessionId': 'eab61e43-2617-4a4b-8b64-239884235d30',
'executionEnvironment': 'CLI', 'ci': False, 'pyversion': '3.8.13', 'samcliVersion': '1.116.0', 'commandName': 'sam local
invoke', 'metricSpecificAttributes': {'events': [{'event_name': 'SamConfigFileExtension', 'event_value': '.toml', 'thread_id':
'ba1472d1be8a4258bce10fcb3747afbc', 'time_stamp': '2024-05-13 21:46:14.647', 'exception_name': None}, {'event_name':
'SamConfigFileExtension', 'event_value': '.toml', 'thread_id': '4d7d2b65326940d7aebc4265646dfc81', 'time_stamp': '2024-05-13
21:46:14.702', 'exception_name': None}]}}}]}
2024-05-13 23:46:23,369 | HTTPSConnectionPool(host='aws-serverless-tools-telemetry.us-west-2.amazonaws.com', port=443): Read
timed out. (read timeout=0.1)
2024-05-13 23:46:23,375 | HTTPSConnectionPool(host='aws-serverless-tools-telemetry.us-west-2.amazonaws.com', port=443): Read
timed out. (read timeout=0.1)
2024-05-13 23:46:23,379 | Telemetry endpoint configured to be
https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2024-05-13 23:46:23,381 | Sending Telemetry: {'metrics': [{'runtimeMetric': {'requestId':
'a53adc53-679e-478a-9032-a01c08bee7d2', 'installationId': '200cdfea-e657-47bc-954f-69c7ded8a763', 'sessionId':
'eab61e43-2617-4a4b-8b64-239884235d30', 'executionEnvironment': 'CLI', 'ci': False, 'pyversion': '3.8.13', 'samcliVersion':
'1.116.0', 'runtimes': ['provided.al2']}}]}
2024-05-13 23:46:24,182 | HTTPSConnectionPool(host='aws-serverless-tools-telemetry.us-west-2.amazonaws.com', port=443): Read
timed out. (read timeout=0.1)
Expected result:
Successfully run the sam local invoke command and run the lambda function.
Additional environment details (Ex: Windows, Mac, Amazon Linux etc)
- OS:
MacOS 14.4.1 (23E224) sam --version:SAM CLI, version 1.116.0- AWS region: n/a
{
"version": "1.116.0",
"system": {
"python": "3.8.13",
"os": "macOS-14.4.1-x86_64-i386-64bit"
},
"additional_dependencies": {
"docker_engine": "24.0.7",
"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
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 1
- Comments: 17 (9 by maintainers)
I think we found the root cause (thanks to @lucashuy).
There was a change in
cargo-lambdapackage which is used for building Rust functions under the hood. This PR changed the default target cpu (microarch) to behaswell. It looks like this is causing issues when executable built with that flag. If it is changed tox86_64then it works fine.However the reason for the change above was to make it more optimized for the target lambda runtime. So this code works when it is deployed (as intended) however when it is tried to be emulated on Apple Silicon machines, it doesn’t work (possible) missing support from Docker Desktop.
We don’t want to change default target since this was selected due to optimizations, for now we would ask customers to use workaround to build with
SAM_BUILD_MODE=debug sam buildwhen testing their lambda functions locally.Thanks all for working with us on this issue. Just to sum up for future folks popping into this issue: SAM CLI builds Rust Functions for “release mode”, meaning that specific architecture level optimizations are applied when building the binary. Unfortunately, these optimizations do not work when invoking them in an x86 container, on an arm64 Mac.
Workaround
Before building the Lambda function, set the
SAM_BUILD_MODE=debugenvironment variable before runningsam build, like such:SAM_BUILD_MODE=debug sam buildPlease note that this is only recommended when building and testing locally. We advise customers to switch back to a release build by unsetting or removing the
SAM_BUILD_MODEenviron variable.Whats next
Our team is going to prioritize adding a disclaimer message saying the essentially the same thing inside of
sam local.Local invocation works for me after change to
Architectures: - arm64. I had to remove all the containers before run the invoke command.