Utilizing Terraform Output Variables in Infrastructure Configuration

choubertsprojects

VPN offers!

1. NordVPN

2. Surfshark

3. ExpressVPN

Terraform is an AWS cloud infrastructure tool. It primarily uses a declarative configuration syntax that allows you to describe the creation, modification, and deletion of one or more IT infrastructures. Output Variables allow users to store data in Terraform state files and retrieve it as needed without having to run any commands on your local system by using terraform apply .

The “terraform use output variable in another module” is a way to pass the variables from one Terraform module to another. This allows for some interesting configurations that can be used by multiple modules.

Utilizing Terraform Output Variables in Infrastructure Configuration

Have you ever had to automate infrastructures on a public cloud like Amazon Web Services (AWS)? Don’t worry if you haven’t yet discovered a way to connect numerous Terraform resources or modules. Allow Terraform output variables to perform the heavy lifting!

In this video, you’ll learn how to use Terraform output variables to improve your infrastructure setup!

Prerequisites

This will be a hands-on presentation in this course. If you want to follow along, make sure you have the following items on hand:

  • Terraform — This lesson utilizes Terraform v1.0.8 on Ubuntu 18.04.5 LTS, however Terraform will function on any operating system, including Windows.

How to Install Terraform on Windows is a related topic.

Terraform Output Variables Defined

Building and maintaining infrastructures are all integrated into an automated process in today’s DevOps age. Terraform is the ideal solution for automation, owing to its core notion of Terraform output variables.

You may use Terraform output variables to show the output of a resource (for example, an AWS resource) on the console. If stated in an output configuration file, output variables may also be utilized as an input parameter for other resources.

1. Run the commands below in your terminal to create a folder in your home directory and change the working directory to it.

The folder in this sample is named terraform-output-demo and is located in your home directory. However, you may name the folder anything you like. Your Terraform configuration files are kept in this folder.

mkdir /terraform-output-demo/terraform-output-demo/terraform-output-demo/terraform-out cd /terraform-output-demo/terraform-output-demo/terraform-output-demo/terraform-

Then, in your preferred code editor, copy and paste the settings below. Save the configuration as main.tf in the /terraform-output-demo/terraform-output-demo/terraform-output-demo/terraform-output-demo/terraform-output

Terraform makes use of a variety of configuration files. Each file is saved in plain text or JSON format, with a name convention of.tf or.tfjson.

The main.tf file’s settings enables you to build a variety of AWS cloud resources, as seen below:

  • The default tenancy of an AWS VPC. Other resources in this VPC include public subnets, route tables, and AWS EC2 instances.
  • Internet Gateway (IGW) – IGW is a service that directs traffic from the public subnet of an EC2 instance to the internet.
  • Public Subnets – Using public subnets, you may expose resources to the whole public.
  • Route Tables — You may use route tables to go from your EC2 instance to the internet.
  • AWS EC2 instances – These are the instances that start up in a freshly formed VPC.
  • The variables cidr block, ami, and instance type are specified. The values for these variables are specified in a separate file called terraform.tfvars, which you will create in the following step.

The Amazon EC2 console is where you can get Linux AMIs (Amazon Machine Images).

## Creating the “aws vpc” “Main” VPC resource ## Defining the CIDR block cidr block = var.main vpc cidr instance tenancy = “default” ## Using 10.0.0.0/24 for demo cidr block = var.main vpc cidr cidr block = var.main vpc cidr cidr block = var.main vpc cidr cidr block = ## Assigning the Internet Gateway to the VPC resource “aws internet gateway” “IGW” # After we construct VPC, vpc id will be produced. vpc id = aws vpc.Main.id vpc id = aws vpc.Main.id vpc id = aw ## Creating a Public Subnet resource “aws subnet” “publicsubnets” vpc id = aws vpc.Main.id # CIDR block of public subnets cidr block = var.public subnets cidr block = var.public subnets cidr block = var.public subnets cidr block = var.public subnets “aws route table” “PublicRT” resource “aws route table” “PublicRT” vpc id = aws vpc.Main.id route cidr block = “0.0.0.0/0” ## Internet Gateway gateway id = aws internet gateway.IGW.id ## Traffic from Public Subnet reaches Internet through Internet Gateway ## Route table association with Public Subnet resource “aws route table association” “PublicRTassociation” subnet id = aws subnet.publicsubnets.id subnet id = aws subnet.publicsubnets.id subnet id = aws subnet.publicsubnets.id subnet id = aws subnet.publicsub aws route table.PublicRT.id route table id = aws route table.PublicRT.id route table id = aws route table.PublicRT ## Creating an EC2 instance using the ami and instance type resources “aws instance” “my-machine” ami = var.ami instance type = var.instance type subnet id = aws subnet.publicsubnets.id tags = Name = “my-ec2-machine” ami = var.ami instance type = var.instance type subnet id = aws_

3. Now, within the /terraform-output-demo directory, create a new file called vars.tf and copy/paste the text below into it. The vars.tf file is a Terraform variables file that contains all variables referenced by the main.tf configuration file.

# Creating a Variable for ami variable “ami” type = string # Creating a Variable for instance type variable “instance type” type = string # Creating a Variable for instance type variable “instance type” type = string

4. Inside the /terraform-output-demo directory, create a new file named provider.tf and paste the provider’s code into it. The AWS provider is defined in the provider.tf file, which allows Terraform to connect to the appropriate cloud services.

Getting Started with the Terraform AWS Provider is a related article.

The code below generates resources in the us-east-2 region using the AWS provider aws.

AWS resources may be launched in any location.

region = “us-east-2” provider “aws”

5. Next, under the /terraform-output-demo directory, create a new file. Create a new file called terraform.tfvars and put the code below into it.

Terraform utilizes the values in the terraform.tfvars file to replace variable references in configuration files (main.tf).

instance type = “t2.micro” ami = “ami-0742a572c2ce45ebf”

6. In the /terraform-output-demo directory, make one last file. Copy and paste the code below into the output.tf file.

Following the terraform apply command, the Terraform output configuration file below gives AWS resources such as AWS EC2 instance arn and public IP address.

It is possible to include all configuration settings in a single configuration file. However, splitting the logic and variables into distinct files is better to make things simple for developers and administrators.

output “ec2 arn” is an output variable that stores the instance’s arn and displays it after the terraform apply command. ## The value is determined by the resource’s name and type (same as that of main.tf) aws instance.my-machine.arn value = aws instance.my-machine.arn ## Output variable that will save and show the instance public IP address following the terraform apply command output “instance ip addr” description = “The public IP address of the primary server instance.” value = aws instance.my-machine.public ip

7. Verify that all essential files are in the terraform-output-demo folder using the tree command below.

terraform-output-demo tree

Verifying the Existence of Required Files Verifying the Existence of Required Files

8. Finally, use the instructions below to open Terraform and browse to the terraform-output-demo directory. Terraform sets up the necessary plugins and providers to deal with resources.

Creating an AWS EKS Cluster using a Terraform Configuration is related.

Terraform init, terraform plan, and terraform apply are the three stages of the Terraform process.

cd terraform-output-demo/terraform-output-demo/terraform-output-demo/terraform terraform begin, terraform plan, and terraform execute

If everything goes as planned after performing the terraform apply command, you’ll see the Apply complete! 6 resources were added, 0 were altered, and 0 were deleted, as stated in the message below.

You’ll need the output instance ID and IP address later to check whether they exist in the AWS Management Console.

Terraform output variables are used to print particular values in the command line output inside the same parent or child module, and they are also used as inputs to build resources using the terraform apply command.

The command shows the output’s EC2 instance arn and instance public IP address as shown below.

Making a mental note of the Instance ID and IP Address Making a mental note of the Instance ID and IP Address

Using the AWS Console to verify Terraform outputs

You should have the VPC and EC2 instances by now, but let’s double-check by manually checking the AWS Management Console for the VPC.

1. Go to the AWS Management Console in your chosen web browser and log in.

2. While in the console, use the top-right search box to look for ‘EC2’, as shown below.

When you select the EC2 menu item, your browser takes you to a page where you can view all of your EC2 instances.

1647502632_199_Utilizing-Terraform-Output-Variables-in-Infrastructure-Configuration Looking for EC2?

3. Next, click on the same instance ID you noted in the “Terraform Output Variables Defined” section (step eight) to view the instance’s details.

Getting a Glimpse into EC2 Instances Getting a Glimpse into EC2 Instances

4. Verify if the IP address of the EC2 instance is the same as the IP address you noted in the “Terraform Output Variables Defined” section (step eight).

Verifying EC2 Instance's IP Address Verifying the IP Address of an EC2 Instance

Conclusion

You learnt how to utilize Terraform output variables to retrieve AWS EC2 instance data like IP address and arn in this lesson. Without login onto AWS, you may contact the instance directly using putty or SSH clients when Terraform produces outputs.

Why not use Terraform output variables with other AWS services now that you have this newfound knowledge?

The “terraform output tutorial” is a tutorial that explains how to use Terraform’s Output Variables in Infrastructure Configuration.

Related Tags

  • terraform output multiple values
  • terraform output example
  • terraform output local variable
  • terraform outputs
  • terraform output for_each