Lesson 4 of 6
Print the server address
Add an output value that prints the instance public IP address so you can open the web page.
6 minInfrastructure
After this lesson, you will be able to add an output value to your Terraform configuration that prints the public IP address of your web server. You will also know how to view outputs after applying and how to use them to connect to your server.
Why output values help
When Terraform creates a cloud resource, it often knows details that you do not know in advance. For your web server, the public IP address is assigned by the cloud provider only when the instance starts. To visit the web page, you need that address.
You could log in to the cloud console and look it up, but that is slow and easy to forget. Terraform can print the address for you after it finishes. You do this with an output value.
An output value is a named piece of data that Terraform shows after an apply. You can also ask Terraform to show outputs at any time without changing anything.
Add an output for the public IP
Open your main.tf file. At the end, after the instance resource, add this block:
output "instance_ip" {
description = "Public IP address of the web server"
value = aws_instance.web.public_ip
}The name instance_ip is what you will use to refer to this output. The description is optional but helps you remember what it is for. The value line tells Terraform to get the public_ip attribute from the resource you named web.
The resource name web comes from the line resource "aws_instance" "web" in your file. The attribute public_ip is provided by the AWS provider. You can find a full list of attributes for aws_instance in the provider documentation, but for now this one is enough.
Run terraform fmt and terraform validate to check the file:
terraform fmt
terraform validateBoth commands should finish without errors. terraform fmt may adjust the spacing in your file. terraform validate checks that the configuration is correct.
See the output after apply
If you have not applied your configuration yet, run terraform plan first to see what will happen. The plan will show that a new output will be created, but it will not show the value because the instance does not exist yet.
terraform planNow run terraform apply. Terraform will show the plan again and ask for confirmation. Type yes and press Enter.
terraform applyTerraform creates the security group and the instance. When it finishes, it prints the outputs at the end. You will see something like:
Outputs:
instance_ip = "203.0.113.15"The actual address will be different for you. This is the public IP address of your web server.
Use the output to visit the web page
Copy the IP address from the output. Open a web browser and go to http://203.0.113.15 (replace with your address). You should see the page that says Hello from Terraform.
If the page does not load, wait a minute and try again. The instance may still be starting up, or the web server may not have finished installing.
Show outputs without applying
If you close your terminal and later want to see the IP address again, you do not need to run apply. Run this command:
terraform outputTerraform reads the state file and prints all outputs. You can also ask for one output by name:
terraform output instance_ipThis prints only the IP address, without the name. That is useful if you want to use the address in a script.
Summary
You added an output value that prints the public IP address of your web server. You applied the configuration and used the output to open the web page. You also learned how to show outputs later with terraform output.
Key takeaways
- An output value prints a piece of data from your Terraform state after apply.
- Use
value = aws_instance.web.public_ipto get the public IP address of an instance namedweb. - Run
terraform outputto see all outputs, orterraform output instance_ipfor just one. - The public IP address is assigned by the cloud provider and is only known after the instance is created.
Try it
Add another output named instance_id that prints the instance ID. The attribute for the ID is id on the aws_instance.web resource. Run terraform fmt and terraform validate. Then run terraform apply and confirm that both outputs appear at the end. After that, run terraform output instance_id and check that it prints only the ID. A good result is that you see both the IP address and the instance ID in the outputs, and the single output command prints just the ID.
Official documentation
Written with AI and checked automatically. Tools change, so a command may behave differently in the version you install. Spotted a mistake? Tell us.