Last Updated on August 27, 2024 by Arnav Sharma
In Terraform, both locals and variables are used to define values, but they serve different purposes and have different behaviors. Understanding the distinction between them is crucial for effective Terraform configuration.
Variables
- Purpose: Variables in Terraform are analogous to function arguments. They are used to customize Terraform modules with different values. This makes your Terraform configurations more dynamic and reusable.
- Definition: Variables are defined in Terraform using the
variable
keyword. They can be set from outside the module (e.g., via command-line flags, environment variables, Terraform Cloud, or theterraform.tfvars
file). - Usage: Variables are used to allow users to input custom values. They are often used for setting up resources that might need different configurations in different environments (like development, staging, production).
- Scope: Variables can be accessed by any resource or module within the same module where they are declared.
Example 1: Defining and Using a Variable
variable “instance_type” {
description = “The type of EC2 instance to create.”
type = string
default = “t2.micro”
}
resource “aws_instance” “example” {
ami = “ami-0c55b159cbfafe1f0”
instance_type = var.instance_type
}
In this example, the instance_type
variable allows users to specify the type of EC2 instance. If not specified, it defaults to t2.micro
.
Example 2: Overriding Variable Values
You can override the default value of a variable in several ways, such as through command-line flags or a terraform.tfvars
file.
Command-line:
terraform apply -var=”instance_type=t2.large”
terraform.tfvars
file:
instance_type = “t2.large”
Locals
- Purpose: Locals (short for “local values”) are used to simplify your Terraform code and reduce repetition. They are like constants or helper variables within a Terraform module.
- Definition: Locals are defined using the
locals
block. They are only accessible within the module where they are defined. - Usage: Locals are useful for creating complex expressions that you need to reuse. For example, you might use a local to construct a naming convention that is used in several resources within a module.
- Scope: Local values are module-scoped and can only be referenced within the module where they are defined. They cannot be set or overridden from outside the module.
Example 1: Defining and Using Locals
locals {
common_tags = {
Owner = “Team A”
Environment = “Prod”
}
}
resource “aws_instance” “example” {
ami = “ami-0c55b159cbfafe1f0”
instance_type = “t2.micro”
tags = local.common_tags
}
In this example, local.common_tags
is used to apply the same set of tags to multiple resources within the module.
Example 2: Simplifying Expressions with Locals
locals {
subnet_prefix = “10.0.${var.environment_id}.0/24”
}
resource “aws_subnet” “example” {
vpc_id = aws_vpc.main.id
cidr_block = local.subnet_prefix
}
Here, local.subnet_prefix
simplifies the creation of a subnet CIDR block based on an environment ID provided as a variable.
Key Differences Illustrated
- Mutability: Variables can be changed externally (e.g., different instance types for different deployments), while locals are fixed once defined (e.g.,
common_tags
used consistently across resources). - Scope: Variables can be passed into modules from parent modules or the Terraform command line. Locals are only accessible within the module where they are defined.
- Purpose: Variables are for user inputs and external configuration. Locals are for internal organization, reducing repetition, and simplifying complex expressions.
When to Use Each
- Use variables when you need to:
- Allow users to customize the configuration.
- Create modules that can be used in different environments with different settings.
- Use locals when you need to:
- Simplify complex expressions that are used multiple times within a module.
- Create a fixed internal value that helps make your configuration more readable and maintainable.
FAQ: Terraform Locals vs Variables
Q: What is Terraform, and how is it used in infrastructure as code?
Terraform is an open-source programming language developed by HashiCorp that allows you to define and provision infrastructure as code. It enables you to manage infrastructure on various cloud platforms such as AWS and Azure through configuration files.
Q: How do you use Terraform locals to name resources?
In Terraform, locals can be assigned to define reusable expressions that can be referenced throughout the configuration files. By using locals to name resources, you can implement a standard naming convention that is consistent throughout your configuration. This makes the code easier to read and maintain.
Q: What is the difference between Terraform locals and input variables?
Terraform locals are values defined within a configuration and are referenced throughout that configuration. They are unlike input variables, which are user-defined and intended to receive values from outside the configuration. Locals are used for assigning reusable values that do not need to be provided by the user, while input variables are used to accept values from the user or another source.
Q: How can you combine Terraform values in a configuration?
To combine Terraform values, you can use locals and input variables within a configuration. Locals allow you to define and reuse values throughout the configuration, while input variables let you pass in values dynamically. This approach helps make the code more modular and reduces duplication.
Q: What is the role of a Terraform module, and how do locals relate to it?
A Terraform module is a container for multiple resources that are used together. Modules can also be used to encapsulate a set of configurations and reuse them in different parts of your infrastructure. Locals defined within a module can be used to manage values in Terraform, ensuring consistency and reducing errors when naming resources or assigning resource tags.
Q: How do you use locals and variables in an Azure DevOps pipeline?
In an Azure DevOps pipeline, you can use Terraform to deploy and manage cloud infrastructure. Locals and input variables can be defined in Terraform configuration files and referenced within the pipeline. This allows you to customize deployments based on different environments and ensure that configurations are consistent across different stages of the workflow.
Q: What is a local value in Terraform and how does it differ from other variables?
A: A local variable in Terraform, often defined within locals.tf
, is used to assign a value that is used throughout the configuration. Unlike input variables, which require external input when terraform runs, local values are calculated and used within the configuration to simplify and optimize resource management.
Q: What is OpenTofu and how is it related to Terraform?
A: OpenTofu is a platform similar to Terraform, used for managing infrastructure as code. Like Terraform, it allows developers to define and manage cloud infrastructure using code, enabling automated and repeatable infrastructure deployment.
Q: How do locals in Terraform differ from input variables?
A: Locals in Terraform are values assigned within the configuration and are used to simplify and optimize resource management. Input variables, on the other hand, require external input, often provided at runtime, and are used to customize the behavior of Terraform configurations.
Q: What is the purpose of using locals versus variables in Terraform?
A: Locals are used to define values that are calculated within the configuration, allowing for cleaner and more efficient code. Variables are typically used for input values provided externally, enabling different configurations based on user input or environment.
Q: How can resource tags be managed using local values in Terraform?
A: Resource tags in Terraform can be managed using local values by defining a set of tags as a local variable. This allows the tags to be reused across multiple resources, ensuring consistency and reducing duplication in the configuration.