skip to Main Content

I am getting the above error when I try query a key in a DynampDB table:

Here is the lambda code:

import json
import boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('resumechallenge-test')
def lambda_handler(event, context):
    response = table.get_item(Key={
        'id':'0'
    })
    views = response['item']['views']
    views = views +1
    print(views)
    response = table.put_item(Item={
        'id':'0',
        'views': views
        
    })
    
    return views

and here is the dynamodb table:

DynamoDB

Any help would be greatly appreciated, as I just keep getting an internal server error message when I go to the lambda function url. Thanks so much!

2

Answers


  1. Chosen as BEST ANSWER

    Thank you very much for taking the time to respond.

    I have updated the code with your ammendments, thank you for explaining. I am very new to this - so learning so much so fast - so bare with me!

    I am still getting the same error though with the new code:

    import json
    import boto3
    dynamodb = boto3.resource('dynamodb')
    table = dynamodb.Table('cloudresume-test')
    def lambda_handler(event, context):
        response = table.get_item(Key={
            'id':'0'
        })
        views = Item: response['Attributes']['views']
        views = views +1
        print(views)
       response = table.update_item(
            Key={
                'id': '0'
            },
            UpdateExpression="#v =  #v + :inc",
            ExpressionAttributeNames={
                "#v": "views",
            
            },
            ExpressionAttributeValues={
                ':inc': 1
            }, 
            ReturnValues='ALL_NEW'
        )
    
        views = response['Item']['views']
        
          return {
            "StatusCode": 200,
            "headers": {
                "Content-Type": "application/json"
            },
            "body": json.dumps({
                "data ": views
            })
        }```
    
    
    Thanks again!!
    

  2. A couple of things:

    CloudWatch

    First thing to do is check your CloudWatch logs for your Lambda, they will reveal the exact reason why your function is producing an error, and ultimately returning Internal Server Error.

    Lambda

    Lambda expects a stauscode and body as a response, which is especially important when using API Gateway. So its good practice to do that:

        return {
            "StatusCode": 200,
            "headers": {
                "Content-Type": "application/json"
            },
            "body": json.dumps({
                "data ": views
            })
        }
    

    DynamoDB

    You table name does not match between your code and your screenshot, double check you have the correct table name specified.

    You are also looking for your value in response['item']['views'] however, the key you should be looking in is Item: response['Attributes']['views']

    A better approach

    You are first getting an item, then updating the counter value, but you leave yourself open to concurrency issues. To handle both concurrency issues, and to reduce your costs and improve performance, you should use update_item request:

        response = table.update_item(
            Key={
                'id': '0'
            },
            UpdateExpression="#v =  #v + :inc",
            ExpressionAttributeNames={
                "#v": "views",
            
            },
            ExpressionAttributeValues={
                ':inc': 1
            }, 
            ReturnValues='ALL_NEW'
        )
    
        views = response['Item']['views']
    

    Now you make a single API call to DynamoDB, which is atomic and prevents the need for an additional read. This is a much better solution to your use-case.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search