Search⌘ K
AI Features

Variables

Explore how to declare variables in Terraform and use them to make configurations dynamic. Understand variable syntax, passing runtime values, setting descriptions, and how Terraform prompts for inputs during apply or destroy actions. This lesson helps you manage variable-driven infrastructure effectively.

Terraform variables

A variable in Terraform is something that can be set at runtime. It allows you to vary what Terraform will do by passing in or using a dynamic value.

Our first Variable

Let’s dive into an example of how to use variables so we can learn how they work:

C++
provider "aws" {
region = "us-east-2"
}
variable "bucket_name" {
description = "the name of the bucket you wish to create"
}
resource "aws_s3_bucket" "bucket" {
bucket = var.bucket_name
}

You declare a variable by using the keyword variable, and then specify the identifier for the variable in quotes. We are using "bucket_name" as the identifier. Inside the variable block, we add a description to describe to the user what ...