This is the DIY challenge of the Integrating Serverless Applications in AWS Cloud Quest.
producer
Lambda
import boto3
import json
import time
# Boto3 - DynamoDB Client - Mode Info: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html
sqs = boto3.resource('sqs')
# Replace Queue name with Queue URL from Account
queue = sqs.Queue('Your-Queue-Name')
# AWS Lambda Function that publishes a message at Queue
def lambda_handler(event, context):
message = {
"coder_id": "123",
"spot_id": "321",
"timestamp": round(time.time() * 1000)
}
response = queue.send_message(MessageBody=json.dumps(message))
return response
consumer
Lambda
import json
import boto3
# Boto3 - DynamoDB Client - Mode Info: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html
sqs = boto3.resource('sqs')
dynamodb = boto3.resource('dynamodb')
# AWS Lambda Function that consumes event from Queue and writes at DynamoDB
def lambda_handler(event, context):
if 'Records' in event:
for record in event['Records']:
payload = json.loads(record["body"])
if 'coder_id' not in payload:
raise ValueError('erro format')
else:
table = dynamodb.Table('checkinData')
table.put_item(
Item={
'coderId': payload['coder_id'],
'timestamp': payload['timestamp'],
'spotID': payload['spot_id']
}
)
return {
'statusCode': 200,
'body': json.dumps(payload)
}
DIY Steps:
- Create a new SQS Queue
diy
- Add Dead-letter queue to
MyMessageQueue
Top comments (1)
Anyone else get this after creating the DLQ?