Skip to content

Bug: sam local invoke - JSONDecodeError #4656

@VinithS

Description

@VinithS

Description:

When calling sam local invoke with --profile aws-profile flag, an exception is thrown. I can re-run the same command without the profile flag and manually set the env variables which executes correctly.

I think it might be similar to #3275 but no templates were provided then.

Steps to reproduce:

run sam local invoke -t /path/to/template.yml --profile my-aws-profile

The lambda template file is very simple.

---
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  This template is here specifically for use with RDE. This template is only for running
  the lambda locally in your own machine using sam invoke.

Globals:
  Function:
    Timeout: 30

Resources:
  MyLambda:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: ./build
      Handler: MyLambda/index.handler
      Runtime: nodejs18.x
      MemorySize: 2000
      Policies:
        - Version: '2012-10-17'
          Statement:
            - Effect: Allow
              Action:
                - dynamodb:*
              Resource: '*'
    Metadata:
      {
        'BuildMethod': 'makefile',
        'aws:asset:path': './Makefile',
        'aws:asset:property': 'Code',
      }

Observed result:

2023-02-02 11:57:39,635 | Config file location: /Volumes/vinith/project/src/project/samconfig.toml
2023-02-02 11:57:39,635 | Loading configuration values from [default.['local', 'invoke'].parameters] (env.command_name.section) in config file at '/Volumes/vinith/project/src/project/samconfig.toml'...
2023-02-02 11:57:39,637 | Configuration values successfully loaded.
2023-02-02 11:57:39,637 | Configuration values are: {}
2023-02-02 11:57:39,657 | Using config file: samconfig.toml, config environment: default
2023-02-02 11:57:39,657 | Expand command line arguments to:
2023-02-02 11:57:39,657 | --template_file=/Volumes/vinith/project/src/project/local-lambda.yml --no_event --layer_cache_basedir=/Users/vinithsh/.aws-sam/layers-pkg --container_host=localhost --container_host_interface=127.0.0.1 
2023-02-02 11:57:39,657 | local invoke command is called
2023-02-02 11:57:39,659 | No Parameters detected in the template
2023-02-02 11:57:39,668 | There is no customer defined id or cdk path defined for resource MyLambda, so we will use the resource logical id as the resource id
2023-02-02 11:57:39,669 | 0 stacks found in the template
2023-02-02 11:57:39,669 | No Parameters detected in the template
2023-02-02 11:57:39,674 | There is no customer defined id or cdk path defined for resource MyLambda, so we will use the resource logical id as the resource id
2023-02-02 11:57:39,674 | 1 resources found in the stack 
2023-02-02 11:57:39,674 | Found Serverless function with name='MyLambda' and CodeUri='./build'
2023-02-02 11:57:39,674 | --base-dir is not presented, adjusting uri ./build relative to /Volumes/vinith/project/src/project/local-lambda.yml
2023-02-02 11:57:39,706 | Found one Lambda function with name 'MyLambda'
2023-02-02 11:57:39,706 | Invoking MyLambda/index.handler (nodejs18.x)
2023-02-02 11:57:39,706 | Loading AWS credentials from session with profile 'my-aws-profile'
2023-02-02 11:57:40,494 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2023-02-02 11:57:40,629 | Sending Telemetry: {'metrics': [{'commandRun': {'requestId': '7c7653de-cbbe-46d1-90f5-6dae2a0caa3c', 'installationId': '397806e8-eb55-4026-b1ff-932edeb1712d', 'sessionId': '868ec6e7-d5e8-48e7-b63f-21f3f99bcf12', 'executionEnvironment': 'CLI', 'ci': False, 'pyversion': '3.8.13', 'samcliVersion': '1.71.0', 'awsProfileProvided': True, 'debugFlagProvided': True, 'region': '', 'commandName': 'sam local invoke', 'metricSpecificAttributes': {'projectType': 'CFN', 'gitOrigin': None, 'projectName': '54385b5107a7105ca2f6674f02d02bae10fc615ec14d6c5e9a79a560a86f7c0e', 'initialCommit': None}, 'duration': 837, 'exitReason': 'JSONDecodeError', 'exitCode': 255}}]}
2023-02-02 11:57:40,711 | Telemetry response: 200

Error: Expecting value: line 1 column 1 (char 0)
Traceback:
  File "click/core.py", line 1055, in main
  File "click/core.py", line 1657, in invoke
  File "click/core.py", line 1657, in invoke
  File "click/core.py", line 1404, in invoke
  File "click/core.py", line 760, in invoke
  File "click/decorators.py", line 84, in new_func
  File "click/core.py", line 760, in invoke
  File "samcli/lib/telemetry/metric.py", line 183, in wrapped
  File "samcli/lib/telemetry/metric.py", line 150, in wrapped
  File "samcli/lib/utils/version_checker.py", line 41, in wrapped
  File "samcli/cli/main.py", line 92, in wrapper
  File "samcli/commands/local/invoke/cli.py", line 94, in cli
  File "samcli/commands/local/invoke/cli.py", line 201, in do_cli
  File "samcli/commands/local/lib/local_lambda.py", line 133, in invoke
  File "samcli/commands/local/lib/local_lambda.py", line 185, in get_invoke_config
  File "samcli/commands/local/lib/local_lambda.py", line 285, in _make_env_vars
  File "samcli/commands/local/lib/local_lambda.py", line 330, in get_aws_creds
  File "samcli/commands/local/lib/local_lambda.py", line 315, in _get_session_creds
  File "boto3/session.py", line 203, in get_credentials
  File "botocore/session.py", line 471, in get_credentials
  File "botocore/credentials.py", line 1988, in load_credentials
  File "botocore/credentials.py", line 975, in load
  File "botocore/credentials.py", line 1001, in _retrieve_credentials_using
  File "json/__init__.py", line 357, in loads
  File "json/decoder.py", line 337, in decode
  File "json/decoder.py", line 355, in raw_decode

Expected result:

Lambda to be invoked.

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

❯ sam --info
{
"version": "1.71.0",
"system": {
"python": "3.8.13",
"os": "macOS-12.6.1-arm64-arm-64bit"
},
"additional_dependencies": {
"docker_engine": "20.10.14",
"aws_cdk": "Not available",
"terraform": "Not available"
}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    blocked/more-info-neededMore info is needed from the requester. If no response in 14 days, it will become stale.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions