How sohaibmohd18/saas-backend-infra-on-aws works
1. This repository is primarily an AWS infrastructure codebase, not a full application stack. Terraform is the main control plane, and the runtime workload is a small containerized sample API used to validate the platform.
2. The infrastructure is split into three layers of Terraform state:
- a one-time backend bootstrap that creates the remote state S3 bucket and DynamoDB lock table,
- environment stacks for dev, staging, and prod,
- reusable modules under terraform/modules for each AWS subsystem.
3. Network architecture is centered on a custom VPC with three tiers of subnets: public, application, and database. The load balancer lives in public subnets, ECS tasks run in private app subnets, and RDS lives in private DB subnets. NAT gateways and optional VPC endpoints are part of the network design, with the number of NATs increasing in higher environments.
4. The request path is simple and explicit: Internet traffic enters an Application Load Balancer, which forwards to ECS Fargate services, which then connect to RDS PostgreSQL. This is the core data plane the architecture diagram should show.
5. Compute is ECS Fargate, managed as a container service rather than EC2 instances. The service supports auto-scaling based on CPU and memory target tracking. Development favors lower-cost settings such as FARGATE_SPOT, while production emphasizes resilience.
6. Persistence is handled by Amazon RDS PostgreSQL 15. The database is private, encrypted, backed up automatically, and configured for Multi-AZ in production. There are no public IPs on ECS or RDS, so all app-to-database traffic stays inside the VPC.
7. Secrets and identity are separated into dedicated modules. Secrets Manager stores database credentials and app secrets using environment-scoped paths, while IAM provides least-privilege roles, including the GitHub Actions OIDC role used for deployment without long-lived AWS keys.
8. The container registry is an AWS ECR repository provisioned by Terraform. The deployment workflow first seeds the repository with a placeholder image, then later GitHub Actions builds the app image and pushes it to ECR for ECS to pull.
9. CI/CD is handled through three GitHub Actions workflows:
- Terraform plan on pull requests,
- Terraform apply on pushes to main, with staging/prod protected by GitHub environment approvals,
- app deployment that builds the container image and updates ECS.
This makes the repo operate as both infrastructure source and deployment pipeline.
10. Monitoring is a first-class subsystem. A CloudWatch dashboard and alarms cover ECS, ALB, and RDS, and alarms notify via SNS email. This gives the architecture both service-health visibility and operational alerting.
11. Environment differences are important to the topology:
- dev is cost-optimized and lighter weight,
- staging adds more production-like networking and sizing,
- prod is the most hardened, with Multi-AZ RDS, deletion protection, and stronger network redundancy.
The diagram should show the same logical shape across all three, with production having the most redundancy.
12. The app/sample-api directory contains a small FastAPI service with /, /health, and /info endpoints. It exists to exercise the infrastructure path end-to-end; it is not the main product logic of the repository.
13. In terms of boundaries, the key separation is:
- Terraform modules define AWS infrastructure primitives,
- environment stacks compose those primitives into deployable environments,
- GitHub Actions orchestrates provisioning and application deployment,
- the sample app is the payload that runs on ECS.
14. A good architecture diagram for this repo should emphasize the following flows:
- developer or CI → Terraform backend/state → AWS resources,
- GitHub Actions → ECR → ECS service,
- Internet → ALB → ECS tasks → RDS,
- CloudWatch alarms → SNS → email.
Those are the main operational and data-flow boundaries in the system.
Main entry points: dev/main.tf Dev stack · staging/main.tf Staging stack · prod/main.tf Prod stack · ecs/main.tf ECS service
Components 17
Terraform
Infrastructure control plane, state bootstrap, and environment composition
- Backend bootstrap · Creates the remote state bucket and lock table for Terraform
terraform/backend/bootstrap/main.tf - Dev stack · Cost-optimized environment composition
terraform/environments/dev/main.tf - Staging stack · Production-like preproduction environment
terraform/environments/staging/main.tf - Prod stack · Hardened production environment with highest redundancy
terraform/environments/prod/main.tf
GitHub Actions
Provisioning and application delivery workflows
- Plan workflow · Runs Terraform plan on pull requests
.github/workflows/terraform-plan.yml - Apply workflow · Runs Terraform apply on main with environment approvals
.github/workflows/terraform-apply.yml - Deploy workflow · Builds the app image, pushes to ECR, and updates ECS
.github/workflows/app-deploy.yml
AWS Runtime
Deployed AWS services and data plane
- VPC · Custom VPC with public, app, and database subnet tiers
terraform/modules/vpc/main.tf - Security groups · Traffic boundaries for ALB, ECS, and RDS
terraform/modules/security/main.tf - Load balancer · Internet-facing entry point in public subnets
terraform/modules/alb/main.tf - ECS service · Container service running private app tasks with auto-scaling
terraform/modules/ecs/main.tf - PostgreSQL · Private PostgreSQL 15 database with backups and Multi-AZ in prod
terraform/modules/rds/main.tf - Container registry · Registry seeded by Terraform and later populated by CI builds
terraform/modules/ecr/main.tf - IAM roles · Least-privilege roles including GitHub Actions OIDC access
terraform/modules/iam/main.tf - Secrets store · Environment-scoped application and database secrets
terraform/modules/secrets/main.tf - Monitoring · Dashboards, alarms, and SNS notifications for ECS, ALB, and RDS
terraform/modules/monitoring/main.tf
Sample App
Containerized FastAPI payload used to exercise the platform
- Sample API · Minimal containerized payload with health and info endpoints
app/sample-api/main.py
Connections 46
- Backend bootstrap → Dev stack: remote state · Environment stack uses the bootstrap-created backend
- Backend bootstrap → Staging stack: remote state · Environment stack uses the bootstrap-created backend
- Backend bootstrap → Prod stack: remote state · Environment stack uses the bootstrap-created backend
- Dev stack → VPC: composes · Dev stack instantiates the shared network foundation
- Dev stack → Security groups: composes · Dev stack wires security boundaries
- Dev stack → Load balancer: composes · Dev stack provisions the entry load balancer
- Dev stack → ECS service: composes · Dev stack provisions the container service
- Dev stack → PostgreSQL: composes · Dev stack provisions the database tier
- Dev stack → Container registry: composes · Dev stack provisions the image repository
- Dev stack → IAM roles: composes · Dev stack provisions roles and policies
- Dev stack → Secrets store: composes · Dev stack provisions secrets access and storage
- Dev stack → Monitoring: composes · Dev stack provisions observability resources
- Staging stack → VPC: composes · Staging stack instantiates the shared network foundation
- Staging stack → Security groups: composes · Staging stack wires security boundaries
- Staging stack → Load balancer: composes · Staging stack provisions the entry load balancer
- Staging stack → ECS service: composes · Staging stack provisions the container service
- Staging stack → PostgreSQL: composes · Staging stack provisions the database tier
- Staging stack → Container registry: composes · Staging stack provisions the image repository
- Staging stack → IAM roles: composes · Staging stack provisions roles and policies
- Staging stack → Secrets store: composes · Staging stack provisions secrets access and storage
- Staging stack → Monitoring: composes · Staging stack provisions observability resources
- Prod stack → VPC: composes · Prod stack instantiates the shared network foundation
- Prod stack → Security groups: composes · Prod stack wires security boundaries
- Prod stack → Load balancer: composes · Prod stack provisions the entry load balancer
- Prod stack → ECS service: composes · Prod stack provisions the container service
- Prod stack → PostgreSQL: composes · Prod stack provisions the database tier
- Prod stack → Container registry: composes · Prod stack provisions the image repository
- Prod stack → IAM roles: composes · Prod stack provisions roles and policies
- Prod stack → Secrets store: composes · Prod stack provisions secrets access and storage
- Prod stack → Monitoring: composes · Prod stack provisions observability resources
- Load balancer → ECS service: forwards · ALB routes inbound requests to ECS services
- ECS service → PostgreSQL: queries · Tasks connect privately to PostgreSQL
- Deploy workflow → Container registry: pushes image · Workflow builds and publishes the application container image
- Deploy workflow → ECS service: updates service · Workflow deploys the new task definition or service revision
- Plan workflow → Dev stack: plans · Pull request workflow validates Terraform changes
- Apply workflow → Dev stack: applies · Main branch apply provisions or updates the dev environment
- Apply workflow → Staging stack: applies · Main branch apply provisions or updates the staging environment
- Apply workflow → Prod stack: applies · Main branch apply provisions or updates the prod environment
- Apply workflow → IAM roles: assumes role · GitHub Actions uses OIDC-based deployment access
- Deploy workflow → IAM roles: assumes role · Deployment workflow uses OIDC-based AWS access
- Deploy workflow → Sample API: builds · Workflow packages the FastAPI service into a container image
- Sample API → ECS service: runs on · Sample API is the payload executed by ECS tasks
- Monitoring → ECS service: watches · CloudWatch alarms track service health and scaling
- Monitoring → Load balancer: watches · CloudWatch alarms track load balancer health and traffic
- Monitoring → PostgreSQL: watches · CloudWatch alarms track database availability and performance
- Monitoring → Secrets store: alerts via · Alarms notify through SNS email subscriptions