Skip to content

Latest commit

Β 

History

History
131 lines (91 loc) Β· 3.08 KB

File metadata and controls

131 lines (91 loc) Β· 3.08 KB

πŸ“¬ FastAPI AWS SQS with LocalStack

πŸš€ This project demonstrates how to send messages to an AWS SQS queue using FastAPI and LocalStack for local testing.

🌟 Why Use AWS SQS?

Amazon Simple Queue Service (SQS) is a fully managed message queuing service that enables decoupling and scaling of microservices, distributed systems, and serverless applications. Some key benefits include:

  • Scalability: Easily handle high-throughput workloads.
  • Reliability: Built-in redundancy ensures message durability.
  • Decoupling: Separates different parts of your application for better maintainability.
  • Security: Supports encryption and access control via AWS IAM policies.
  • Asynchronous Processing: Ideal for background tasks and event-driven architectures.

πŸ“‚ Project Structure

.
β”œβ”€β”€ .env
β”œβ”€β”€ main.py
β”œβ”€β”€ core
β”‚   └── config.py
β”œβ”€β”€ dependencies
β”‚   └── aws_sqs.py
β”œβ”€β”€ routes
β”‚   └── api
β”‚       └── messages.py
β”œβ”€β”€ schemas
β”‚   └── messages.py
└── utils
    └── logger.py

πŸ”§ Requirements

  • Python 3.8+
  • uv (for dependency management)

πŸ“₯ Installation

1️⃣ Clone the repository:

git clone https://github.com/egoan82/fastapi-with-aws-sqs.git
cd fastapi-with-aws-sqs

2️⃣ Create and activate a virtual environment with uv:

uv venv
source .venv/bin/activate

3️⃣ Install dependencies:

pip install -r requirements.txt

4️⃣ Configure environment variables in .env:

AWS_KEY_ID="test"
AWS_SECRET_KEY="test"
AWS_REGION="us-east-1"
SQS_QUEUE_URL="https://localhost.localstack.cloud:4566/000000000000/queue.fifo"
LOCALSTACK_URL="https://localhost.localstack.cloud:4566"

πŸš€ Running the Project

1️⃣ Run the FastAPI application:

uvicorn main:app --reload

️2️⃣ Run listener to receive messages:

python main.py

3️⃣ Access the interactive API documentation:

πŸ”— Swagger UI
πŸ”— ReDoc

πŸ“€ Sending a Message to SQS

1️⃣ Make a POST request to /send_message with the following body:

{
  "title": "string",
  "message": "string"
}

2️⃣ The API responds with the message_id of the sent message.

πŸ–ΌοΈ Screenshots

Here are some screenshots of the API in action:

πŸ“Œ Swagger UI

Swagger UI

πŸ“Œ LoocalStack Dashboard - Queues

API Response

πŸ“Œ LoocalStack Dashboard - Messages

API Response

πŸ“Œ LoocalStack Dashboard - Message Details

API Response

πŸ“Œ Terminal - Message received

API Response

πŸ“š Resources


πŸš€ Happy coding and enjoy building with FastAPI and AWS SQS! πŸŽ‰