Code on screen

Last Updated on February 17, 2024 by Arnav Sharma

Terraform has revolutionized the way DevOps engineers and data scientists manage and provision infrastructure. With its unique Hashicorp Code Language (HCL), Terraform offers a robust coding structure, making it a popular infrastructure tool. Among its myriad features, Terraform Local Values, often termed as “locals,” stand out as a pivotal component. This Saturn Cloud blog aims to provide an in-depth understanding of Terraform Local Values, their declaration, usage, best practices, and practical examples with Azure.

What Exactly is a Terraform Local Value?

In the vast sea of Terraform configurations, a local value is like a beacon. It assigns a name to an expression, allowing users to reference the name multiple times within a module. This eliminates the need to repeat the expression, making the Terraform code more concise and readable. Drawing a parallel with traditional programming languages, Terraform modules can be likened to function definitions:

  • Input variables equate to function arguments.
  • Output values are analogous to function return values.
  • Local values can be compared to a function’s temporary local variables.

For simplicity, local values are often termed as “locals” when the context makes the meaning evident.

Azure-Specific Examples with Terraform Locals

Azure Virtual Network Name Composition: Suppose you have a naming convention for your Azure Virtual Networks that combines the environment, region, and a static string.

locals {
  env      = "prod"
  region   = "westus"
  vnet_name = "${local.env}-${local.region}-vnet"
}

Using the local value in an Azure Virtual Network resource:

resource "azurerm_virtual_network" "example" {
  name                = local.vnet_name
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
}

Azure Resource Group Naming: Creating a local value for Azure Resource Group name:

locals {
  rg_name = "tf-rg-${local.env}"
}

Using the local value in an Azure Resource Group:

resource "azurerm_resource_group" "example" {
  name     = local.rg_name
  location = "West US"
}

Azure Storage Account Name Composition: Azure storage account names need to be unique across Azure and can only contain lowercase letters and numbers. Using locals can help generate such names.

locals {
  sa_prefix = "tfstg"
  sa_suffix = "001"
  sa_name   = "${local.sa_prefix}${local.sa_suffix}"
}

Using the local value in an Azure Storage Account:  

resource "azurerm_storage_account" "example" {
  name                     = local.sa_name
  resource_group_name      = azurerm_resource_group.example.name
  location                 = azurerm_resource_group.example.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
}

Key Takeaways and Best Practices

Terraform locals are a powerful tool for simplifying Terraform configurations. They enhance code readability and reduce repetition. With the added Azure-specific examples, you can now see how locals can be effectively used in real-world scenarios, making your Terraform configurations more modular and maintainable. As you continue your journey with Terraform, remember to use locals judiciously. Combine them effectively with other Terraform components for optimal results, and always keep an eye on the Terraform state to ensure consistency.

Harnessing the Power of Terraform Locals

The use of local values can be helpful to avoid repeating the same values and expressions multiple times within a Terraform configuration. While they are a powerful tool, if overused, they can also make a configuration hard to read by future maintainers by hiding the actual values used. Therefore, it’s essential to strike a balance. As you delve deeper into the world of IaC and Terraform, locals will undoubtedly become one of your go-to tools to streamline and optimize your configurations.

FAQ – Local Variables in Terraform

Q: What are local variables in Terraform?

A: Local variables in Terraform are variables that are defined within a Terraform configuration file and are used to store and reuse values. They can be used to assign dynamic values, perform calculations, or simplify the configuration code.

Q: How do I use local variables in Terraform?

A: To use local variables in Terraform, you need to define them within a local block in your Terraform configuration file. You can then reference these variables throughout your configuration code using the local.variable_name syntax.

Q: What is the benefit of using local variables in Terraform?

A: The key advantage of using local variables in Terraform is that they allow you to simplify your configuration code and make it more readable. They also help in reducing duplication and promoting reusability of values within your configuration.

Q: How are local variables different from input variables in Terraform?

A: Local variables in Terraform are defined and used within the same Terraform configuration file, while input variables are passed externally to the Terraform configuration file at runtime. Local variables are useful within a single configuration file, while input variables are used to pass values between different modules or configurations.

Q: Can I use local values in Terraform for resource tags?

A: Yes, local values in Terraform can be used for defining resource tags. You can define a local variable to store the desired tags and then reference the local variable when creating resources. This allows you to reuse the same set of tags across multiple resources.

Q: How do I access environment variables in Terraform?

A: To access environment variables in Terraform, you can use the interpolation syntax by prefixing the environment variable name with “env.”. For example, to access the value of the “MY_VARIABLE” environment variable, you would use “${env.MY_VARIABLE}”.

Q: Can I use Terraform locals with module outputs?

A: Yes, you can use Terraform locals with module outputs. You can define a local variable to store the value returned by a module output, and then reference this local variable wherever you need to use the module output value within your configuration code.

Q: What is the recommended way to use local values in Terraform code?

A: The recommended way to use local values in Terraform is to define them within a separate variables.tf file and declare them as variables using the locals {} block. This helps in organizing and managing your local variables separately from the rest of your configuration code.

Q: Can Terraform locals be used for variable substitution within resource blocks?

A: Yes, Terraform locals can be used for variable substitution within resource blocks. You can define a local variable that holds a value and then reference this local variable within the resource block using the interpolation syntax, using the local.variable_name syntax.

Q: How do I use local values with Terraform state?

A: Local values in Terraform are not stored in the Terraform state. They are evaluated during the Terraform plan and apply process and their values are used within the configuration. If you need to store values in the Terraform state, you should use Terraform variables instead of local values.

Keywords: infrastructure as code variable values data source tfvars file learn how to use configuration language use cases in terraform doesn’t compare terraform contant data scientists and devops engineers

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Toggle Dark Mode