Create Lambda - DynamoDB Count Function

Viewed 233

I am creating a SAM web app, with the backend being an API in front of a Python Lambda function with a DynamoDB table that maintains a count of the number of HTTP calls to the API. The API must also return this number. The yaml code itself loads normally. My problem is writing the Lambda function to iterate and return the count. Here is my code:

def lambda_handler(event, context):
    dynamodb = boto3.resource("dynamodb")
    ddbTableName = os.environ["databaseName"]
    table = dynamodb.Table(ddbTableName)
    # Update item in table or add if doesn't exist
    ddbResponse = table.update_item(
        Key={"id": "VisitorCount"},
        UpdateExpression="SET count = count + :value",
        ExpressionAttributeValues={":value": Decimal(context)},
        ReturnValues="UPDATED_NEW",
    )

    # Format dynamodb response into variable
    responseBody = json.dumps({"VisitorCount": ddbResponse["Attributes"]["count"]})

    # Create api response object
    apiResponse = {"isBase64Encoded": False, "statusCode": 200, "body": responseBody}

    # Return api response object
    return apiResponse

I can get VisitorCount to be a string, but not a number. I get this error: [ERROR] TypeError: lambda_handler() missing 1 required positional argument: 'cou    response = request_handler(event, lambda_context)le_event_request What is going on?

[UPDATE] I found the original error, which was that the function was not properly received by the SAM app. Changing the name fixed this, and it is now being read. Now I have to troubleshoot the actual Python. New Code:

import json
import boto3
import os

dynamodb = boto3.resource("dynamodb")
ddbTableName = os.environ["databaseName"] 
table = dynamodb.Table(ddbTableName)
Key = {"VisitorCount": { "N" : "0" }}
    
def handler(event, context):
           # Update item in table or add if doesn't exist
    ddbResponse = table.update_item(
        UpdateExpression= "set VisitorCount = VisitorCount + :val",
        ExpressionAttributeValues={":val": {"N":"1"}},
        ReturnValues="UPDATED_NEW",
    )

    # Format dynamodb response into variable
    responseBody = json.dumps({"VisitorCount": ddbResponse["Attributes"]["count"]})

    # Create api response object
    apiResponse = {"isBase64Encoded": False, "statusCode": 200,"body": responseBody}

    # Return api response object
    return apiResponse

I am getting a syntax error on Line 13, which is

UpdateExpression= "set VisitorCount = VisitorCount + :val", But I can't tell where I am going wrong on this. It should update the DynamoDB table to increase the count by 1. Looking at the AWS guide it appears to be the correct syntax.

1 Answers

Not sure what the exact error is but ddbResponse will be like this:

ddbResponse = table.update_item(
    Key={
        'key1': aaa,
        'key2': bbb
    },
    UpdateExpression= "set VisitorCount = VisitorCount + :val",
    ExpressionAttributeValues={":val": Decimal(1)},
    ReturnValues="UPDATED_NEW",
)
  • Specify item to be updated with Key (one item for one Lambda call)
  • Set Decimal(1) for ExpressionAttributeValues

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.Python.03.html#GettingStarted.Python.03.04

Related