Module outputs in Terraform are essential for sharing data between modules and the root configuration. When you create a module, you can define outputs that expose specific values from resources created within that module, making them accessible to the calling configuration.
To access module outpu…Module outputs in Terraform are essential for sharing data between modules and the root configuration. When you create a module, you can define outputs that expose specific values from resources created within that module, making them accessible to the calling configuration.
To access module outputs, you use the syntax: module.<MODULE_NAME>.<OUTPUT_NAME>
First, you need to define outputs in your child module. In the module's outputs.tf file, you declare output blocks:
output "instance_id" {
description = "The ID of the EC2 instance"
value = aws_instance.example.id
}
output "public_ip" {
description = "The public IP address"
value = aws_instance.example.public_ip
}
Once the module is called in your root configuration, you can reference these outputs elsewhere. For example:
module "web_server" {
source = "./modules/ec2"
instance_type = "t2.micro"
}
resource "aws_eip" "example" {
instance = module.web_server.instance_id
}
output "server_ip" {
value = module.web_server.public_ip
}
Key points about module outputs:
1. Outputs must be explicitly defined - only values declared as outputs are accessible outside the module.
2. You can use module outputs as inputs to other modules, creating dependencies between modules.
3. The 'sensitive' argument can mark outputs containing confidential data, preventing them from displaying in logs.
4. Output values are computed after terraform apply completes for that module.
5. You can use the 'depends_on' meta-argument in output blocks when implicit dependencies are insufficient.
6. Module outputs can be complex types including lists, maps, and objects.
Accessing module outputs enables modular architecture where components communicate through well-defined interfaces, promoting reusability and maintainability in your Terraform configurations.
Accessing Module Outputs in Terraform
Why It's Important
Module outputs are essential for creating reusable, composable infrastructure code. They allow modules to expose specific values to the calling (parent) module, enabling communication between different parts of your Terraform configuration. Understanding how to access module outputs is crucial for building modular, maintainable infrastructure and is a key topic on the Terraform Associate exam.
What Are Module Outputs?
Module outputs are values that a child module exposes to its parent module. Think of them as the return values of a function. When you call a module, you can access these outputs to use values created within that module elsewhere in your configuration.
Outputs are defined within a module using the output block:
output "instance_ip" { value = aws_instance.example.public_ip description = "The public IP of the instance"}
How to Access Module Outputs
To access an output from a child module, use the following syntax:
module.<MODULE_NAME>.<OUTPUT_NAME>
Example:
If you have a module block named "web_server" that outputs "instance_ip", you would access it as:
module.web_server.instance_ip
Complete Example:
# Calling the module module "vpc" { source = "./modules/vpc" cidr_block = "10.0.0.0/16"} # Accessing the module output resource "aws_instance" "example" { subnet_id = module.vpc.subnet_id # other configuration... }
Key Points to Remember
• Outputs must be explicitly defined in the child module to be accessible • The module keyword is required when referencing module outputs • You cannot access internal resources of a module unless they are exposed via outputs • Sensitive outputs can be marked with sensitive = true to prevent values from being displayed in CLI output • Module outputs can reference other module outputs, creating chains of dependencies
Output Dependencies
Terraform automatically creates implicit dependencies when you reference module outputs. If Resource B uses an output from Module A, Terraform understands that Module A must be created first.
Exam Tips: Answering Questions on Accessing Module Outputs
1. Know the exact syntax: The format is always module.<name>.<output> - watch for incorrect variations like modules (plural) or missing the module prefix
2. Understand encapsulation: You can only access values that are explicitly defined as outputs - internal module resources are not accessible from outside
3. Distinguish between module outputs and resource attributes: Resource attributes use resource_type.name.attribute while module outputs use module.name.output
4. Remember the description argument: While optional, the description argument in output blocks is a best practice for documentation
5. Pay attention to sensitive outputs: Know that sensitive = true prevents the value from being shown in the plan output but does not encrypt it
6. Watch for questions about root module outputs: Outputs defined in the root module are displayed after terraform apply and can be queried with terraform output
7. Understand the difference: Input variables pass data INTO a module, while outputs pass data OUT of a module
8. Common exam trap: Questions may present syntax that looks correct but uses wrong keywords - always verify the module keyword is present and spelled correctly