Use Output Variables to Query Data in AWS Using Terraform

30 minutes
  • 3 Learning Objectives

About this Hands-on Lab

Hey there, Gurus! Welcome to the lab! This lab will demonstrate the usefulness of output variables and how they can be used to collect useful data about resources in your infrastructure. In this lab, we will first deploy the lab environment, then we will add some output variables to output the instance name and its IP. Let’s get started!

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Set Up the Environment

In the Terminal

  1. Once logged into your instance, change to the lab-aws-outputs directory.
  2. Copy your ami and your subnet_id from your resource_ids.txt file, then update the main configuration file.
  3. Initialize the working directory.
  4. Apply the Terraform configuration.
Add Output Variables and Deploy Changes

In the Terminal

  1. Create an outputs.tf file.
  2. Add this code:
    
    output "instance_name" {
    description = "The tag name for this instance"
    value       = var.instance_name
    }

output "instance_id" {
description = "ID of the EC2 instance"
value = aws_instance.app_server.id
}

output "instance_public_ip" {
description = "Public IP address of the EC2 instance"
value = aws_instance.app_server.public_ip
}


3. Apply the configuration changes.
4. Notice the output after the apply.
Confirm the Changes

In the Terminal

  1. Confirm the outputs are working.
  2. Inspect the state.
  3. Destroy your infrastructure.

Additional Resources

You are an admin who supports one of the largest online comic book retailers in the world. You are one of a handful of admins who have been assigned to use Terraform to manage the company's cloud deployments. You are asked to add an ouputs configuration file to your Terraform configuration that will use output variables to query the name, ID, and public IP of your instance so this information is easy to look up when needed.

Configuration Used in This Lab

Code Added in the Video

output "instance_name" {
  description = "The tag name for this instance"
  value       = var.instance_name
}

output “instance_id” {
  description = “ID of the EC2 instance”
  value       = aws_instance.app_server.id
}

output “instance_public_ip” {
  description = “Public IP address of the EC2 instance”
  value       = aws_instance.app_server.public_ip
}

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?