Cloud Computing, DevOps

3 Mins Read

Simplify your Code using Terraform Locals

Voiced by Amazon Polly

Overview

Terraform is an Infrastructure as Code (IAC) tool allowing users to manage their infrastructure declaratively. It defines the infrastructure resources using a domain-specific language (DSL) called HCL (HashiCorp Configuration Language).

Terraform allows variables to make the code more dynamic, but sometimes these variables can become too complex or redundant, especially if you need to reference the same value multiple times. In this case, Terraform locals can help.

Pioneers in Cloud Consulting & Migration Services

  • Reduced infrastructural costs
  • Accelerated application deployment
Get Started

What are Terraform Locals?

Terraform locals allow you to define variables only visible within the current module, making reusing values or expressions throughout the code easier. Locals can be defined as static values or expressions, or they can reference other variables or resources within the module.

Syntax of Terraform Locals: Locals are defined using the locals block,

which can contain any number of variables or expressions.

In the above example, we have defined three locals: variable1, variable2, and expression. The first two are simple static values, while the third reference the values of variable1 and variable2 using interpolation syntax.

Why use Terraform Locals?

Terraform locals offer several benefits, including:

Improved Readability: Terraform locals allow you to assign meaningful names to values, making your code more readable and easier to understand.

Reduced Duplication: Terraform locals allow you to define a value once and use it throughout your code. This reduces duplication and makes your code more maintainable.

Reusability: Terraform locals can be defined in the root module and used throughout your code, including in child modules. This makes it easy to reuse values across your codebase.

Let’s look at some examples of how Terraform locals can be used.

Defining Local Variables

In this example, we define several local variables that will be used throughout our Terraform configuration.

We have defined several local variables in this example, including the AWS region, VPC name, subnet CIDR block, and instance type. These variables can be used throughout our code to create resources.

Using Local Variables in Resource Creation

In this example, we use the local variables defined in the previous example to create an AWS VPC and subnet.

In this example, we use the local variables vpc_name and subnet_cidr to set the VPC name and subnet CIDR block, respectively. We also use the region local variable to set the availability zone for the subnet.

Using Local Variables in Child Modules

This example defines a child module that creates an EC2 instance. We use local variables to pass in the instance type and subnet ID.

Limitations of Terraform Locals

There are a few limitations of Terraform locals to be aware of, including:

Visibility: Locals are only visible within the current module, so you cannot use them in other modules or resources.

Scope: Locals are only visible within the current Terraform configuration, so you cannot use them across different configurations.

Re-evaluation: Locals are re-evaluated every time Terraform runs, which can affect performance if you have a lot of complex expressions.

Conclusion

Terraform locals can be a powerful tool for simplifying your code and making it more modular and readable. By defining variables only visible within the current module, you can reuse values and expressions without repeating them multiple times. However, it is important to be aware of the limitations of locals, such as their limited scope and re-evaluation during each Terraform run.

Freedom Month Sale — Discounts That Set You Free!

  • Up to 80% OFF AWS Courses
  • Up to 30% OFF Microsoft Certs
Act Fast!

About CloudThat

CloudThat is an award-winning company and the first in India to offer cloud training and consulting services worldwide. As a Microsoft Solutions Partner, AWS Advanced Tier Training Partner, and Google Cloud Platform Partner, CloudThat has empowered over 850,000 professionals through 600+ cloud certifications winning global recognition for its training excellence including 20 MCT Trainers in Microsoft’s Global Top 100 and an impressive 12 awards in the last 8 years. CloudThat specializes in Cloud Migration, Data Platforms, DevOps, IoT, and cutting-edge technologies like Gen AI & AI/ML. It has delivered over 500 consulting projects for 250+ organizations in 30+ countries as it continues to empower professionals and enterprises to thrive in the digital-first world.

FAQs

1. Can locals be modified during runtime?

ANS: – No, locals are defined in the root module and cannot be modified during runtime. They are evaluated during the Terraform apply phase and cannot be updated during that process. If you need to update a value, modify the local variable in your code, and run terraform apply again.

2. Can locals be used across multiple Terraform configurations?

ANS: – No, locals are specific to the Terraform configuration in which they are defined. They cannot be used across multiple Terraform configurations. However, you can define a local module and use it across multiple configurations, providing a similar level of reuse.

3. Can locals be used within modules?

ANS: – Yes, locals can be used within modules. You can pass local variables to child modules using input variables.

WRITTEN BY Rekha S

Share

Comments

    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!