Skip to content

Lab 13: Terraform IaC

Field Value
Tier 3 — Operations
Estimated Time 60 minutes
Prerequisites Terraform, Docker
Auto-Grade Yes

Scenario

Your infrastructure team has been provisioning resources manually through cloud consoles. There is no version control, no reproducibility, and no audit trail. Last quarter, someone accidentally deleted a production database because they clicked the wrong button in the AWS console. The VP of Engineering has mandated infrastructure-as-code for all new resources.

You are starting with a local exercise using Terraform's Docker provider. You need to create a reusable module that provisions a Docker network and three containers (web, API, database) with proper configuration. The infrastructure must be fully described in Terraform code, use variables for customization, output useful values, and manage state correctly.

Objectives

  • Initialize a Terraform project with the Docker provider
  • Create a module app-stack that provisions a Docker network + 3 containers
  • Use variables for image tags, container names, and network name
  • Define outputs for container IDs and network ID
  • Run terraform plan successfully with no errors
  • Run terraform apply to create all resources
  • Verify all three containers are running

Setup

./setup.sh

Creates a Terraform project skeleton at /tmp/lab-terraform/.

Hints

Hint 1: Docker provider
terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "~> 3.0"
    }
  }
}
provider "docker" {}
Hint 2: Module structure Create `modules/app-stack/` with `main.tf`, `variables.tf`, and `outputs.tf`. Call it from the root module with `module "stack" { source = "./modules/app-stack" }`.
Hint 3: Docker network resource
resource "docker_network" "app" {
  name = var.network_name
}
Hint 4: Docker container resource
resource "docker_container" "web" {
  name  = var.web_name
  image = docker_image.nginx.image_id
  networks_advanced {
    name = docker_network.app.name
  }
}
Hint 5: State management For this lab, local state is fine. The state file `terraform.tfstate` is created automatically. Never commit state files to git.

Grading

./grade.sh

Solution

See the solution/ directory for complete Terraform configuration.