AWS Builder Center

From Manual AWS Infrastructure to Terraform: Deploying a FastAPI Application with CI/CD

I built a small FastAPI application on AWS manually first, then brought the existing infrastructure under Terraform without rebuilding it. This article covers the architecture, Terraform migration, GitHub Actions CI/CD, and the engineering decisions behind the project.

I wanted to build a small AWS deployment that I could actually understand from end to end.
Instead of starting with a large collection of AWS services, I built a simple FastAPI application and gradually added the infrastructure needed to run it, deploy it, monitor it, and eventually manage it with Terraform.
The interesting part of the project wasn't simply getting FastAPI running on EC2. It was understanding what happens between a Git commit and a running application on AWS.

The Architecture

The final architecture is intentionally simple.
Project architecture

The EC2 instance runs inside a public subnet. Nginx is the public-facing reverse proxy, while Uvicorn listens only on  127.0.0.1:8000 .
PostgreSQL runs on Amazon RDS in private subnets and is not publicly accessible.
The AWS environment uses a VPC with one public subnet for the application server and two private subnets for the database subnet group.
I deliberately did not add a NAT Gateway, load balancer, Kubernetes, or other infrastructure that wasn't necessary for this application. The goal was to understand the architecture rather than make it look larger than it needed to be.

Building It Manually First

I started by creating and configuring the AWS infrastructure manually.
The application was deployed to an Amazon Linux EC2 instance using:
  • FastAPI
  • Uvicorn
  • Nginx
  • systemd
  • PostgreSQL on RDS
I also configured IAM roles, Systems Manager, security groups, CloudWatch monitoring, and database migrations with Alembic.
Before introducing Terraform, I verified that the pieces actually worked together.
For example, from EC2 I verified connectivity to RDS, including DNS resolution, TCP connectivity on port 5432, PostgreSQL authentication, and SSL connectivity.
This manual phase was intentional.
I wanted to understand the infrastructure first instead of writing Terraform configuration without knowing what the resources were actually doing.

Bringing the Existing Infrastructure Under Terraform

Once the environment was working, I started the Terraform migration.
This created an important constraint: the infrastructure already existed.
I didn't want Terraform to destroy the EC2 instance, RDS database, or VPC and create replacements simply because Terraform didn't originally create them.
Instead, the process was:
Existing AWS resource ↓ Terraform resource definition ↓ terraform import ↓ terraform plan ↓ No changes
I created Terraform modules for the meaningful parts of the architecture:
modules/ ├── networking/ ├── security/ ├── iam/ ├── compute/ ├── database/ └── monitoring/
I then imported the existing resources into Terraform state and adjusted the configuration until Terraform accurately represented the infrastructure.
One of the most useful moments in the project was reaching:
 No changes. Your infrastructure matches the configuration. 
That result meant Terraform wasn't simply describing what I wanted to create. It was accurately describing infrastructure that was already running.
The production Terraform state is stored remotely in Amazon S3, with the bootstrap configuration kept separate from the production configuration.
I also deliberately kept Terraform's scope limited to the CloudDeploy infrastructure rather than trying to manage every AWS resource in the account.

CI/CD

After the infrastructure was stable, I connected the application deployment to GitHub Actions.
The pipeline is split into CI and production deployment.
GitHub Actions authenticates to AWS using OpenID Connect rather than storing long-lived AWS access keys.
The deployment then uses AWS Systems Manager to execute the deployment commands on EC2.
An important change I made during this phase was making production deployment depend on successful CI.
Previously, deployment could be triggered directly from a push to  main . I changed the workflow so that the deployment waits for the CI workflow to complete successfully.
The deployment also checks out the exact commit SHA that passed CI. This avoids accidentally deploying a different version of the repository.

Monitoring

CloudWatch provides basic operational visibility.
The CloudWatch Agent collects system metrics such as CPU, memory, and disk usage, along with Nginx access and error logs.
There are also alarms for infrastructure health, while FastAPI/Uvicorn logs remain available through the systemd journal.
Again, I kept this intentionally small. The purpose was to learn the basic observability workflow rather than build a full monitoring platform.

What I Learned

The biggest lesson from this project was that Terraform is not just a tool for creating AWS infrastructure.
The difficult part was accurately representing infrastructure that already existed.
I had to understand the relationship between:
Terraform configuration ↓ Terraform state ↓ Actual AWS infrastructure
Importing resources also made me pay much more attention to provider behavior, resource attributes, and the difference between values I can configure and values Terraform computes from AWS.
The CI/CD work reinforced another lesson: a deployment pipeline is more than "run some commands on the server." There should be a clear relationship between the code that was tested and the code that reaches production.

Final Result

CloudDeploy is now a small but complete AWS deployment environment:
FastAPI ↓EC2 + Nginx + systemd ↓RDS PostgreSQL ↓CloudWatch
GitHub ↓CI ↓OIDC ↓AWS ↓SSM ↓EC2
AWS infrastructure ↓Terraform ↓Remote state
The project intentionally remains small.
I could have added more AWS services, but that would have made the architecture more complicated without making it more useful.
For me, the value of CloudDeploy is that I can explain why each major component exists, how the pieces communicate, how the application is deployed, and how the running infrastructure is represented as code.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article