Skip to content
Welcome To Charanjit Cheema Blog

Welcome To Charanjit Cheema Blog

An Open Source and Cloud Blog

Menu
  • Home
  • About Me!
  • Way to my Technical Blog
  • Contact me
  • Privacy Policy
Menu

What Are Terraform Local and External Variables? Explained with Examples

Posted on November 11, 2025 by Charanjit Singh

When I first started writing Terraform code, I remember how confusing variables felt in the beginning. I had variable blocks everywhere some defined locally, some coming from outside, and I used to wonder which one takes priority and how Terraform actually decides the value. Over time, as I built real-world automation, I realized understanding variables is not just a Terraform concept it’s the foundation of writing clean, scalable, and reusable Infrastructure as Code.

Let’s take a simple Terraform setup to understand this better.

# variables.tf
variable "region" {
  description = "AWS region where resources will be created"
  type        = string
  default     = "ap-south-1"
} 

Here, the variable region is defined locally within the Terraform configuration. This is known as a local variable block because it’s defined and used within the same Terraform working directory. The default value makes sure Terraform still works even if you don’t explicitly pass the variable at runtime.

Now, let’s assume you want to override this region value without touching your code. This is where external variables come into play. Terraform allows you to pass variables externally through different methods for example, using .tfvars files, environment variables, or command line arguments.

Let’s say you have a terraform.tfvars file like this:

region = "us-east-1" 

When you run terraform plan, Terraform will automatically pick up this external file and override the default value of region from “ap-south-1” to “us-east-1”. This is how external variable blocks help keep your infrastructure configuration reusable across different environments such as: dev, uat, or prod.

In one of my projects, we had to deploy EC2 instances in multiple regions using the same Terraform codebase. Instead of maintaining separate .tf files, I used external variable files such as dev.tfvars, uat.tfvars, and prod.tfvars, each containing specific configurations. With a simple command like:

terraform apply -var-file="prod.tfvars" 

Terraform would automatically pick all the environment specific values from that file and build the infrastructure accordingly.

Now, there’s another layer to understand local variables (locals) in Terraform are not the same as variables defined in variables.tf. Locals are more like computed variables within your code. They help simplify expressions and avoid duplication. For example:

locals {
  instance_name = "${var.env}-app-server"
} 

Here, the local variable instance_name takes input from another variable env and generates a new value dynamically. You can use local.instance_name anywhere in your configuration for example, as the name tag of an EC2 instance.

So, in short, variables.tf defines configurable parameters that can be overridden externally, while locals define internal reusable expressions to simplify the code.

Here’s how both work together in a real world scenario:

# variables.tf
variable "env" {
  type        = string
  description = "Environment name"
  default     = "dev"
}

# locals.tf
locals {
  bucket_name = "my-${var.env}-bucket"

  bucket_tags = {
    Name        = local.bucket_name
    Environment = var.env
  }
}

# main.tf
resource "aws_s3_bucket" "my_bucket" {
  bucket = local.bucket_name
  tags   = local.bucket_tags
} 

In this code example, I have also used a local variable block for bucket tags. This makes tag management simple and consistent across multiple resources. Instead of repeating tags in every resource block, defining them once inside locals ensures any change reflects everywhere. For instance, if I update the environment or bucket name, the tags automatically adjust. This approach keeps the code clean, modular, and easy to extend when you add more resources later.

As I mentioned above, If I apply this with a different environment using an external file like:

# prod.tfvars
env = "prod" 

and run:

terraform apply -var-file="prod.tfvars" 

Terraform will automatically create a bucket named “my-prod-bucket” with tags like Name = my-prod-bucket and Environment = prod. The same code, with different variable inputs, now works seamlessly across multiple environments.

That’s the real power of understanding the difference between local variables and external variables it helps make your Terraform code flexible, environment independent, and easy to maintain.

Over time, as you start managing larger infrastructure, this clarity helps you design cleaner modules, reuse components, and handle multiple environments without rewriting code. I’ve learned that writing Terraform isn’t about syntax it’s about designing logic that scales well.

So the next time you write a Terraform variable block, remember: Locals make your code simpler whereas external variables make it reusable.

Loading

  • Author
  • Recent Posts
Charanjit Singh
Follow him
Charanjit Singh
Charanjit is currently working as a Cloud Architect at Mphasis, with 18 years of experience in IT infrastructure projects, implementation, and support. While his main role is as a DevOps engineer, he holds a Cloud Architect position and has strong skills in cloud technologies and automation. His expertise includes Terraform, AWS, Azure DevOps, Azure Cloud, VMware, and Linux systems.

Charanjit is passionate about automating tasks and improving processes. He uses tools like Terraform and Azure DevOps to build and manage cloud infrastructure and streamline deployment. He also enjoys using Shell scripts and Ansible playbooks to make systems run more efficiently.

In his free time, Charanjit enjoys learning about new technologies and sharing his knowledge through his blog. When he’s not working, he likes listening to music, having a cup of coffee, and relaxing in nature.

You can connect with Charanjit on Twitter, Facebook, LinkedIn, or email him at charanjit.cheema@cjcheema.com.
Charanjit Singh
Follow him
Latest posts by Charanjit Singh (see all)
  • What Are Terraform Local and External Variables? Explained with Examples - November 11, 2025
  • How to Deploy Docker Containers with NGINX on AWS EC2 Using Ansible and GitHub Actions - April 26, 2025
  • No More DynamoDB! Use Native S3 locking for Terraform State - February 7, 2025

Like this:

Like Loading...

Related

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Tags

AWS Cloud Computing Dockers Networking Open Networking OpenSource RHEL-CentOS SDN Server Hardware SLES tcpdump Ubuntu WSL

Follow me @

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 2 other subscribers

Recent Posts

  • What Are Terraform Local and External Variables? Explained with Examples
  • How to Deploy Docker Containers with NGINX on AWS EC2 Using Ansible and GitHub Actions
  • No More DynamoDB! Use Native S3 locking for Terraform State
  • How to Bring and Manage Manually Created AWS Resources Under Terraform Management
  • Iterating Cloud Resource Provisioning Using Terraform Count and For_Each Meta-Arguments

Recent Comments

  1. Charanjit Singh on Terraform and Ansible Collaboration for AWS Cloud Deployment
  2. christinatodd2020aeaa798563 on Terraform and Ansible Collaboration for AWS Cloud Deployment
  3. Charanjit Singh on How to Set password policy in CentOS or RHEL system
  4. SAURABH on How to recover or rebuild initramfs in CentOS 7 Linux
  5. Sangita on How to Set password policy in CentOS or RHEL system

Archives

  • November 2025
  • April 2025
  • February 2025
  • January 2025
  • August 2024
  • July 2024
  • June 2024
  • January 2024
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • September 2022
  • August 2022
  • July 2020
  • May 2020
  • February 2020
  • November 2019
  • June 2019
  • May 2019
  • March 2019
  • February 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • June 2018
  • May 2018
  • April 2018

Categories

  • Automation
  • Cloud Computing
  • Coding
  • CyberSecurity
  • Networking
  • OpenSource
  • RHEL-CentOS
  • Server Hardware
  • SLES
  • Technical Blog
  • Ubuntu
  • WSL

Blog Stats

  • 18,353 hits
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy
  • Home
  • About Me!
  • Way to my Technical Blog
  • Contact me
  • Privacy Policy
© 2025 Welcome To Charanjit Cheema Blog | Powered by Superbs Personal Blog theme
%d