0

The attempt to call an AWS lambda function from Python on windows fails.

I have reset my credentials to new AWS keys. No good. I have added AWSLambdaFullAccess to my user ID and group. No workee.

import boto3,json

session = boto3.Session(
    aws_access_key_id="YesSireee",
    aws_secret_access_key="MySuperDuperSecretAccessKey",
)
lambda_client = boto3.client('lambda', region_name="us-east-2")
test_event = dict(key1="testme") 
try: 
    response = lambda_client.invoke(
  FunctionName='arn_copied_from_console',
  InvocationType='Event',
  LogType='None',
  Payload=json.dumps(test_event),
  )
except Exception as e: print(e)
print(response); # should be None. 

The error is:

An error occurred (UnrecognizedClientException) when calling the Invoke operation: The security token included in the request is invalid.
1
  • You are creating your own session but then creating the client using a default one. Shouldn’t that be session.client(...? Commented Oct 9, 2019 at 8:06

2 Answers 2

1

This is a wild guess but is the time on your test system correct (for example using NTP)?

Sign up to request clarification or add additional context in comments.

Comments

0

Your problem is here:

lambda_client = boto3.client('lambda', region_name="us-east-2")

It should be session.client instead of boto3.client. If you use boto3.client, it will search for credentials in the environment variables, credential file, IMDS etc, disregarding the session you created.

To use the credentials supplied in the code, use the session.client.

A better way to address this problem is to remove your credentials from your code, and populate them in the ~/.aws/credentials file, or in your environment variables, and then continue using boto3.client without creating sessions with hard-coded credentials.

Hope that helps :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.