Followers

Terraform - Locals

  1. Terraform Locals  Before we try to understand the Terraform locals take a step back and try to compare terraform locals with a normal  ...

 

Before we try to understand the Terraform locals take a step back and try to compare terraform locals with a normal local variables declared inside the function, so any local variable present inside the function will only be accessible within that function similarly to the terraform locals are available within -

  1. Local terraform module
  2. Within the terraform configuration

Value assignment- Terraform locals do not change their value once it is assigned, you have to re-assign a new value.

Power of Expression- Apart from static value assignment terraform locals can use the power of expression, so instead of writing the same expression the multiple times through the terraform configuration, you can declare a terraform locals and use the same terraform locals at other places.

Example with expression -

 locals {
  my_local = "${var.ec2_instance_name}"
}
BASH

Example without expression -

 locals {
  my_local = "t2.micro"
}
BASH


If you are working on a large enterprise infrastructure then it is impossible to work without Terraform Locals. But let's see the benefits of using terraform locals in a bit more detail -

  1. Terraform locals can be re-used multiple numbers of times in the terraform configuration file.
  2. It can reduce the work of updating your terraform configuration at multiple places. With terraform locals you need to update its value once and it should reflect all over the place where it is referred.

Let's create our first Terraform local and in this terraform local we will store the Tag name for the AWS EC2 instance.

locals {
  staging_env = "staging"
}
BASH

As you can see in the above syntax we have created a tag name Staging for the EC2 instance. And we're going to use the same local throughout our Terraform configuration.

We use the terraform locals for -

  1. Putting a tag on aws_subnet
  2. Putting a tag on aws_instance for ec2

Here is my Terraform configuration for my AWS environment

BASH
variables.tf
variable "instance_type" {
  type = string
  default = "t2.micro"
}
variable "ami" {
  type = string
  default ="ami-05fa00d4c63e32376"
}
variable "key_name" {
  type = string
  default = "newkey"
}
main.tf
provider "aws" {
  region ="us-east-1"
}
//interpolation
locals {
  mylocal = "prod"
}
resource "aws_instance" "ins1" {
  ami = var.ami
  instance_type = var.instance_type
  key_name = var.key_name
  tags = {
     "Name" = "${local.mylocal}-env"
  }
}

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 -

variable.tf

variable "instance_type" {
  type = string
  default = "t2.micro"
}
variable "ami" {
  type = string
  default ="ami-05fa00d4c63e32376"
}
variable "key_name" {
  type = string
  default = "newkey"
}
variable "region" {
  type = string
  default = "us-east-1"
}
variable "location" {
  type = string
  default = "India"
}
variable "env" {
    type = string
    default = "stag"
 
}

main.tf

BASH
provider "aws" {
  region ="us-east-1"
}
//interpolation
locals {
  mylocal = "${var.env}-Location-${var.location}"
}
resource "aws_instance" "ins1" {
  ami = var.ami
  instance_type = var.instance_type
  key_name = var.key_name
  tags = {
     "Name" = "${local.mylocal}"
  }
}

COMMENTS

Name

Ansible,6,AWS,1,Azure DevOps,1,Containerization with docker,2,DevOps,2,Docker Quiz,1,Docker Swarm,1,DockerCompose,1,ELK,2,git,2,git quiz,1,Git Worksheet,1,ITIL,1,ITSM,1,Jira,3,Kubernetes,1,Kubernetes Quiz,5,SAST DAST Security Testing,1,SDLC Quiz,5,SonarQube,3,Splunk,2,vagrant kubernetes,1,YAML Basics,1,
ltr
item
DevOpsWorld: Terraform - Locals
Terraform - Locals
DevOpsWorld
https://www.devopsworld.co.in/2022/09/terraform-locals.html
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/2022/09/terraform-locals.html
true
5997357714110665304
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content