Roadmaps / DevOps & Cloud / Lab 4

Lab 4 of 8 · Infrastructure as Code with Terraform

Run your app with Terraform

Use Terraform to build your image and run it as a container, then change and destroy it, all from code.

Time
45–60 minutes
Cost
Free: everything runs on your computer with Docker.

What you learn

Instead of clicking around a console, you describe infrastructure in files, review it like code, and let a tool create, change and delete it. The same workflow works for Docker, AWS, Azure and Google Cloud.

  • Providers, resources and state
  • The init → plan → apply → destroy cycle
  • Reading a plan before you change anything
  • Why state files must never go into Git

In the AI era: AI tools write Terraform quickly, and sometimes write it wrong. Reading `terraform plan` carefully before you apply is the habit that keeps AI-written infrastructure safe.

Before you start

  • Finish lab 2 and make sure Docker is running.

  • Install Terraform 1.6 or newer. Check with terraform version.

Step by step

  1. 1 Describe the infrastructure

    This file tells Terraform to build your Dockerfile into an image and run a container from it on port 8080.

    File: infra/main.tf
    terraform {
      required_version = ">= 1.6"
      required_providers {
        docker = {
          source  = "kreuzwerker/docker"
          version = "~> 3.0"
        }
      }
    }
    
    provider "docker" {}
    
    resource "docker_image" "app" {
      name = "ai-status-app:terraform"
      build {
        context = "${path.module}/.."
      }
    }
    
    resource "docker_container" "app" {
      name  = "ai-status-terraform"
      image = docker_image.app.image_id
      ports {
        internal = 8000
        external = var.host_port
      }
    }
    
  2. 2 Add a variable and an output

    Variables make the code reusable; outputs print useful facts after an apply.

    File: infra/variables.tf
    variable "host_port" {
      description = "Port on your computer that reaches the app"
      type        = number
      default     = 8080
    }
    
    output "url" {
      value = "http://localhost:${var.host_port}/health"
    }
    
  3. 3 Initialise and plan

    init downloads the Docker provider. plan shows what would change, without changing anything. Read it: Terraform will add 2 resources.

    Run this
    cd infra
    terraform init
    terraform plan
    You should see:

    Plan: 2 to add, 0 to change, 0 to destroy.

  4. 4 Apply

    Type yes when asked. Terraform builds the image and starts the container.

    Run this
    terraform apply
    curl "$(terraform output -raw url)"
    You should see:

    Apply complete! Resources: 2 added and then {"status":"ok"}.

  5. 5 Change it with code

    Move the app to port 9090 and look at the plan: Terraform replaces the container because a port cannot be changed in place.

    Run this
    terraform apply -var="host_port=9090"
    curl http://localhost:9090/health
    You should see:

    The plan says must be replaced, then the app answers on 9090.

  6. 6 Destroy everything

    Cleaning up is part of the job, and in the cloud it is what stops the bill.

    Run this
    terraform destroy
    You should see:

    Destroy complete! Resources: 2 destroyed.

  7. 7 Commit the code, not the state

    State files can contain secrets, so they stay out of Git. Teams keep state in a locked remote backend instead.

    Run this
    cd ..
    printf '.terraform/\n*.tfstate\n*.tfstate.*\n' >> .gitignore
    git add infra .gitignore
    git commit -m "Add Terraform for the app container"
    git push
    You should see:

    GitHub shows infra/main.tf, infra/variables.tf and infra/.terraform.lock.hcl, but no .tfstate file.

You are done when

  • You ran init, plan, apply and destroy and can say what each one does.

  • You read a plan that replaced a resource and understood why.

  • Your repository has the Terraform files and no state files.

Finished? Get your proof page

We check your public GitHub repository and give you a page to share with recruiters, and can add the project to your resume.