Learn how to easily retrieve Terraform module attribute values using the versatile Terraform console.
In Terraform, modules are self-contained units of infrastructure code that promote reusability and organization. When you use modules, you often need to access the outputs they produce. These outputs can represent values like resource IDs, IP addresses, or any other data that your modules make available.
To access module outputs in Terraform, you use the following syntax:
module.<MODULE_NAME>.<OUTPUT_NAME>
Example:
Let's say you have a module named "networking" with an output "subnet_ids":
module "networking" {
source = "./modules/networking"
# ... other module inputs
}
output "subnet_ids" {
value = module.networking.subnet_ids
}
Explanation:
module.<MODULE_NAME>
: This part targets the specific module you want to access. Replace <MODULE_NAME>
with the actual name of your module (e.g., "networking").
.<OUTPUT_NAME>
: This part specifies the output value you want to retrieve. Replace <OUTPUT_NAME>
with the name of the output defined within your module (e.g., "subnet_ids").
Important Notes:
terraform refresh
or terraform apply
to get the latest state information before trying to access module outputs.terraform console
to experiment with accessing module outputs interactively.This Terraform code defines a networking module that creates a VPC and two subnets. The module outputs the subnet IDs. The root module calls the networking module and accesses the subnet IDs output, demonstrating how to use module outputs in Terraform.
1. Networking Module (./modules/networking/main.tf):
# Configure a VPC
resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr
enable_dns_hostnames = true
enable_dns_support = true
tags = {
Name = "main-vpc"
}
}
# Configure two subnets
resource "aws_subnet" "public_subnet" {
vpc_id = aws_vpc.main.id
cidr_block = var.public_subnet_cidr
availability_zone = var.availability_zone
tags = {
Name = "public-subnet"
}
}
resource "aws_subnet" "private_subnet" {
vpc_id = aws_vpc.main.id
cidr_block = var.private_subnet_cidr
availability_zone = var.availability_zone
tags = {
Name = "private-subnet"
}
}
# Expose subnet IDs as output
output "subnet_ids" {
value = [aws_subnet.public_subnet.id, aws_subnet.private_subnet.id]
}
2. Root Module (main.tf):
# Define variables for the networking module
variable "vpc_cidr" {
default = "10.0.0.0/16"
}
variable "public_subnet_cidr" {
default = "10.0.1.0/24"
}
variable "private_subnet_cidr" {
default = "10.0.2.0/24"
}
variable "availability_zone" {
default = "us-west-2a"
}
# Use the networking module
module "networking" {
source = "./modules/networking"
vpc_cidr = var.vpc_cidr
public_subnet_cidr = var.public_subnet_cidr
private_subnet_cidr = var.private_subnet_cidr
availability_zone = var.availability_zone
}
# Access and output the subnet IDs from the networking module
output "subnet_ids" {
value = module.networking.subnet_ids
}
Explanation:
output
block.module
block. It then accesses the subnet_ids
output from the networking module using module.networking.subnet_ids
and displays it using an output
block.This example demonstrates how to define outputs in a module and access them from the root module. You can then use these outputs in other parts of your Terraform code. Remember to run terraform apply
to create the resources and make the outputs available.
By understanding and effectively using module outputs, you can create well-structured, reusable, and maintainable Terraform code for your infrastructure deployments.
This table summarizes how to access outputs from Terraform modules:
Feature | Description | Example |
---|---|---|
Syntax | module.<MODULE_NAME>.<OUTPUT_NAME> |
module.networking.subnet_ids |
module.<MODULE_NAME> |
Targets the specific module by its name. |
module.networking refers to a module named "networking". |
.<OUTPUT_NAME> |
Specifies the desired output value defined within the targeted module. |
.subnet_ids retrieves the "subnet_ids" output. |
Root Module Outputs | Only outputs defined in the root module are accessible outside of their defining module. | Outputs defined within the "networking" module are only accessible within that module unless also defined in the root module. |
Terraform Refresh | Run terraform refresh or terraform apply to update state information before accessing outputs. |
Ensures you are accessing the latest output values. |
Debugging | Use terraform console for interactive experimentation with module output access. |
Allows you to test and verify output access. |
Accessing outputs from Terraform modules is essential for building reusable and well-structured infrastructure code. By using the module.<MODULE_NAME>.<OUTPUT_NAME>
syntax, you can retrieve values from your modules and utilize them in other parts of your Terraform configurations. Remember to define outputs in the root module for external access and keep your state information up-to-date. By mastering module outputs, you enhance the modularity, maintainability, and scalability of your Terraform projects.