nerdexam
HashiCorp

TA-002-P · Question #435

You are writing a child Terraform module which provisions an AWS instance. You want to make use of the IP address returned in the root configuration. You name the instance resource "main". Which of…

The correct answer is A. Option A. To define an output value for the public IP address of an AWS instance named 'main' within a child module, the correct HCL2 syntax uses aws_instance.main.public_ip as the output's value.

Interact with Terraform modules

Question

You are writing a child Terraform module which provisions an AWS instance. You want to make use of the IP address returned in the root configuration. You name the instance resource "main". Which of these is the correct way to define the output value using HCL2?

Options

  • AOption A
  • BOption B

How the community answered

(29 responses)
  • A
    93% (27)
  • B
    7% (2)

Why each option

To define an output value for the public IP address of an AWS instance named 'main' within a child module, the correct HCL2 syntax uses `aws_instance.main.public_ip` as the output's value.

AOption ACorrect

Assuming Option A represents `output "instance_ip" { value = aws_instance.main.public_ip }`, this is the correct HCL2 syntax to define an output named 'instance_ip' that exposes the `public_ip` attribute of an `aws_instance` resource named `main`.

BOption B

Option B must represent an incorrect syntax for referencing a resource attribute in an output block; for instance, it might use `aws_instance.main.ip_address` if `public_ip` is the actual attribute, or an incorrect order of elements.

Concept tested: Referencing resource attributes in outputs (HCL2)

Source: https://developer.hashicorp.com/terraform/language/values/outputs#expressions-in-output-values

Topics

#Terraform outputs#HCL2 syntax#Module development#Resource attributes

Community Discussion

No community discussion yet for this question.

Full TA-002-P Practice