AcademyWorld Builder: Infrastructure as CodeDynamic Structures (Variables)

Module 3: Dynamic Architectures

Hard-coding names (like "terraform-mission-bucket") is fine for a single mission. but in the real world, you'll need to build the same infrastructure for different environments—Development, Staging, and Production.

Tactical Intel: Input Variables

Variables allow you to make your blueprints flexible. Instead of writing a new file for every environment, you just pass in a different variable.

Step 1: Defining the Variable

We keep our variables in a separate file to keep things organized.

Mission: Create a file named variables.tf and add this code:

variable "bucket_name" {
  description = "The name of the mission bucket"
  type        = string
  default     = "dynamic-mission-bucket"
}

Step 2: Referencing the Variable

Now, we need to tell our main.tf to use this variable instead of the hard-coded name.

Mission: Open main.tf and change the bucket name line to: bucket = var.bucket_name

booting...

Tactical Insight: The variable prefix

In Terraform, we access variables using the var. prefix. This tells the engine to look in your variable definitions for the value.

Mission Control

Define an Input Variable

Expected Command

grep 'variable' variables.tf

Link Variable to Resource