I am currently working on the AWS platforma and Lambda. Please note that this is totally new to me. I am asked to change a code without being knowledgeable on the subject.
I have a lambda code with a lambda_handler() function.
I need to access the value 'apiKeyId'. For now, I have this line of code, that was writen by my predecessor (he left saying "you just have to uncomment this line") :
def lambda_handler(event, context):
id = event['requestContext']['identity']['apiKeyId']
However, when I run a test from the lambda window on AWS, I get this error :
{
"errorMessage": "'requestContext'",
"errorType": "KeyError",
"stackTrace": [
" File \"/var/task/lambda_function.py\", line 61, in lambda_handler\n id = event['requestContext']['identity']['apiKeyId']\n"
]
}
If I print the value 'requestContext', indeed, there is no 'identify' value, only this :
event = {'httpMethod': 'GET', 'queryStringParameters': {'q': 'toto'}}
Can someone explain to me why there is no 'identify' and 'apiKeyId' value in 'requestContext' ? Is there another way to access 'apiKeyId' ?
Your test
even is different that the actual event
passed to the function through API Gateway. To make the test work, you have to replicate the event structure that API gatway is using. That structure is explained in AWS docs: