Module variables and outputs
Hard-coded values in a Terraform module prevent reuse across environments. Input variables and output values let you parameterize module behavior and share resource attributes between modules — making it practical to build standardized infrastructure components that teams can deploy consistently.
Overview
Input variables and output values are the two mechanisms that make Terraform modules reusable and composable. Input variables let you customize a module's behavior without touching its source code. Output values expose key resource attributes so other modules or root configurations can consume them.
Together, they establish a clean separation between what a module needs (inputs) and what it produces (outputs). This makes it practical to build standardized, environment-agnostic infrastructure components — network modules, security group modules, compute modules — that different teams can reuse with confidence across environments and regions.
Module variables
Hard-coded values in a module make it rigid: every caller gets the same configuration with no way to override it. If you instantiate the same module twice with hard-coded names, you get a name-collision error at terraform plan or terraform apply.
Input variables solve this by letting callers supply values at call time, keeping the module's logic consistent while making its configuration flexible.
Importance of variables
Input variables are particularly valuable for managing differences across environments. A common pattern is to use smaller instance types in staging environments and larger instance types in production — the same module, different variable values.
Use input variables to parameterize modules
Step 1: Replace hard-coded values
Replace any hard-coded values with references to input variables. For example, to parameterize the VPC name:
# Before: hard-coded value
resource "alicloud_vpc" "main" {
vpc_name = "my-vpc"
}
# After: parameterized with a variable
resource "alicloud_vpc" "main" {
vpc_name = var.vpc_name
}
Step 2: Declare variables
Declare each input variable in variables.tf, placed in the same directory as the resource definitions. A complete variable declaration includes three core attributes:
# variables.tf
variable "vpc_name" {
description = "Name of the VPC"
type = string
}
|
Attribute |
Description |
|
|
Documents the variable's purpose for module callers |
|
|
Enforces the expected data type (string, number, bool, list, map, object) |
|
|
Makes the variable optional; omit to require an explicit value from callers |
Step 3: Pass a value when you call a module
Pass values for input variables in the module block:
module "vpc_dev" {
source = "./modules/vpc"
vpc_name = "dev-vpc"
}
module "vpc_prod" {
source = "./modules/vpc"
vpc_name = "prod-vpc"
}
Unlike root configurations, module input variables cannot be set interactively when Terraform runs — all values must be passed in the module block.
Module output values
To pass a resource attribute from one module to another, declare it as an output value in the source module. Without an output value, the attribute remains private to that module.
Why output values are needed
Share resource attributes (IDs, connection strings, IP addresses) across modules
Expose only the information downstream modules actually need
Express explicit dependencies between modules so Terraform applies them in the right order
Define and use output values
Define output values in the source module
# modules/vpc/outputs.tf
output "vpc_id" {
description = "ID of the created VPC"
value = alicloud_vpc.main.id
}
output "vswitch_ids" {
description = "IDs of the created vSwitches"
value = alicloud_vswitch.vswitches[*].id
}
Use output values in the destination module
# modules/ecs/main.tf
module "vpc" {
source = "../vpc"
}
resource "alicloud_instance" "example" {
vswitch_id = module.vpc.vswitch_ids[0]
}
Example
Communication between the network module and the server module
The network module outputs its VPC ID, and the ECS module takes it as an input variable. Terraform resolves this dependency automatically.
# modules/network/main.tf
resource "alicloud_vpc" "main" {
vpc_name = var.vpc_name
cidr_block = var.vpc_cidr
}
# modules/network/outputs.tf
output "vpc_id" {
value = alicloud_vpc.main.id
}
# modules/ecs/variables.tf
variable "vpc_id" {
description = "ID of the VPC"
type = string
}
# root main.tf
module "network" {
source = "./modules/network"
vpc_name = "example-vpc"
vpc_cidr = "172.16.0.0/16"
}
module "ecs" {
source = "./modules/ecs"
vpc_id = module.network.vpc_id
}
Multi-environment configuration
Use .tfvars files to supply environment-specific values without changing the module. Apply each environment's configuration by passing the corresponding file to terraform apply:
# environments/dev.tfvars
environment = "dev"
instance_type = "ecs.g6.small"
instance_count = 1
# environments/prod.tfvars
environment = "prod"
instance_type = "ecs.g6.2xlarge"
instance_count = 3
# Apply with a specific environment
terraform apply -var-file="environments/dev.tfvars"
Best practices
-
Variable design
Declare a variable for every configuration item that callers may need to customize
Provide sensible default values so the module works out of the box for the common case
Add
validationblocks to catch invalid inputs early, before resources are createdWrite clear, specific descriptions — callers rely on them to understand what each variable controls
-
Output design
Expose only the attributes that downstream modules actually need — keep internal details private
Add descriptions to output values so callers understand what each output represents
Think about downstream consumers when designing outputs — output what they need, not what's convenient
-
Environment management
Use Terraform workspaces to isolate state between environments
Maintain separate
.tfvarsfiles for each environment (dev, staging, prod)Use conditional expressions in variable defaults or locals to handle environment-specific differences
-
Usage notes
Module input variables cannot be set interactively at runtime — pass all values in the module block
Run
terraform initafter modifying a moduleEnsure the type of the value you pass matches the declared type of the variable