DEV Community

architec
architec

Posted on • Updated on

AWS Cloud Quest: Integrating Serverless Applications Study Note

This is the DIY challenge of the Integrating Serverless Applications in AWS Cloud Quest.

Image description

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

Enter fullscreen mode Exit fullscreen mode

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)
    }
Enter fullscreen mode Exit fullscreen mode

DIY Steps:

  1. Create a new SQS Queue diy
  2. Add Dead-letter queue to MyMessageQueue

Image description

Top comments (1)

Collapse
 
alexako profile image
Alex J. Reyes

Image description
Anyone else get this after creating the DLQ?