How to use Terraform locals?

Working the [Terraform][3] it iss 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 do not 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 am trying to create a terraform local in which I am going to Define The server name so in the future if there is a change in the server name so I do not 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"

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

 2provider "aws" {
 3   region     = "eu-central-1"
 4   access_key = "AKIATQ37NXB2G2LXXXXX"
 5   secret_key = "r1oaShokKPw+YY7qaHxj8mD2T8BpxRUVXXXXXXXX"
 8locals {
 9  staging_env = "staging"
12resource "aws_vpc" "staging-vpc" {
13  cidr_block = ""
15  tags = {
16    Name = "${local.staging_env}-vpc-tag"
17  }
20resource "aws_subnet" "staging-subnet" {
21  vpc_id =
22  cidr_block = ""
24  tags = {
25    Name = "${local.staging_env}-subnet-tag"
26  }
29resource "aws_instance" "ec2_example" {
31   ami           = "ami-0767046d1677be5a0"
32   instance_type = "t2.micro"
33   subnet_id =
35   tags = {
36           Name = "${local.staging_env} - Terraform EC2"
37   }

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"
1variable server_name {
2  description = "Name of server"
3  type        = string
4  default     = "primary-app-server"

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}"

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 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?
  11. How to use Terraform resource meta arguments?
  12. How to use Terraform Data sources?
  13. What is terraform provisioner?
  14. Terraform how to do SSH in AWS EC2 instance?
  15. How Terraform modules works?
  16. How to run specific terraform resource?
  17. How to use Workspaces in Terraform?
  18. Securing AWS secrets using HashiCorp Vault with Terraform?
  19. Managing Terraform states?
  20. Terraform state locking using DynamoDB (aws_dynamodb_table)