Terraform Tutorial — Practice Projects Exercises

1 minute read

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:

  1. aws_lb — the Application Load Balancer
  2. aws_lb_target_group — where to send traffic
  3. aws_lb_listener — listen on port 80
  4. aws_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
  1. aws_db_subnet_group — group your private subnets for RDS
  2. aws_security_group for RDS — allow port 5432 from the web server SG
  3. aws_db_instance — the RDS instance in the private subnet group
  4. 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
  1. Create a GitHub repo named terraform-aws-vpc (must follow terraform-<PROVIDER>-<NAME> convention)
  2. Include main.tf, variables.tf, outputs.tf, versions.tf, and README.md
  3. Tag a release (e.g., v1.0.0)
  4. Sign in to registry.terraform.io with GitHub
  5. Click “Publish” and select your repository
  6. The registry automatically reads your module structure and documentation

Chapter Summary

In this chapter, you built five projects:

  1. S3 Static Website — bucket, policy, website configuration
  2. EC2 Web Server — instance, security group, user data
  3. VPC Network — subnets, IGW, NAT, route tables
  4. Module Refactor — extract VPC into reusable module
  5. Multi-Environment — workspaces + tfvars for dev/prod

Next up: Next Steps — your learning roadmap beyond the basics.


← Project 5: Multi-Environment Next Steps →