Cloud Computing, DevOps

3 Mins Read

Simplify your Code using Terraform Locals

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.

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.

Pioneers in Cloud Consulting & Migration Services

  • Reduced infrastructural costs
  • Accelerated application deployment
Get Started

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.

Get your new hires billable within 1-60 days. Experience our Capability Development Framework today.

  • Cloud Training
  • Customized Training
  • Experiential Learning
Read More

About CloudThat

CloudThat is also the official AWS (Amazon Web Services) Advanced Consulting Partner and Training partner and Microsoft gold partner, helping people develop knowledge of the cloud and help their businesses aim for higher goals using best in industry cloud computing practices and expertise. We are on a mission to build a robust cloud computing ecosystem by disseminating knowledge on technological intricacies within the cloud space. Our blogs, webinars, case studies, and white papers enable all the stakeholders in the cloud computing sphere.

Drop a query if you have any questions regarding Terraform, Terraform Locals and I will get back to you quickly.

To get started, go through our Consultancy page and Managed Services Package that is CloudThat’s offerings.

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!