📖 Scenario: You are building a Terraform configuration to deploy cloud resources. You want to organize your code using modules and then access the outputs from those modules in your main configuration.
🎯 Goal: Create a Terraform module that defines a resource and outputs a value. Then, in the main Terraform configuration, call the module and access its output value.
📋 What You'll Learn
Create a Terraform module with a resource and an output
Call the module from the main Terraform configuration
Access the module's output in the main configuration
💡 Why This Matters
🌍 Real World
Modules help organize Terraform code for reusable infrastructure components. Outputs allow sharing information between modules and the main configuration.
💼 Career
Understanding modules and outputs is essential for managing complex infrastructure as code in professional cloud engineering roles.
Progress0 / 4 steps
1
Create a Terraform module with a resource and output
Create a Terraform module directory called my_module. Inside it, create a file main.tf that defines a resource random_pet.example with length = 2. Then create an output called pet_name that outputs the random_pet.example.id.
Terraform
Hint
Use the random_pet resource to generate a random name. Then create an output block with the exact name pet_name.
2
Call the module from the main Terraform configuration
In the main Terraform configuration file main.tf, add a module block called example_module that uses the source ./my_module.
Terraform
Hint
Use a module block with the exact name example_module and set source = "./my_module".
3
Access the module output in the main configuration
Create an output in the main Terraform configuration called module_pet_name that outputs the value of module.example_module.pet_name.
Terraform
Hint
Define an output block with the exact name module_pet_name and set its value to module.example_module.pet_name.
4
Complete the Terraform configuration with required provider
Add the terraform block with required providers specifying random provider with source hashicorp/random and version constraint "~> 3.0".
Terraform
Hint
Use the terraform block at the top level with required_providers specifying the random provider.
Practice
(1/5)
1. What is the main purpose of output blocks in a Terraform module?
easy
A. To share information from inside the module to the outside
B. To define variables that the module accepts
C. To create resources inside the module
D. To delete resources after deployment
Solution
Step 1: Understand the role of output blocks
Output blocks are used to expose values from a module so other parts of the Terraform configuration can use them.
Step 2: Differentiate outputs from variables and resources
Variables are inputs to modules, resources create infrastructure, outputs share info outwards.
Final Answer:
To share information from inside the module to the outside -> Option A
Quick Check:
Outputs = share info outside module [OK]
Hint: Outputs pass info out of modules, variables bring info in [OK]
Common Mistakes:
Confusing outputs with variables
Thinking outputs create resources
Assuming outputs delete resources
2. Which of the following is the correct syntax to define an output named instance_ip in a Terraform module?
easy
A. output "instance_ip" : aws_instance.example.public_ip
B. output instance_ip = aws_instance.example.public_ip
C. output "instance_ip" { value = aws_instance.example.public_ip }
D. output { instance_ip = aws_instance.example.public_ip }
Solution
Step 1: Recall Terraform output block syntax
Outputs use the keyword output followed by the name in quotes, then a block with value = ....
Step 2: Match syntax to options
output "instance_ip" { value = aws_instance.example.public_ip } matches the correct syntax: output "name" { value = ... }.
Final Answer:
output "instance_ip" { value = aws_instance.example.public_ip } -> Option C
Quick Check:
Output block = output "name" { value = ... } [OK]
Hint: Outputs need quotes and value assignment inside braces [OK]
Common Mistakes:
Missing quotes around output name
Using equals sign outside block
Incorrect colon or braces placement
3. Given this module output block:
output "bucket_name" {
value = aws_s3_bucket.my_bucket.id
}
What will be the value of the root module's name output after applying?
medium
A. The name of the S3 bucket resource type
B. Null because the module output is not defined
C. An error because outputs cannot be nested
D. The ID of the S3 bucket created by the module
Solution
Step 1: Understand module output propagation
The module outputs the bucket's ID as bucket_name. The root module accesses it via module.storage.bucket_name.
Step 2: Confirm root output value
The root output name simply passes through the module's output value, so it will be the bucket ID.
Final Answer:
The ID of the S3 bucket created by the module -> Option D
Quick Check:
Root output = module output value [OK]
Hint: Root outputs can reference module outputs directly [OK]
Common Mistakes:
Thinking outputs cannot be nested or chained
Confusing bucket ID with resource type name
Assuming output is null if not explicitly set in root
4. You have this output block inside a module:
output "subnet_id" {
value = aws_subnet.example
}
Why is this incorrect for outputting the subnet ID?
medium
A. The output value should reference a specific attribute, like aws_subnet.example.id
B. Output blocks cannot reference resources directly
C. The output name cannot be "subnet_id"
D. The module source path is missing
Solution
Step 1: Check output value correctness
To output the subnet ID, the value must reference a specific attribute like aws_subnet.example.id, not the whole resource object.
Step 2: Identify correct attribute to output
For subnet ID, use aws_subnet.example.id instead of the resource itself.
Final Answer:
The output value should reference a specific attribute, like aws_subnet.example.id -> Option A
Quick Check:
Output value = resource attribute, not resource object [OK]
Hint: Always output resource attributes, not whole resources [OK]
Common Mistakes:
Outputting entire resource instead of attribute
Wrong output block name
Ignoring module source configuration
5. You want to create a module that outputs a list of public IPs from multiple instances it creates. Which output block correctly returns this list?
hard
A. output "public_ips" { value = aws_instance.example[0].public_ip }
B. output "public_ips" { value = [for inst in aws_instance.example : inst.public_ip] }
C. output "public_ips" { value = aws_instance.example[*].id }
D. output "public_ips" { value = aws_instance.example }
Solution
Step 1: Understand how to output multiple values
When multiple instances exist, use a for-expression to collect their public IPs into a list.
Step 2: Match correct syntax for list output
output "public_ips" { value = [for inst in aws_instance.example : inst.public_ip] } uses a for-expression to extract public_ip from each instance correctly.
Final Answer:
output "public_ips" { value = [for inst in aws_instance.example : inst.public_ip] } -> Option B
Quick Check:
Use for-expression to output lists from multiple resources [OK]
Hint: Use for-expressions to gather attributes from multiple resources [OK]
Common Mistakes:
Outputting single attribute from multiple instances without list
Using wrong attribute like id instead of public_ip
Outputting entire resource object
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.