Terraform by Example


Variables

Terraform variables are used to control behavior of resources. It’s a common practice to declare an empty variable and set its value during execution.

Variables can be set in many different ways (e.g., through a .tfvars file, environment variables, command line flags, etc).

It’s a common practice to declare your variables in a separate vars.tf file.

Declaring a variable #

variable "max_instance_count" {
  type = number
}

Declaring a variable with a default value #

If a default value is present, the variables is considered to be optional and the default value will be used if no value is set.

variable "max_instance_count" {
  type = number
  default = 3

Declaring a variable with a default value and validation #

You can create validation rules for a variable by adding a validation block.

variable "max_instance_count" {
  type = number
  default = 3

  validation {
    condition     = var.max_instance_count <= 5
    error_message = "max_instance_count should be less than or equal to 5!"
  }
}

Using a variable in a module #

To use a variable in a module, you need to use var.<variable name>

variable "bucket_name" {
  type        = string
  description = "The name of the S3 bucket"
}

resource "aws_s3_bucket" "default" {
  bucket = var.bucket_name
}

Calling a module using input variables #

Variables act as input variables for a module, this allows other users to set their own values for a variable.

module "s3" {
  source = "path/to/s3-module"
  bucket_name = "my-bucket"
}

Want to learn more about Variables? Check out the docs.

Continue to Locals