I'm not familiar with Lambda or APIs, but I'm trying to use my API to execute a PUT operation and call a lambda code to update a DynamoDB table. When I test my lambda function using a JSON event, it operates as intended.
import boto3
import json
def updateScore(event, context):
dynamodb_client = boto3.client('dynamodb', region_name='us-east-1')
dynamodb_resource = boto3.resource('dynamodb', region_name='us-east-1')
table = dynamodb_resource.Table('Highscore')
score = event["Highscore"]
print(event.keys())
response = table.update_item(
Key={'ID': 0},
ExpressionAttributeNames = {"#hs": "Highscore"},
UpdateExpression="SET #hs = :val1",
ExpressionAttributeValues={":val1": score}
)
return {
"statusCode": 200,
"headers": {
'Access-Control-Allow-Headers': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'PUT'
},
'body': event['Highscore']
}
with the JSON object test event
{
"ID": 0,
"Highscore": 1
}
The code correctly updates my table, but when I try to test the function using the API gateway, it behaves as if the event provided by the gateway does not exist (I say this because in the logs it shows a Key Error on the event parameter indicating that it does not exist). I receive an internal server error 502 on both the test in the AWS console and on Postman. The identical information that was in the event JSON object is what I am delivering in the body, however it is still not working. Thank you for your assistance.