Connect with us

Get more updates and further details about your project right in your mailbox.

Thank you!
Oops! Something went wrong while submitting the form.
September 26, 2023

Building Scalable Serverless CRUD APIs with FastAPI and AWS DynamoDB Local: A Step-by-Step Guide

The best time to establish protocols with your clients is when you onboard them.

Heading

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse varius enim in eros elementum tristique. Duis cursus, mi quis viverra ornare, eros dolor interdum nulla, ut commodo diam libero vitae erat. Aenean faucibus nibh et justo cursus id rutrum lorem imperdiet. Nunc ut sem vitae risus tristique posuere.

Revolutionize the way you develop applications by seamlessly integrating Python FastAPI and the power of AWS DynamoDB.

DynamoDB: AWS’s Managed NoSQL Powerhouse

DynamoDB, an AWS’s Managed NoSQL Powerhouse DynamoDB, an AWS-managed NoSQL database, boasts effortless scalability, low-latency access, and automatic replication. Its prowess lies in effectively managing substantial data loads and high-speed read/write operations, making it an ideal solution for diverse data handling needs.

FastAPI: Speed and Simplicity in Python FastAPI

FastAPI, a contemporary Python web framework, excels in rapid API development with its agility and ease. It handles vital tasks such as data validation, serialization, and documentation, empowering developers to effortlessly craft robust APIs. With asynchronous programming support, FastAPI is tailor-made for real-time applications.

FastAPI vs. Traditional REST APIs:

Speed and Efficiency:

  • FastAPI: Utilizes asynchronous programming for high performance, handling more requests with fewer resources.
  • REST APIs: Generally synchronous, which can lead to slower response times under heavy loads.

Type Safety and Validation:

  • FastAPI: Leverages Python type hints for automatic data validation and interactive API documentation.
  • REST APIs: Often require manual validation and lack built-in interactive documentation.

Framework Features:

  • FastAPI: Modern framework with automatic serialization, dependency injection, and native WebSocket support.
  • REST APIs: May need additional libraries for similar features, potentially adding complexity.

Development Speed:

  • FastAPI: Quick development with automatic endpoint generation and reduced boilerplate code.
  • REST APIs: More manual setup and configuration, possibly leading to longer development cycles.

Why Combine FastAPI and DynamoDB?

By combining FastAPI with DynamoDB, you leverage the strengths of both. FastAPI’s efficient API design and asynchronous capabilities align perfectly with DynamoDB’s scalability and low-latency access. This synergy enables developers to build high-performance APIs powered by a dynamic, robust database, enhancing user experiences significantly.

Local DynamoDB Configuration:

We will begin the process by setting up our DynamoDB environment through the straightforward method of downloading Docker. Download it from here https://docs.docker.com/engine/install/

Run below command to run DynamoDB locally, Create a folder structure. This is how the folder structure should look like:

There are two ways to start docker, either we can start it globally or you can go to docker-compose.yml, start a terminal. Open a terminal or command prompt.

1.Pull the DynamoDB Local Docker image:

docker pull amazon/dynamodb-local

2.Run the DynamoDB Local container:

docker run -p 8000:8000 amazon/dynamodb-local

Open a new terminal window in the same project folder, if you are running it from the terminal. This terminal will be used for additional commands without interrupting the running DynamoDB container.

Dynamodb Admin:

Setting up DynamoDB Admin (a web-based GUI tool for managing your DynamoDB tables) can be helpful for visualizing and managing your local DynamoDB instance. Here’s how you can set up DynamoDB Admin to work with DynamoDB Local:

Go to FastAPI, install DynamoDB Admin using npm (Node.js package manager). Make sure you have Node.js installed on your machine.

npm install -g dynamodb-admin

Run DynamoDB Admin, specifying the local DynamoDB endpoint.

DYNAMO_ENDPOINT=http://localhost:8000

Make sure to replace http://localhost:8000 with the appropriate endpoint if you are running DynamoDB Local on a different host or port.

You can go to the browser, run http://localhost:8001/ and access the web based UI, since the aim of this article is to integrate fastapi with dynamodb, we won’t go deep into it but you can create tables as it happens in aws.Now, let’s create a virtual environment for running before we get started with fastApi

Creating a virtual environment is a best practice in Python development. It isolates project-specific dependencies, ensuring that different projects can have their own distinct set of libraries without interfering with each other. This will be the command for creating virtual environment

python -m venv venv

Enter the Below command in terminal this will activate venv:

.\venv\Scripts\activate

Install dependencies

pip install fastapi[all]

Dependencies Explanation:

pip install: This is the same command used to install Python packages.

fastapi[all]: This uses an “extra” called all provided by the FastAPI package. An “extra” is a way to install a package along with additional optional dependencies.

pip install uvicorn boto3

Dependencies Explanation:

uvicorn: This is an ASGI server implementation that is commonly used to run FastAPI applications. It provides high performance and supports asynchronous programming.

boto3: This is the Amazon Web Services (AWS) SDK for Python. It allows you to interact with various AWS services, including DynamoDB (a NoSQL database service).

Docker compose configuration:

Configuration via docker-compose.yml

This Docker Compose configuration defines a service named “application.” It creates an image based on the Dockerfile in the current directory, forwards traffic from port 8000 on the host to port 80 in the container, and establishes a volume link between the local “app” directory and the “/app” directory inside the container.

DockerFile content

This Dockerfile starts with a base image named “tiangolo/uvicorn-gunicorn-fastapi” that includes Python 3.8 and the Uvicorn ASGI server. The “COPY” command is used to copy the contents of the local “./app” directory to the “/app” directory inside the container. This establishes the application code’s presence within the container, making it ready to be executed by the specified server.

# main.py

To confirm proper FastAPI configuration, execute the following command:

Go to the URL, if you see {“Hello”: “World”}, then congratulations, you’ve just configured FastAPI successfully.

To begin our CRUD operations, the initial step involves table creation. Let’s establish a “todo” table as the foundation.

Create a createTable.py file in dynamodb folder

Now try to run the command python createTable.py, if you get “Table status:Active” in the terminal, then table creation is successful. Functions for crud Api in crud.py

Now lets create routers in main.py:

You have the option to test it either using Postman or a UI-based framework for FastAPI. To test it within the FastAPI framework, simply run the following URL: “localhost:8080/docs”. Additionally, there’s another URL option available: “localhost:8080/redoc”.

CodeStax.Ai
Profile
September 26, 2023
-
6
min read
Subscribe to our newsletter
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Share this article:

More articles