Navigating Microservices: Domain Mapping and Nginx with FastAPI on AWS

Shikha Pandey
5 min readDec 27, 2023

--

In today’s dynamic software landscape, microservices architecture has become a key strategy for building scalable and maintainable applications. Each microservice functions independently, fostering flexibility and innovation. However, managing multiple microservices and ensuring their seamless integration can pose challenges.

One particular challenge that surfaces is the need to organize and consolidate multiple microservices under a unified domain. Without a cohesive approach to domain mapping, developers may struggle to provide a centralized access point for these services. Additionally, the lack of a unified domain makes it cumbersome for users and developers to interact with the services coherently.

In this context, addressing the intricacies of domain mapping becomes essential for streamlining access, enhancing user experience, and fostering a more efficient development environment.

This blog series will guide you through the process of deploying your FastAPI microservice under your custom domain, regardless of where you’ve purchased it.

The Problem:

Imagine you’ve developed a powerful microservice using FastAPI, and you’re eager to make it accessible under your custom domain, which you’ve purchased from a domain registrar like NameCheap, Hostinger, GoDaddy, etc. However, orchestrating this deployment seamlessly poses challenges. Without a unified domain strategy, coordinating the interactions between microservices becomes complex, hindering the overall manageability of your application.

Moreover, the absence of a centralized domain makes it cumbersome for users and developers to navigate through the services effectively. This lack of cohesion can hinder the user experience and complicate the development process.

The Solution:

Prerequisites:

Before we dive into the implementation, ensure you have the following prerequisites:

  1. AWS Account: You’ll need an AWS account to deploy your FastAPI microservices on an EC2 instance and leverage AWS services such as Route 53.
  2. Domain: Purchase a domain from a domain registrar of your choice (e.g., NameCheap, Hostinger, GoDaddy). This will serve as the custom domain under which your microservices will be accessible.
  3. Basic Knowledge of FastAPI: Familiarize yourself with FastAPI and have a FastAPI microservice ready for deployment.

Now, let’s get on with the solutions.

Deploying Microservices in Docker:

1. Deployment:

  • Utilize Docker containers for each FastAPI microservice on your AWS EC2 instance.

What is Docker?
Docker is a containerization platform that enables developers to package applications and their dependencies into isolated containers. These containers can run consistently across various environments, providing a standardized and efficient way to deploy and manage applications.

2. Exposing Container Ports:

  • Expose the necessary ports for each microservice container to enable communication.

3. Local Testing:

  • Ensure seamless interaction with each microservice locally within the EC2 instance.

Stay tuned for a more detailed exploration of this phase! Our upcoming blog post will delve into the intricacies of deploying microservices in Docker, providing a comprehensive guide for a smoother and more efficient process.

Domain Mapping and Nginx Configuration:

1. Domain Registration:

  • Register a domain through a domain registrar (e.g., example.com).

2. Route 53 Configuration:

What is Route 53?
Amazon Route 53 is a scalable and highly available Domain Name System (DNS) web service provided by AWS. It enables developers to register and manage domain names, as well as route end-user requests to applications.

How to do Route 53 Configuration:

Step 1: Log in to AWS Console: Log in to your AWS Management Console.

Step 2: Navigate to Route 53: Go to the Route 53 dashboard.

Step 3: Create Hosted Zone: Create a new hosted zone for your domain.

  • Click on “Create Hosted Zone.”
  • Enter your domain name (e.g., example.com).
  • Click “Create.”

Step 4: Record Set Creation:

  • Inside your hosted zone, create a new record set.
  • Set the record name (commonly www) and choose the type (e.g., A — IPv4 address).
  • Configure the value to point to your EC2 instance’s public IP address.
  • Save the record set.

Step 5: Update Nameservers:

  • After creating the hosted zone, Route 53 will provide you with a set of nameservers.
  • Update the nameservers at your domain registrar’s dashboard to point to the Route 53 nameservers.

3. Nginx Configuration:

What is Nginx?
Nginx is a powerful web server and reverse proxy server that is widely used for serving static content, acting as a load balancer, and handling various other tasks in web applications. In this context, Nginx is configured to manage domain mapping, SSL termination, and routing requests to microservices.

  • Launch your EC2 instance and Install Nginx on it:

Run the following commands in your EC2 terminal.

sudo apt install nginx
  • Open the Nginx default configuration file for editing:

— Using vi (Visual Editor):

sudo vi /etc/nginx/sites-enabled/default

If you’re using vi, you can navigate using arrow keys, and to edit, press i to enter insert mode. After editing, press Esc to exit insert mode, and then type :wq to save and exit.

—Alternative Using nano:

sudo nano /etc/nginx/sites-enabled/default

If you’re using nano, it provides on-screen guidance for editing. Use arrow keys for navigation. After editing, press Ctrl + O to write changes, press Enter, and then press Ctrl + X to exit.

Choose the editor that you find more comfortable.

  • Replace the default configuration with the following, adapting paths and settings to your microservices:
server {
listen 80;
server_name example.com;

location /microservice1/ {
proxy_pass http://127.0.0.1:8001/;
# ... other proxy settings
}

location /microservice2/ {
proxy_pass http://127.0.0.1:8002/;
# ... other proxy settings
}

location /microservice3/ {
proxy_pass http://127.0.0.1:8003/;
# ... other proxy settings
}

# ... other Nginx settings

}
  • Save and exit the editor.
  • Test Nginx configuration for syntax errors:
sudo nginx -t
  • If the test is successful, restart Nginx to apply the changes:
sudo service nginx restart
  • Check the status of the nginx server:
sudo service nginx status
  • Implement SSL/TLS certificates for encrypted and secure communication.
  • Now run your microservice containers on your EC2 instance.

Try to access your APIs or Swagger UI docs, most likely you will meet with an error. This is because when you’re running your FastAPI application behind a proxy server like NGINX, the application might not be aware of the path it’s being served at. This causes issues.

Let’s Fix it up:

  • In each FastAPI app, use the root_path parameter to configure the Swagger docs path.
from fastapi import FastAPI
app = FastAPI(root_path="/microservice1")
  • Repeat this for other microservices.

Access Swagger UI:

Testing and Troubleshooting:

1.Test Your Setup:

2. Troubleshooting:

  • Check Nginx and FastAPI logs for any errors.
  • Verify DNS settings and SSL certificate configurations.

To Sum Up:

The deployment of containerized FastAPI microservices, coupled with domain mapping and Swagger documentation, offers a robust solution to the challenges of managing multiple microservices. By following this guide, you not only organize your microservices effectively under a unified domain but also provide clear API documentation for developers.

Additional Considerations:

  • Scalability with AWS Services: Explore AWS services like Elastic Container Service (ECS) and Elastic Load Balancer (ELB) for enhanced scalability and high availability.
  • Monitoring and Insights: Leverage AWS CloudWatch to monitor your infrastructure and gain valuable insights into the performance of your microservices.

To wrap things up, consider this guide your compass through the intricate world of microservices deployment, domain mapping, and API documentation. It’s your key to unlocking a user experience that seamlessly merges sophistication with security.

Feel at liberty to tailor the content to mirror your unique experiences and perspectives. Now, dive into the joy of writing!

If you want a complete blog from scratch from API creation to dockerization and deployment, feel free to let me know in the comments.

--

--

Shikha Pandey

Software Engineer - Tech Enthusiast - Startup Enthusiast. Reach me out at https://shikhapandey.me/:)