How to use Terraform outputs and inputs
Hello. In this tutorial, we will explain the input and output variables in Terraform scripting language.
1. Introduction
Terraform is an open-source tool developed by HashiCorp for building, changing, and versioning the infrastructure safely and efficiently. It is used to manage the infrastructure of the popular cloud service providers and custom in-house solutions. It helps manage both low-level (Compute, Storage, Networking, etc.) and high-level components (such as SaaS, DNS, etc.) Terraform deployment automation is divided into different sections i.e. –
- IaaC – IaaC is popularly known as the Infrastructure as a Code wherein the infrastructure is described using a high-level configuration syntax. This allows a blueprint of the infrastructure which can be deployed, versioned, and shared for re-use
- Execution Plans – Terraform has a planning step where it generates an execution plan. The execution plan tells the administrator what Terraform will do once applied and helps to avoid any surprises when it creates the infrastructure
- Resource Graph – Terraform builds a graph of all the resources and parallelizes the creation and modification of non-dependent resources. This offers insights into learning the dependencies in their infrastructure
- Change Automation – Terraform allows to apply of complex changesets to the infrastructure with minimal human intervention
1.1 Configuration language
Terraform has its configuration language designed to meet the infrastructure automation requirements. The main purpose of this language is to declare resources and a group of resources (gathered into a module) represents a larger unit of configuration. Language syntax consists of a few elements i.e. – Blocks, Arguments, and Expressions.
- Blocks – Containers for other contents and represents the object configuration
- Arguments – Assign a value to the name and appear within the blocks
- Expressions – Represents a single value, referenced value, or combination of other values
1.2 Steps
To create the infrastructure via the Terraform scripts following commands need to be executed in a sequence. However, details and actions may differ between workflows.
terraform init
– Initializing the new or existing terraform configurationterraform plan
– Generate the execution plan from the resources specified in the fileterraform apply
– Create the infrastructure from the resources specified in the fileterraform destroy
– Destroy the created infrastructure
2. How to use Terraform outputs and inputs
In terraform, variable usage makes the configuration more dynamic. In this article, we will cover input and output variables.
2.1 Input variables
Input variables in terraform scripting language are used to specify variables at the runtime to set up the infrastructure deployment in the cloud. The variables are defined in the main terraform file but as a recommended practice these variables are defined in a separate file known as variables.tf
to provide better readability and organization of things. A variable in the file is defined inside the variable
block with a label and consists of three optional arguments i.e.
description
: Define the purpose of the variabletype
: Specify the datatype such as primitive (string, number, bool) and complex (list, map, object)default
: If present, value is considered to be optional and if no value is set the default value will be used
Every label name for the variable should be unique in the configuration file and are accessed in the var.variable_name
format. A sample variable file from the AWS environment looks like –
variables.tf
variable "aws_access_key" { type = string description = "cli user access key" default = "YOUR_AWS_ACCESS_KEY" } variable "aws_secret_key" { type = string description = "cli user secret key" default = "YOUR_AWS_SECRET_KEY" } variable "region" { type = string description = "ec2 region" default = "ap-south-1" } variable "tags" { type = map(string) description = "mapping of tags to assign to the ec2 instance" default = { environment = "development" terraform = "true" company = "ducat" batch = "devops" } }
These variables are used in the actual implementation and are accessed in the implementation files.
2.2 Output variables
Output variables in terraform scripting language allow developers to define variables in the configuration file that we want to share with other resources or users. For eg. Display the public IP address or the EC2 instance id/name etc. An output variable is defined inside the output
block with a label. Every label name should be unique in the configuration file. Here is a sample output from the AWS environment –
outputs.tf
output "id" { description = "instance-id" value = aws_instance.terraforminstance[0].id } output "arn" { description = "instance-arn" value = aws_instance.terraforminstance[0].arn } output "instance_state" { description = "instance-state" value = aws_instance.terraforminstance[0].instance_state } output "keyname" { description = "keyname" value = aws_instance.terraforminstance[0].key_name } output "public_ip" { description = "public-ip" value = aws_instance.terraforminstance[0].public_ip }
That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!
3. Summary
In this tutorial, we learned an introduction to Terraform and understood the usage of input and output variables while creating the infrastructure in the real environment.