AWS SAM Incorrect region

Viewed 373

I am using AWS SAM to test my Lambda functions in the AWS cloud.

This is my code for testing Lambda:

# Set "running_locally" flag if you are running the integration test locally
running_locally = True
 
def test_data_extraction_validate():

    if running_locally:
        lambda_client = boto3.client(
            "lambda",
            region_name="eu-west-1",
            endpoint_url="http://127.0.0.1:3001",
            use_ssl=False,
            verify=False,
            config=botocore.client.Config(
                signature_version=botocore.UNSIGNED,
                read_timeout=10,
                retries={'max_attempts': 1}
            )
        )
    else:
        lambda_client = boto3.client('lambda',region_name="eu-west-1")

    ####################################################
    #   Test 1. Correct payload
    ####################################################

    with open("payloads/myfunction/ok.json","r") as f:
        payload = f.read()

    # Correct payload
    response = lambda_client.invoke(
        FunctionName="myfunction",
        Payload=payload
    )
    result = json.loads(response['Payload'].read())
    assert result['status'] == True
    assert result['error'] == ""

This is the command I am using to start AWS SAM locally:

sam local start-lambda -t template.yaml --debug --region eu-west-1

Whenever I run the code, I get the following error:

botocore.exceptions.ClientError: An error occurred (ResourceNotFound) when calling the Invoke operation: Function not found: arn:aws:lambda:us-west-2:012345678901:function:myfunction

I don't understand why it's trying to invoke function located in us-west-2 when I explicitly told the code to use eu-west-1 region. I also tried to use AWS Profile with hardcoded region - the same error.

When I switch the running_flag to False and run the code without AWS SAM everything works fine.

===== Updated =====

The list of env variables:

# env | grep 'AWS'                  
AWS_PROFILE=production

My AWS configuration file:

# cat /Users/alexey/.aws/config     
[profile production]
region = eu-west-1

My AWS Credentials file

# cat /Users/alexey/.aws/credentials
[production]
aws_access_key_id = <my_access_key>
aws_secret_access_key = <my_secret_key>
region=eu-west-1
1 Answers

Make sure you are actually running the correct local endpoint! In my case the problem was that I had started the lambda client with an incorrect configuration previously, and so my invocation was not invoking what I thought it was. Try killing the process on the port you have specified: kill $(lsof -ti:3001), run again and see if that helps!

This also assumes that you have built the function FunctionName="myfunction" correctly (make sure your function is spelt correctly in the template file you use during sam build)

Related