0

I need help with figuring out how to format my Key in lambda to update an item in DynamoDB. Below is the code I have but I can't figure out how to format the Key.

My table looks as follows:

enter image description here

'''

import json import boto3

dynamodb = boto3.resource('dynamodb')

client = boto3.client('dynamodb')

def lambda_handler(event, context):

response = client.update_item(
    TableName='hitcounter',
    Key={????????},
    UpdateExpression='ADD visits :incr',
    ExpressionAttributeValues={':incr': 1}

)


print(response)

'''

ERROR MESSAGE:

''' { "errorMessage": "'path'", "errorType": "KeyError", "stackTrace": [ " File \"/var/task/lambda_function.py\", line 11, in lambda_handler\n Key={'path': event['path']},\n" ] }

'''

1 Answer 1

1

The AWS docs provides an example for Updating an item:

table.update_item(
    Key={
        'username': 'janedoe',
        'last_name': 'Doe'
    },
    UpdateExpression='SET age = :val1',
    ExpressionAttributeValues={
        ':val1': 26
    }
)

I'm not sure from your question, if the AWS examples are unclear or what is the issue specifically?

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

8 Comments

I keep getting this error. I'm assuming that it has to do with my Key. I've tried following documentation but still get it. ''' { "errorMessage": "'path'", "errorType": "KeyError", "stackTrace": [ " File \"/var/task/lambda_function.py\", line 11, in lambda_handler\n Key={'path': event['path']},\n" ] } '''
@nktanu This is your lambda code issue, not specific to DdB. Can you update the message with the error message, the structure of event object and how do you access it?
Sorry I'm pretty new to this and learning as much as I can right now. I added the error to to my post. I'm not sure what you mean by the structure of the event and how it's accessed? @Marcin
can you add log output for the "event" variable? Check on your cloudwatch what the log result and paste here? I guess the problem came there. It's something like event['path'] is missing value.
Got it figured out! It had to do with the defining variables at the top of my code
|

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.