Generic Terraform infrastructure for the Ramin NestJS and React starter application.
- CloudFront serves a private S3 frontend and proxies
/api/*to an ALB. - A CloudFront Function handles SPA routes without rewriting API errors.
- The ALB accepts only CloudFront origin-facing addresses and requires a generated origin header.
- ECS Fargate tasks run in two private subnets and reach external services through one NAT gateway.
- Cognito provides authorization-code-with-PKCE authentication for the SPA.
- Secrets Manager stores the MongoDB URI; Terraform creates only the empty secret container.
- A second private S3 bucket supports application file storage.
- Optional Route 53, ALB, ECS, CloudWatch, and SNS monitoring is disabled by default.
The single NAT gateway is a deliberate starter-cost choice and is not Availability Zone redundant. Production applications can move to one NAT gateway per Availability Zone when that availability/cost tradeoff is justified. Non-current frontend object versions expire after 30 days; uniquely named immutable assets remain available so an older browser shell never points at a deleted deployment asset.
Requirements: Terraform 1.7 or newer and AWS credentials only when running plan or apply.
scripts/setup-local.sh
scripts/verify.shscripts/verify.sh formats, initializes without a backend, validates, and runs mocked-provider Terraform tests. It does not contact or modify AWS.
Local state is the default onboarding mode and is ignored by Git. For shared or production work:
- Create an S3 state bucket outside this stack with encryption, versioning, public access blocking, and restricted IAM access.
- Copy
backend.tf.exampleto ignoredbackend.tf. - Copy
backend.hcl.exampleto ignoredbackend.hcland set the bucket, key, and region. - Run
terraform init -reconfigure -backend-config=backend.hcl.
The S3 backend example uses native lockfiles. State is sensitive because it contains the generated CloudFront-to-ALB verification value. Do not commit state, plans, real tfvars, or backend configuration.
- Set project, environment, owner, and exact GitHub repository values in
terraform.tfvars. - If GitHub's OIDC provider already exists in the AWS account, set
github_oidc_provider_arn. Only one provider with that URL can exist per account. - Run
scripts/verify.sh, then reviewterraform plan. - Apply the stack only after approval. The ECS service starts at zero tasks, so the placeholder image is never pulled.
- Put the MongoDB URI into the output secret outside Terraform. Never pass it through a Terraform variable.
- Copy
github_backend_repository_variablesandgithub_frontend_repository_variablesinto their matching GitHub repositories or environments. - Manually run the backend deployment, then the frontend deployment.
The deployment workflows own the live ECS task-definition revision and desired count. Terraform intentionally ignores those two ECS service fields after bootstrap so a later infrastructure apply cannot roll an application release backward or scale it to zero. Terraform still owns the task-definition baseline, IAM, networking, environment variables, and secret references; backend deployments inherit the latest baseline and replace only its image.
Set monitoring_enabled = true to create endpoint health checks, ALB/ECS alarms, and SNS email subscriptions. Confirm each SNS subscription email before expecting alerts. When the application region is not us-east-1, Route 53 health-check alarms and their SNS topic live in us-east-1; regional ALB/ECS alarms use a regional topic. Monitoring, NAT, ALB, and other resources incur recurring AWS charges.
Buckets retain data by default and can block terraform destroy. Empty them deliberately first. Setting allow_destroy_nonempty_buckets = true is an explicit destructive opt-in and should not be a routine production setting. Secrets use a seven-day recovery window.
Custom domains, ACM, Route 53 hosted zones, WAF, MongoDB Atlas provisioning, Bedrock, autoscaling, and Terraform apply workflows are intentionally outside this starter stack.