Terraform Tutorial — Practice Projects Exercises
Extension challenges to push your skills further.
Exercise 1: Add CloudFront to Project 1
Task: Extend the S3 static website project to serve content through CloudFront CDN. What resources do you need to add?
Answer
Add these resources:
resource "aws_cloudfront_distribution" "website" {
origin {
domain_name = aws_s3_bucket_website_configuration.website.website_endpoint
origin_id = "S3-Website"
custom_origin_config {
http_port = 80
https_port = 443
origin_protocol_policy = "http-only"
origin_ssl_protocols = ["TLSv1.2"]
}
}
enabled = true
default_root_object = "index.html"
default_cache_behavior {
allowed_methods = ["GET", "HEAD"]
cached_methods = ["GET", "HEAD"]
target_origin_id = "S3-Website"
viewer_protocol_policy = "redirect-to-https"
forwarded_values {
query_string = false
cookies { forward = "none" }
}
}
restrictions {
geo_restriction { restriction_type = "none" }
}
viewer_certificate {
cloudfront_default_certificate = true
}
}
output "cloudfront_url" {
value = "https://${aws_cloudfront_distribution.website.domain_name}"
}
Exercise 2: Add an ALB to Project 2
Task: Instead of accessing the EC2 instance directly, put it behind an Application Load Balancer. What resources do you need?
Answer
You need four resources:
aws_lb— the Application Load Balanceraws_lb_target_group— where to send trafficaws_lb_listener— listen on port 80aws_lb_target_group_attachment— register the EC2 instance
You also need to update the security group to allow traffic from the ALB instead of directly from the internet.
Exercise 3: Add RDS to Project 3
Task: Add a PostgreSQL RDS instance in the private subnets of your VPC. What additional resources do you need?
Answer
aws_db_subnet_group— group your private subnets for RDSaws_security_groupfor RDS — allow port 5432 from the web server SGaws_db_instance— the RDS instance in the private subnet group- A sensitive variable for the database password
Exercise 4: Publish Your Module
Task: Describe the steps to publish the VPC module from Project 4 to the Terraform Registry.
Answer
- Create a GitHub repo named
terraform-aws-vpc(must followterraform-<PROVIDER>-<NAME>convention) - Include
main.tf,variables.tf,outputs.tf,versions.tf, andREADME.md - Tag a release (e.g.,
v1.0.0) - Sign in to registry.terraform.io with GitHub
- Click “Publish” and select your repository
- The registry automatically reads your module structure and documentation
Chapter Summary
In this chapter, you built five projects:
- S3 Static Website — bucket, policy, website configuration
- EC2 Web Server — instance, security group, user data
- VPC Network — subnets, IGW, NAT, route tables
- Module Refactor — extract VPC into reusable module
- Multi-Environment — workspaces + tfvars for dev/prod
Next up: Next Steps — your learning roadmap beyond the basics.