Sunday, 29 June 2025

Terraform Global Object

 📌 What is a Terraform Global Object? 

A global object in Terraform refers to a built-in symbol that you can access anywhere in your configuration without explicitly declaring it. These objects provide context, metadata, or access to other modules, resources, or Terraform settings. 

 

🧱 Explanation of module (Global Object) 

module.<MODULE_NAME>.<OUTPUT_NAME> 
 

  • module: This is the global object in Terraform that holds all instantiated modules. 

  • <MODULE_NAME>: The name you give your module block (like vpc, eks, rds, etc.). 

  • <OUTPUT_NAME>: A value defined using an output block inside that module. 

Example 

module "vpc" { 
 source = "./modules/vpc" 
} 
 
output "my_vpc_id" { 
 value = module.vpc.vpc_id 
} 
 

In this case: 

  • module is the global object. 

  • vpc is the instance name of the module. 

  • vpc_id is defined as an output in the modules/vpc code. 

 

🌍 List of Terraform Global Objects 

Here are all major global objects that you can use anywhere in Terraform: 

Global Object 

Description 

Module 

 

Access outputs of child modules. Example: module.vpc.vpc_id 

Terraform 

 

Access Terraform settings and metadata (e.g., backend workspace). Example: terraform.workspace 

Var 

 

Access input variables. Example: var.environment 

Local 

 

Access named local values defined in the current module. Example: local.tags 

Path 

 

Access paths related to the module. Example: path.module 

Count 

 

Used inside count-enabled resources to refer to the index. Example: count.index 

Each 

 

Used inside for_each to access key/value. Example: each.key, each.value 

Self 

 

 

Refers to the current resource being created. Used in dynamic blocks or nested resource configuration. 

Data 

 

Access data sources. Example: data.aws_ami.ubuntu.id 

Resource 

 

Access other resources if needed (used in rare advanced scenarios). 

 

🔎 Examples 

Use Case 

Example 

Variable reference 

 

var.region 

Module output 

 

module.eks.cluster_name 

Local value 

 

local.tags["Environment"] 

Terraform workspace 

 

terraform.workspace 

Path of module 

 

path.module 

Count index 

 

count.index 

For_each loop value 

 

each.value.name 

 

✅ Summary Table 

Object 

Example Use 

Description 

var 

var.instance_type 

Input variables 

local 

local.env_name 

Local values inside a module 

module 

module.vpc.vpc_id 

Output values from a module 

terraform 

terraform.workspace 

Terraform's current workspace 

path 

path.module, path.root 

Path metadata (current file/module) 

data 

data.aws_region.current.name 

Data source references 

count 

count.index 

Current index in a count loop 

each 

each.key, each.value 

Current item in a for_each loop 

self 

self.name (inside dynamic) 

Current resource inside nested block 

 

No comments:

Post a Comment