
Host a Static Site on EC2 with Terraform (VPC, Optional ALB)
Host a Static Site on EC2 with Terraform (VPC, Optional ALB, Session Manager) For static sites, S3 + CloudFront is usually the better default. This post points at a small Terraform demo and pulls a few excerpts from main.tf , variables.tf , iam.tf , and user_data.tftpl . Full layout: tf-aws-ec2-static-demo (local path ~/workspace/jdevto/tf-aws-ec2-static-demo if you keep it beside this blog repo). S3 + CloudFront with Terraform: art0018 . Overview The demo provisions a VPC, nginx on Amazon Linux 2023 , index.html (AZ + private IP from IMDSv2 ), and robots.txt . use_alb=false (default): one instance in one public subnet; clients hit :80 on the instance public IP (CIDR from allowed_http_cidr ). use_alb=true : internet ALB across az_count ≥ 2 public subnets; az_count instances in private subnets (one per AZ), NAT for egress, no instance public IP; instances register to one target group with HTTP / health check expecting 200 ; instance SG allows :80 from the ALB SG and from the VPC CIDR .
Continue reading on Dev.to
Opens in a new tab



