Terraform Associate Exam QuestionsBrowse all questions from this exam

Terraform Associate Exam - Question 321


You are writing a child Terraform module that provisions an AWS instance. You want to reference the IP address returned by the child module in the root configuration. You name the instance resource "main".

Which of these is the correct way to define the output value?

Show Answer
Correct Answer: A

To define an output value for the IP address returned by a child Terraform module, you need to use the 'output' block with the 'value' argument containing the expression that returns the desired value. The expression should refer to the attribute exposed by the resource. In this case, 'aws_instance.main.private_ip' is a valid expression that gets the private IP of the instance named 'main'. Therefore, option A correctly defines the output value.

Discussion

2 comments
Sign in to comment
akbiyikOption: A
Aug 26, 2024

Each output value exported by a module must be declared using an output block: output "instance_ip_addr" { value = aws_instance.server.private_ip } The value argument takes an expression whose result is to be returned to the user. In this example, the expression refers to the private_ip attribute exposed by an aws_instance resource defined elsewhere in this module (not shown). Any valid expression is allowed as an output value. https://developer.hashicorp.com/terraform/language/values/outputs

thureOption: A
Mar 28, 2025

Why the other options are incorrect: B: value = ${main.private_ip} is invalid because: It lacks the full resource address (aws_instance.main) Terraform no longer requires ${} interpolation for simple expressions C & D: return is not a valid keyword in Terraform's HCL syntax.