Back to course

Variables: Don't Repeat Yourself (DRY)

Infrastructure as Code (Terraform & OpenTofu Mastery)

Dynamic Infrastructure

If you want to change your server size, you shouldn't edit the main code. Use variables.

1. Define the variable in variables.tf:

hcl variable "instance_type" { type = string default = "t2.micro" description = "The size of the server" }

2. Use it in main.tf:

hcl resource "aws_instance" "my_server" { ami = "ami-xyz" instance_type = var.instance_type }