2022-10-12

Variables vs. Locals in Terraform

Introduction

This article explains the difference between Variable and Locals in Terraform.

Variables in Terraform

In Terraform, Variables are used to provide configurable inputs to your Terraform modules. This allows elements of your module to be customized without altering the module's own source code and thus enables the reuse of modules without modification.

Limitations in Definition

Unlike other programming languages, variables in Terraform do not allow conditional expressions during their definition.

variable "image_id" {
  type = string
}

Overwriting External Variables

Variables defined in Terraform can be overwritten externally, providing flexibility in adjusting the values based on different environments or conditions.

Setting Values in Variables

Variables in Terraform can be assigned values in several ways.

Interactive Input during apply Execution

When running the terraform apply command, Terraform will interactively prompt for input for any variables that have not been assigned a value.

$ terraform apply

var.image_id
  Enter a value:

Specifying Options via the Command Line (-var, -ver-file)

You can also provide variable values directly on the command line with the -var or -var-file options.

$ terraform apply -var="image_id=ami-abc123"

Specifying Values in the terraform.tfvars File

Values for variables can be defined in a terraform.tfvars file. Terraform automatically loads this file, if it exists.

image_id = "ami-abc123"

Specifying Values using Environment Variables (TF_VAR_xxx)

Terraform will also read environment variables named TF_VAR_name to find the value for a variable.

bash
$ export TF_VAR_image_id=ami-abc123
$ terraform apply

Setting Default Values during Variable Definition

Finally, you can specify a default value directly in the variable definition, which will be used if no other value is provided.

variable "image_id" {
  type    = string
  default = "ami-abc123"
}

Locals in Terraform

Local values, commonly known as "locals," in Terraform, are named expressions that can be used as a convenience to avoid repeating the same values or expressions multiple times in a configuration.

Conditional Expression Usage during Definition

In contrast to variables, locals allow the use of conditional expressions (ternary operators) during their definition.

locals {
  count = var.is_prod ? 1 : 0
}

Non-overwritable from External Sources

Unlike variables, locals cannot be overwritten from external sources. This means that they can provide stability and predictability within your Terraform configurations.

Ability to Describe Multiple Times

Locals can be defined multiple times in your configuration, each with different values.

locals {
  server_name   = "hoge"
  server_core   = 1
  server_memory = 2
}

Use Case of Variables and Locals

Utilizing Locals in tf Files

When handling variables within tf files, it is usually preferable to use locals as they provide stability and reduce repetition in your configurations. For example, if you have a common value or expression used in multiple places in your configuration, you can define it once as a local and then reference the local value instead.

locals {
  common_value = "shared_among_resources"
}

resource "aws_instance" "example" {
  ami           = local.common_value
  instance_type = "t2.micro"
}

resource "aws_ebs_volume" "example" {
  availability_zone = "us-west-2a"
  size              = 40
  tags = {
    Name = local.common_value
  }
}

Using Variables when External Input is Required

However, in cases where you need to input values from external sources, variables become indispensable.

variable "instance_type" {
  description = "The instance type to use for our EC2 Instance"
  type        = string
  default     = "t2.micro"
}

resource "aws_instance" "example" {
  ami           = "ami-abc123"
  instance_type = var.instance_type
}

Recommendations for the Use of Variables and Locals

Locals are suitable for organizing complex expressions and reusing common values, while Variables are suitable when you want to customize the configuration externally.

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!