Infrastructure as code & deployment targets (CDK, CloudFormation, Fargate, EC2, Cloudflare Tunnel)
Why IaC, CDK vs CloudFormation vs Terraform, VPCs and private subnets, ALB + Fargate, RDS with generated secrets, EC2 + SSM, and exposing apps through Cloudflare Tunnel with no inbound ports.
Why infrastructure as code
Clicking through the AWS console can’t be reviewed, repeated or rolled back. IaC defines servers, networks, databases and permissions as code, so it’s versioned, reviewed in PRs, reproducible for staging and production, and documents itself.
| Tool | Language | Notes |
|---|---|---|
| AWS CloudFormation (IR3D) | YAML/JSON | Native AWS; verbose |
| AWS CDK (Hostel) | TypeScript/Java/Python, compiled to CloudFormation | High-level constructs, loops, types |
| Terraform / OpenTofu | HCL | Multi-cloud; huge ecosystem; state file to manage |
| Pulumi | General-purpose languages | Multi-cloud CDK-like |
| render.yaml / fly.toml | YAML | PaaS blueprints (your course uses render.yaml) |
Typical AWS architecture (Hostel)
Internet -> Route53/DNS -> ALB (public subnets, HTTPS via ACM cert, HTTP->HTTPS redirect)
-> ECS Fargate tasks (PRIVATE subnets, 2 vCPU / 4 GB)
-> RDS Postgres 18 (private, credentials in Secrets Manager)
-> SSM parameters for app secrets
hostel-backend/infrastructure/lib/infrastructure-stack.tsconst db = new rds.DatabaseInstance(this, 'Db', {
engine: rds.DatabaseInstanceEngine.postgres({ version: rds.PostgresEngineVersion.VER_18 }),
credentials: rds.Credentials.fromGeneratedSecret('hostel'), // Secrets Manager, never seen by humans
vpc, vpcSubnets: { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS },
});
new ecsPatterns.ApplicationLoadBalancedFargateService(this, 'Api', {
cpu: 2048, memoryLimitMiB: 4096,
taskSubnets: { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS },
circuitBreaker: { rollback: true },
certificate, // ACM wildcard *.hostellifeplus.com
redirectHTTP: true,
taskImageOptions: { secrets: { JWT_SECRET: ecs.Secret.fromSsmParameter(jwtParam) /* ... */ } },
});One high-level construct creates the ALB, target group, listeners, security groups, ECS service, task definition and log group.
deploy/infra.yaml · docker-compose.prod.yml · fetch-env.sh · deploy-prod.sh · cloud-cutover.shCloudFormation for ECR, an S3 deploy bucket, EC2, IAM (SSM + KMS) and RDS. The app runs with docker-compose on one EC2 instance (app + RabbitMQ + cloudflared). Logs ship to CloudWatch via the awslogs driver. Deploys arrive through SSM send-command, so there’s no SSH. cloud-cutover.sh documents the migration into this setup.
docker-compose.yaml (cloudflared, TUNNEL_TOKEN)SperoTek (and IR3D in production) uses Cloudflare Tunnel. cloudflared makes an outbound connection to Cloudflare, and Cloudflare routes api.yourdomain through it. The server has no inbound ports open, so there’s nothing to port-scan or DDoS directly, TLS is handled by Cloudflare, and no load balancer is needed. That’s ideal for small budgets.
Watchtower + docker-compose on a serverSimplest of all: a VM running compose, with Watchtower pulling new images.
Choosing a target
| Target | Cost / effort | Scaling | Your projects |
|---|---|---|---|
| PaaS (Render, Railway, Fly.io, Heroku) | Lowest effort | Click or auto | The course site |
| Single VM + compose (+ Watchtower or Tunnel) | Cheap, simple | Vertical | RM, IR3D |
| ECS Fargate + ALB | Medium; no servers to patch | Horizontal | Hostel |
| Kubernetes (EKS, GKE) | High | Very flexible | Bonus module |
| Serverless (Lambda, Cloud Run) | Pay per request | Auto, scales to zero | Needs fast startup (native image) |
Related concepts you must know
- VPC basics. Public vs private subnets, NAT gateways (expensive), security groups (stateful firewalls) vs NACLs.
- DNS. A, AAAA and CNAME records. Your course:
backend.leroylabs.devCNAME points toyoursite.onrender.com. - TLS certificates. ACM, Let’s Encrypt, Cloudflare edge certs.
- Least-privilege IAM per service.
- Cost awareness. NAT gateways, idle RDS and data egress are common surprise bills.