This project demonstrates how to use FastAPI to send messages to an AWS SQS queue and how to set up an AWS Lambda function to process those messages.
- Python 3.10+
- AWS Account
- AWS CLI configured with the necessary permissions
- FastAPI
- Uvicorn
- boto3
- python-dotenv
git clone https://github.com/dcsm8/aws_sqs_example.git
cd aws_sqs_example
python -m venv venv
source venv/bin/activate # On Windows use `venv\Scripts\activate`
pip install fastapi uvicorn boto3 python-dotenv
Create a .env
file in the root directory and add your AWS credentials:
AWS_ACCESS_KEY_ID=your_access_key_id
AWS_SECRET_ACCESS_KEY=your_secret_access_key
AWS_REGION=us-east-1 # Change to your desired region
Update the queue_url
in main.py
with your actual SQS queue URL:
queue_url = 'https://sqs.us-east-1.amazonaws.com/YOUR_ACCOUNT_ID/MyQueue' # Replace with your Queue URL
Start the FastAPI application:
uvicorn main:app --reload
You can test sending messages to the SQS queue using a tool like Postman
- Set the request type to POST.
- Set the URL to
http://127.0.0.1:8000/send-message/
. - Set the header
Content-Type
toapplication/json
. - Set the body to:
{ "action": "process_data", "data": { "key1": "value1", "key2": "value2" } }
- Log in to the AWS Management Console.
- Navigate to the Lambda Console.
- Create a new Lambda function named
ProcessSQSEvent
with Python 3.10 runtime.
Here is an example Lambda function code:
import json
def lambda_handler(event, context):
for record in event['Records']:
body = record['body']
print(f'Received message: {body}')
try:
message = json.loads(body)
print(f'Parsed message: {message}')
if 'action' in message:
if message['action'] == 'process_data':
print('Processing data...')
elif message['action'] == 'send_email':
print('Sending email...')
else:
print(f'Unknown action: {message["action"]}')
except json.JSONDecodeError:
print('Could not parse message body as JSON')
return {
'statusCode': 200,
'body': json.dumps('Messages processed successfully!')
}
- Add an SQS trigger to your Lambda function and select your SQS queue.
- Ensure the Lambda function's execution role has permissions to read from the SQS queue.
Send messages using your FastAPI application and check the Lambda logs in CloudWatch to verify the messages are processed correctly.