How to Reference Module Outputs in Terraform
Exhibit
Refer to the exhibit.
```hcl
module "bucket" {
source = "./modules/s3-bucket"
bucket_name = "my-app-data"
}
resource "aws_s3_bucket_object" "config" {
bucket = module.bucket.bucket_name
key = "config.json"
source = "config.json"
}
```
The module at './modules/s3-bucket' contains:
```hcl
variable "bucket_name" {}
resource "aws_s3_bucket" "this" {
bucket = var.bucket_name
}
output "bucket_name" {
value = aws_s3_bucket.this.id
}
```After running 'terraform apply', the user sees that the 'aws_s3_bucket_object' is created successfully, but the bucket name is not as expected. What is the most likely reason?
Quick Answer
The answer is that the bucket name should be as expected because the module output is correctly defined. In Terraform, when you reference module outputs, the `id` attribute of an `aws_s3_bucket` resource is literally the bucket name itself, not a separate generated identifier—this is a key distinction from many other AWS resources where `id` is an internal hash. On the HashiCorp Terraform Associate TF-003 exam, this question tests your understanding of how module outputs expose resource attributes and the specific behavior of the `aws_s3_bucket` resource. A common trap is assuming `id` is always an opaque identifier, but for S3 buckets, it directly equals the name you assigned. To remember this, think: “S3 bucket id = bucket name, no guesswork needed.”
⚠ Common exam trap
HashiCorp often tests the misconception that 'id' is a random or internal identifier rather than the actual resource name, leading candidates to incorrectly think the output is wrong when it is actually correct.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
The output 'bucket_name' in the module is set to 'aws_s3_bucket.this.id', which is the bucket name, so the bucket name should be as expected.
The module output 'bucket_name' is defined as 'aws_s3_bucket.this.id', and in Terraform, the 'id' attribute of an 'aws_s3_bucket' resource is exactly the bucket name (not a generated ID). Since the user sees the object created successfully, the module is being called and the output is correctly referencing the bucket name, so the bucket name should be as expected. The question implies the user's expectation is wrong or the bucket name is actually correct, making D the only statement that aligns with Terraform's behavior.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
The module variable 'bucket_name' is not consumed by the resource; the resource uses a hardcoded name.
Why it's wrong here
The resource uses 'var.bucket_name', so it is consumed.
- ✗
The module output is incorrectly defined; it should use 'bucket' attribute instead of 'id'.
Why it's wrong here
Both 'id' and 'bucket' resolve to the bucket name.
- ✗
The module does not have an output for the bucket name, so the reference fails silently.
Why it's wrong here
The output is defined, and if it were missing, Terraform would error.
- ✓
The output 'bucket_name' in the module is set to 'aws_s3_bucket.this.id', which is the bucket name, so the bucket name should be as expected.
Why this is correct
The configuration appears correct; if the bucket name is not as expected, the issue might be elsewhere, but the output is correct.
Quick reference
AWS S3 Storage Class Comparison
| Storage Class | Min Duration | Retrieval | Use Case |
|---|---|---|---|
| S3 Standard | None | Immediate | Frequently accessed data |
| S3 Standard-IA | 30 days | Immediate | Infrequent access, rapid retrieval |
| S3 One Zone-IA | 30 days | Immediate | Non-critical infrequent data |
| S3 Intelligent-Tiering | None | Immediate–hours | Unknown or changing access patterns |
| S3 Glacier Instant | 90 days | Milliseconds | Archive with instant retrieval |
| S3 Glacier Flexible | 90 days | Minutes–hours | Archive, flexible retrieval |
| S3 Glacier Deep Archive | 180 days | Hours | Long-term compliance archive |
Go deeper
Related to this question
About these practice questions
Courseiva writes every TF-004 question from scratch — 428 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
Same concept, more angles
2 more ways this is tested on TF-004
These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.
Variation 1. Which four of the following statements about interacting with Terraform modules are correct? (Choose four.)
medium- ✓ .The source attribute in a module block can reference a local file path, a Git repository, the Terraform Registry, or an HTTP URL.
- .To use a module from a private registry, you must always specify a version constraint in the source attribute of the module block.
- ✓ .A module can reference outputs from another module using the syntax module.<MODULE_NAME>.<OUTPUT_NAME>.
- ✓ .Terraform automatically downloads module dependencies when running terraform init, including nested modules from the root module.
- ✓ .Module inputs are defined as variables in the module's root directory, and outputs are defined as output values that can be consumed by the calling configuration.
- .A module can be used to create resources only in the same provider configuration as the root module; it cannot define its own provider configurations.
Why : This statement is correct because Terraform modules expose outputs that can be referenced by the calling configuration using the syntax `module.<MODULE_NAME>.<OUTPUT_NAME>`. This allows values computed inside a module to be used elsewhere in the root module, enabling modular composition and data sharing between modules.
Variation 2. After applying a module that creates a VPC, a user wants to use the VPC ID in another resource within the root configuration. How should they reference the output from the module?
easy- ✓ A.vpc_id = module.vpc.vpc_id
- B.vpc_id = module_vpc.vpc_id
- C.vpc_id = data.module.vpc.vpc_id
- D.vpc_id = local.vpc_id
Why A: In Terraform, module outputs are accessed using the syntax `module.<module_name>.<output_name>`. Since the module is named `vpc` and it exposes an output called `vpc_id`, the correct reference is `module.vpc.vpc_id`. This allows the root configuration to consume the VPC ID created by the module.
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This TF-004 practice question is part of Courseiva's free HashiCorp certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the TF-004 exam.