AWS Lambda is one of the computing services provided by AWS. It runs your code in response to events and automatically manages the compute resources it needs.
In this case, when someone uploads a file to S3 bucket. It triggers Lambda function to read and update count item on event table in DynamoDB. These are the steps I use to do the tasks:
import json
import boto3
dynamodb_client = boto3.resource('dynamodb')
table = dynamodb_client.Table('event-table')
def lambda_handler(event: any, context: any):
upload: str = event["event"]
upload_count: int = 0
# Get the upload count from the DynamoDB table
result = table.get_item(Key={"event": upload})
if "Item" in result:
upload_count = result["Item"]["count"]
# Increment the upload count and put the item into DynamoDB table.
upload_count += 1
table.put_item(Item={"event": upload, "count": upload_count})
message: str = f"Hello {user}! You have visited us {upload_count} times."
return {"message": message}
Stay up-to-date with new posts