How to use Terraform locals?



Working the [Terraform][3] it's always fun but it is easier when you know all the concepts very well.

There is one more concept Terraform locals which comes very handy when you are working with very large terraform configuration files.

Terraform locals are quite similar to Terraform variables but Terraform locals don’t change their value. On the other hand, if you talk about Terraform input variables then it is dependent on user input and it can change its value.

So if you have a very large Terraform file where you need to use the same values or expressions multiple times then Terraform local can be useful for you.

If you look at the following diagram, here I’m trying to create a terraform local in which I’m going to Define The server name so in the future if there is a change in the server name so I don’t need to change the server name at all places but instead I can just update my locals.

Table of Content

  1. Create your first Terraform Local
  2. Combine terraform local with terraform variable


1. Create your first Terraform Local

Let’s create our first Terraform local and in this terraform local we are going to Define the server name we’re going to deploy or where we are going to apply our terraform configuration.

1locals {
2  staging_env = "staging"
3}

As you can see in the above syntax we have created a local environment. And we’re going to use the same local throughout our Terraform configuration

Here is my Terraform configuration for my AWS environment

 1
 2provider "aws" {
 3   region     = "eu-central-1"
 4   access_key = "AKIATQ37NXB2G2LXXXXX"
 5   secret_key = "r1oaShokKPw+YY7qaHxj8mD2T8BpxRUVXXXXXXXX"
 6}
 7
 8locals {
 9  staging_env = "staging"
10}
11
12resource "aws_vpc" "staging-vpc" {
13  cidr_block = "10.5.0.0/16"
14
15  tags = {
16    Name = "${local.staging_env}-vpc-tag"
17  }
18}
19
20resource "aws_subnet" "staging-subnet" {
21  vpc_id = aws_vpc.staging-vpc.id
22  cidr_block = "10.5.0.0/16"
23
24  tags = {
25    Name = "${local.staging_env}-subnet-tag"
26  }
27}
28
29resource "aws_instance" "ec2_example" {
30   
31   ami           = "ami-0767046d1677be5a0"
32   instance_type = "t2.micro"
33   subnet_id = aws_subnet.staging-subnet.id
34   
35   tags = {
36           Name = "${local.staging_env} - Terraform EC2"
37   }
38}
39
40 

2. Combine terraform local with terraform variable

now we know how to use terraform local the next thing which we are going to try is to combine terraform local along with Terraform variable.

First, let’s create a few terraform variables -

1variable location {
2  description = "Location of server"
3  type        = string
4  default     = "finland"
5}
1variable server_name {
2  description = "Name of server"
3  type        = string
4  default     = "primary-app-server"
5}

Now let’s create a terraform local, in which we are going to combine the two variables which we have created just above

1locals {
2    server_details = "${var.location}-${var.server_name}"
3}

And now we can use this local inside our terraform configuration where we are going to define the server detail.

Best practices for using locals

  1. You should use terraform locals excessively inside your Terraform configuration
  2. Always keep in mind to use terraform local where you think that value is going to be changed in the future.
  3. Always think of Terraform Locals as a central place for storing configuration values.

You can read more on about Terraform Locals over here


Read More -

  1. Install terraform on Ubuntu 20.04, CentOS 8, MacOS, Windows 10, Fedora 33, Red hat 8 and Solaris 11
  2. How to setup Virtual machine on Google Cloud Platform using terraform
  3. Create EC2 Instance on AWS using terraform
  4. How to use Terraform Input Variables
  5. What is variable.tf and terraform.tfvars?
  6. How to use Terraform locals?
  7. How to use Terraform output values?
  8. Understanding terraform count, for_each and for loop?
  9. Cloud-nuke : How to nuke AWS resources and save additional AWS infrastructure cost?
  10. How to use Terraform Dynamic blocks?