What is the correct syntax to define an output in a Terraform module to expose the public IP of an aws_instance named 'web'?

Prepare for the HashiCorp Terraform Associate Exam. Utilize flashcards and multiple-choice questions with detailed hints and explanations. Boost your confidence and be ready for success!

Multiple Choice

What is the correct syntax to define an output in a Terraform module to expose the public IP of an aws_instance named 'web'?

Explanation:
Outputs in a Terraform module are how you expose values to the module’s caller. To reveal the public IP of an aws_instance named web, you define an output and set its value to the resource’s attribute. The correct approach uses the resource reference format: aws_instance.web.public_ip, placed inside an output block. So the valid form is output "ip" { value = aws_instance.web.public_ip }. This makes the instance’s public IP available to whatever module consumes this one. Other patterns aren’t appropriate here. export isn’t Terraform syntax for outputs, and var.ip would just pass in a value from a variable rather than expose the resource’s attribute. Referencing module.web.public_ip would point to a child module’s output, not to the aws_instance resource within this module.

Outputs in a Terraform module are how you expose values to the module’s caller. To reveal the public IP of an aws_instance named web, you define an output and set its value to the resource’s attribute. The correct approach uses the resource reference format: aws_instance.web.public_ip, placed inside an output block. So the valid form is output "ip" { value = aws_instance.web.public_ip }. This makes the instance’s public IP available to whatever module consumes this one.

Other patterns aren’t appropriate here. export isn’t Terraform syntax for outputs, and var.ip would just pass in a value from a variable rather than expose the resource’s attribute. Referencing module.web.public_ip would point to a child module’s output, not to the aws_instance resource within this module.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy