# Triggering AWS Lambda from Amazon SQS
- Learning material: ACloudGURU
## Diagram

## Introduce
In this hands-on AWS lab, we will learn how to trigger a Lambda function using SQS. This Lambda function will process messages from the SQS queue and insert the message data as records into a DynamoDB table.
## Steps
### Create the Lambda Function

- Add SQS as a trigger

- Modify the code
```javascript=
from datetime import datetime
import json
import os
import boto3
dynamodb = boto3.resource('dynamodb')
def lambda_handler(event, context):
# Count items in the Lambda event
no_messages = str(len(event['Records']))
print("Found " +no_messages +" messages to process.")
for message in event['Records']:
print(message)
# Write message to DynamoDB
table = dynamodb.Table('Message')
response = table.put_item(
Item={
'MessageId': message['messageId'],
'Body': message['body'],
'Timestamp': datetime.now().isoformat()
}
)
print("Wrote message to DynamoDB:", json.dumps(response))
```
### Log In to the EC2 Instance and Test the Script
- Switch user
```
su - cloud_user
```
- Execute the code in EC2 for sending messages to SQS
```javascript=
#!/usr/bin/env python3.8
# -*- coding: utf-8 -*-
import argparse
import logging
import sys
from time import sleep
import boto3
from faker import Faker
parser = argparse.ArgumentParser()
parser.add_argument("--queue-name", "-q", required=True,
help="SQS queue name")
parser.add_argument("--interval", "-i", required=True,
help="timer interval", type=float)
parser.add_argument("--message", "-m", help="message to send")
parser.add_argument("--log", "-l", default="INFO",
help="logging level")
args = parser.parse_args()
if args.log:
logging.basicConfig(
format='[%(levelname)s] %(message)s', level=args.log)
else:
parser.print_help(sys.stderr)
sqs = boto3.client('sqs')
response = sqs.get_queue_url(QueueName=args.queue_name)
queue_url = response['QueueUrl']
logging.info(queue_url)
while True:
message = args.message
if not args.message:
fake = Faker()
message = fake.text()
logging.info('Sending message: ' + message)
response = sqs.send_message(
QueueUrl=queue_url, MessageBody=message)
logging.info('MessageId: ' + response['MessageId'])
sleep(args.interval)
```
- Send 10 messages every second
```
./send_message.py -q Messages -i 0.1
```

### Confirm Messages Were Inserted into the DynamoDB Table
- See the spike in SQS monitoring

- Check the table in DynamoDB

- Go to "Explore items" and choose the table you want for checking.
