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.
Experiencing error " Can't access attributes on a primitive-typed ... | I am using terraform vpc module to create vpcs and subnets and I have written a module for adding routes “aws_route” as it has to be used multiple times across my code. When i tried to pass the route table ids to from terraform module to one of the variables in my module i am experiencing error " Error: Unsupported attribute. Can’t access attributes on a primitive-typed value (string)." I am new to terraform and not sure what exactly the meaning of this error and what needs to be done to get rid...
Terraform Output Values : Complete Guide & Examples | What are Terraform outputs? See the different options for their configuration and how Terraform handles and exports output values between modules.
Is there any way to inspect module variables and outputs? - Terraform | Pretty self explanatory I guess. Is there any way to do what’s described in the title? terraform state show and terraform state list only target resources. Can I inspect the values of module variables and outputs somehow? This would be extremely handy for debugging purposes.
Customize Terraform configuration with variables | Terraform ... | Input variables make your Terraform configuration more flexible by defining values that your end users can assign to customize the configuration.
Output is not published by a root module - HCP Terraform ... | Hello, it appears that in order to obtain an “output” value from the root module, I have to define it (output) twice. Once in the root and then in the calling module. Is this an expected behavior and why? I’d think that publishing in the root level should be sufficient… main.tf: module "vm_key" { source = "../../../modules/AWS/comp/keys" ... } output "vm_key" { value = module.vm_key.key_name } root module ( …/…/…/modules/AWS/comp/keys/keys.tf) : resource "aws_key_pair" "vm_key" { .....