lab82_diagram

The purpose of this lab is to create a chain of events. Messages send to a SQS Queue will trigger a Lambda function that will process them and store the output as JSON object to a S3 bucket.

Lab 82

Lab Details

  1. Services covered
  2. Lab description
  3. Lab date
  4. Prerequisites
  5. Lab steps
  6. Lab files
  7. Acknowledgements

Services Covered

  • lambda Lambda
  •  S3
  •  SQS

Lab description

The purpose of this lab is to create a chain of events. Messages send to a SQS Queue will trigger a Lambda function that will process them and store the output as JSON object to a S3 bucket.

  • Create Lambda function
  • Create a SQS Queue
  • Trigger Lambda function on new messages in the Queue

Lab date

10-10-2021


Prerequisites

  • AWS account

Lab steps

  1. Create a destination S3 bucket.
  2. Create a IAM Role for Lambda, it should allow it execution, access to S3 and SQS

  3. Create a Lambda function that will process and store messages
    import json
    
    import boto3
    
    def lambda_handler(event, context):
    
       sqs_msg = json.loads(event['Records'][0]['body'])
    
       print("SQS Message : ", sqs_msg)
    
       bucket_name = "<<BUCKET_NAME>>"
    
       try:
    
           s3Client = boto3.client("s3", region_name= "us-east-1")
    
           Response = s3Client.put_object(Bucket= bucket_name, Key= "Message.json", Body= json.dumps(sqs_msg))
    
           print("S3 upload success !")
    
           return {
    
               "status" : 200,
    
               "body" : "S3 upload success"
    
           }
    
       except Exception as e:
    
           print("Client connection to S3 failed because ", e)
    
           return{
    
               "status" : 500,
    
               "body" : "S3 upload failed"
    
           }
  4. Create a standard Queue in SQS.
  5. In the Lambda Configuration tab go to Triggers and choose the created queue.
  6. In SQS send a message to the queue.

    It should trigger execution of Lambda function and store that message to S3.


Lab files


Acknowledgements

Tags:

Leave a Reply