aws / aws-sam-cli

CLI tool to build, test, debug, and deploy Serverless applications using AWS SAM
https://aws.amazon.com/serverless/sam/
Apache License 2.0
6.51k stars 1.17k forks source link

Bug: sam local invoke - KeyError #7585

Open schambers-slate opened 2 weeks ago

schambers-slate commented 2 weeks ago

Description:

Running any sam local commands results in this error. Randomly started happening yesterday and I'm unable to run sam local

I'm unable to run any sam commands locally included sam local start-api, sam local invoke, etc. All cli commands with sam local result in same output below.

It seems as if there's some local cache thats tripping up sam cli but I'm unable to find this cache if it exists

Steps to reproduce:

Create a new project via sam init run sam local start-api

Observed result:

~/projects/redacted » sam local invoke sampleLambda --debug                                                                                                                                                                        redacted@Slate-MBP
2024-10-19 09:50:10,661 | Config file location: /Users/redacted/projects/redacted/samconfig.toml
2024-10-19 09:50:10,662 | Loading configuration values from [default.['local', 'invoke'].parameters] (env.command_name.section) in config file at '/Users/redacted/projects/redacted/samconfig.toml'...
2024-10-19 09:50:10,663 | Configuration values successfully loaded.
2024-10-19 09:50:10,663 | Configuration values are: {'stack_name': 'redacted'}
2024-10-19 09:50:10,666 | Using SAM Template at /Users/redacted/projects/redacted/template.yaml
2024-10-19 09:50:10,681 | Using config file: samconfig.toml, config environment: default
2024-10-19 09:50:10,682 | Expand command line arguments to:
2024-10-19 09:50:10,682 | --template_file=/Users/redacted/projects/redacted/template.yaml --function_logical_id=sampleLambda --no_event --layer_cache_basedir=/Users/redacted/.aws-sam/layers-pkg --container_host=localhost
--container_host_interface=127.0.0.1
2024-10-19 09:50:10,683 | local invoke command is called
2024-10-19 09:50:10,726 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2024-10-19 09:50:10,754 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2024-10-19 09:50:10,755 | Unable to find Click Context for getting session_id.
2024-10-19 09:50:10,755 | Sending Telemetry: {'metrics': [{'commandRun': {'requestId': 'e9ebed45-0a06-406e-9de7-81fe11c607e7', 'installationId': '45bb19e2-1959-43a3-ac1b-6b7fdfb3b4b3', 'sessionId': '382e9533-3a04-4cf1-9501-cb19a0412246',
'executionEnvironment': 'CLI', 'ci': False, 'pyversion': '3.8.20', 'samcliVersion': '1.126.0', 'awsProfileProvided': False, 'debugFlagProvided': True, 'region': '', 'commandName': 'sam local invoke', 'metricSpecificAttributes': {'projectType': 'CFN',
'gitOrigin': None, 'projectName': 'f41f7aaec721fe0c0f56413e88516cbc49d429765405cb56abc41d41c2d20b3f', 'initialCommit': None}, 'duration': 44, 'exitReason': 'KeyError', 'exitCode': 255}}]}
2024-10-19 09:50:10,757 | Sending Telemetry: {'metrics': [{'events': {'requestId': '16e4e564-5554-4d87-a16e-19f6a83ebeeb', 'installationId': '45bb19e2-1959-43a3-ac1b-6b7fdfb3b4b3', 'sessionId': '382e9533-3a04-4cf1-9501-cb19a0412246',
'executionEnvironment': 'CLI', 'ci': False, 'pyversion': '3.8.20', 'samcliVersion': '1.126.0', 'commandName': 'sam local invoke', 'metricSpecificAttributes': {'events': [{'event_name': 'SamConfigFileExtension', 'event_value': '.toml', 'thread_id':
'bf3954e50f004a339017df8cc10e91da', 'time_stamp': '2024-10-19 14:50:10.660', 'exception_name': None}, {'event_name': 'SamConfigFileExtension', 'event_value': '.toml', 'thread_id': '6010d1ad437f44f599ccaf1dcc0a5bf4', 'time_stamp': '2024-10-19
14:50:10.681', 'exception_name': None}]}}}]}
2024-10-19 09:50:11,253 | HTTPSConnectionPool(host='aws-serverless-tools-telemetry.us-west-2.amazonaws.com', port=443): Read timed out. (read timeout=0.1)
2024-10-19 09:50:11,254 | HTTPSConnectionPool(host='aws-serverless-tools-telemetry.us-west-2.amazonaws.com', port=443): Read timed out. (read timeout=0.1)

Error: '57c56afcdd34eee861d78048a9651ea78ae14eae'
Traceback:
  File "click/core.py", line 1078, in main
  File "click/core.py", line 1688, in invoke
  File "click/core.py", line 1688, in invoke
  File "click/core.py", line 1434, in invoke
  File "click/core.py", line 783, in invoke
  File "samcli/cli/cli_config_file.py", line 347, in wrapper
  File "click/decorators.py", line 92, in new_func
  File "click/core.py", line 783, in invoke
  File "samcli/lib/telemetry/metric.py", line 185, in wrapped
  File "samcli/lib/telemetry/metric.py", line 150, in wrapped
  File "samcli/lib/utils/version_checker.py", line 43, in wrapped
  File "samcli/cli/main.py", line 95, in wrapper
  File "samcli/commands/local/invoke/cli.py", line 108, in cli
  File "samcli/commands/local/invoke/cli.py", line 185, in do_cli
  File "samcli/commands/local/cli_common/invoke_context.py", line 184, in __init__
  File "samcli/commands/local/cli_common/invoke_context.py", line 363, in _add_account_id_to_global
  File "botocore/client.py", line 569, in _api_call
  File "botocore/client.py", line 1005, in _make_api_call
  File "botocore/client.py", line 1029, in _make_request
  File "botocore/endpoint.py", line 119, in make_request
  File "botocore/endpoint.py", line 196, in _send_request
  File "botocore/endpoint.py", line 132, in create_request
  File "botocore/hooks.py", line 412, in emit
  File "botocore/hooks.py", line 256, in emit
  File "botocore/hooks.py", line 239, in _emit
  File "botocore/signers.py", line 105, in handler
  File "botocore/signers.py", line 188, in sign
  File "botocore/signers.py", line 306, in get_auth_instance
  File "botocore/credentials.py", line 634, in get_frozen_credentials
  File "botocore/credentials.py", line 522, in _refresh
  File "botocore/credentials.py", line 538, in _protected_refresh
  File "botocore/credentials.py", line 685, in fetch_credentials
  File "botocore/credentials.py", line 695, in _get_cached_credentials
  File "botocore/credentials.py", line 2150, in _get_credentials
  File "botocore/tokens.py", line 82, in get_frozen_token
  File "botocore/tokens.py", line 95, in _refresh
  File "botocore/tokens.py", line 109, in _protected_refresh
  File "botocore/tokens.py", line 306, in _refresher
  File "botocore/utils.py", line 3352, in __call__
  File "botocore/utils.py", line 3505, in __getitem__

An unexpected error was encountered while executing "sam local invoke".
Search for an existing issue:
https://github.com/aws/aws-sam-cli/issues?q=is%3Aissue+is%3Aopen+Bug%3A%20sam%20local%20invoke%20-%20KeyError
Or create a bug report:
https://github.com/aws/aws-sam-cli/issues/new?template=Bug_report.md&title=Bug%3A%20sam%20local%20invoke%20-%20KeyError

Expected result:

sam to invoke lambda function locally

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

M1 Mac

  1. OS: OSX
  2. sam --version: SAM CLI, version 1.126.0
  3. AWS region: us-east-1
{
  "version": "1.126.0",
  "system": {
    "python": "3.8.20",
    "os": "macOS-14.6.1-arm64-arm-64bit"
  },
  "additional_dependencies": {
    "docker_engine": "27.2.0",
    "aws_cdk": "Not available",
    "terraform": "1.9.3"
  },
  "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 Provided above

schambers-slate commented 2 weeks ago

This is actually caused by not having an sso token locally. The error message isn't too indicative. I solved it by refreshing my sso token via aws sso login

I'm going to leave the issue open for now as the error message should really be refined to indicate that this is the potential issue

sidhujus commented 2 weeks ago

Hi @schambers-slate, Thanks for raising this issue! This seems to be a bug coming from the boto3 client since we do have error handling for errors coming from fetching the account id. Ill try to reproduce this locally and see if i can find the exact behaviour

schambers-slate commented 2 weeks ago

Thanks @sidhujus, If you need me to try to replicate again I can do so as well. Let me know.