0
votes

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

1 Answers

1
votes

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?