Deploys a highly available web application on AWS using an Application Load Balancer (ALB) and Auto Scaling Group (ASG). Each EC2 instance serves a static HTML page via a lightweight Python HTTP server.
Internet
│
▼
[ALB] (port 80)
│
├── EC2 Instance (port 8080)
├── EC2 Instance (port 8080)
└── EC2 Instance (port 8080) ← scales between 2–10
Resources provisioned:
aws_security_group— separate security groups for the ALB and EC2 instances (instances only accept traffic from the ALB)aws_launch_template— latest Amazon Linux 2 AMI, bootstrapped with a user-data script that serves an HTML pageaws_autoscaling_group— rolling instance refresh, ELB health checks, spread across all default VPC subnetsaws_lb— internet-facing Application Load Balanceraws_lb_target_group— HTTP health checks against/aws_lb_listener— forwards port 80 traffic to the target group
State is stored remotely in S3 (day6-terraform-state-bucket) with DynamoDB locking (terraform-state-locks) in eu-west-2.
- Terraform >= 1.0
- AWS credentials configured (environment variables,
~/.aws/credentials, or IAM role) - An S3 bucket named
day6-terraform-state-bucketand a DynamoDB table namedterraform-state-locksalready exist ineu-west-2
# Initialise (downloads providers and configures the S3 backend)
terraform init
# Preview changes
terraform plan
# Apply
terraform apply
# Destroy all resources
terraform destroyAfter a successful apply, the ALB DNS name is printed as an output. Open it in a browser to reach the web cluster.
| Name | Description | Default |
|---|---|---|
server_port |
Port the web server listens on | 8080 |
alb_port |
Port the ALB listener accepts traffic on | 80 |
instance_type |
EC2 instance type | t2.micro |
asg_min_size |
Minimum instances in the ASG | 2 |
desired_size |
Desired instances in the ASG | 5 |
asg_max_size |
Maximum instances in the ASG | 10 |
Override any variable at apply time:
terraform apply -var="instance_type=t3.small" -var="desired_size=3"| Name | Description |
|---|---|
alb_dns_name |
DNS name of the Application Load Balancer |
| Setting | Value |
|---|---|
| Backend | S3 |
| Bucket | day6-terraform-state-bucket |
| Key | global/s3/terraform.tfstate |
| Region | eu-west-2 |
| Locking | DynamoDB — terraform-state-locks |
| Encryption | Enabled |