Variables

View saved

Declare a variable

Save as variables.tf:

variable "greeting" {
  type        = string
  description = "Text written to the file"
  default     = "Hello from Terraform"
}

Use it in a resource

Reference with var.NAME.

resource "local_file" "hello" {
  filename = "${path.module}/hello.txt"
  content  = "${var.greeting}\n"
}

Set values

Defaults work for demos. Override via CLI or a terraform.tfvars file.

terraform apply -var="greeting=Ship it"

# terraform.tfvars
# greeting = "Ship it"

Keep secrets out of git

Use variables for sensitive values and mark them sensitive = true. Prefer environment variables or a secrets tool for real credentials.

Comments

One comment per signed-in account. Comments are saved with this page’s URL.