How to Install Terraform
Terraform must first be installed on your machine. Terraform is distributed as a binary package for all supported platforms and architecture.
To install Terraform, find the appropriate package for your system and download it. Terraform is packaged in a zip archive. You need to unzip it and keep it in a location.
Unzip the package. Let’s create a directory terraform and save the unzipped file inside the folder.
cd /home/ubuntu
mkdir terraform
wget https://releases.hashicorp.com/terraform/0.11.8/terraform_0.11.8_linux_amd64.zip
unzip terraform_0.11.8_linux_amd64.zip
Once we have the terraform file, we will set its path in .bashrc file. Add a line to the file: PATH=$PATH:/home/ubuntu/terraform.
vi .bashrc
PATH=$PATH:/home/ubuntu/terraform
To verify the installation, we have to run the command terraform from anywhere on the machine. We will go through all these commands at the latter part of this blog.
terraform
BUILD INFRASTRUCTURE
Once we have Terraform installed, we will create the infrastructure. For the cloud provider, we are going to use AWS here. We can use any other cloud vendors such as Azure, Google Cloud Platform, Digital Ocean, etc.
If you don’t have AWS account, create one. We will use the resources which are under free-tier.
The set of files used to describe infrastructure is called Terraform configurations. We are going to write our first configuration to launch infrastructure in AWS.
Let’s create a directory for our project and create a terraform file with all our configurations.
mkdir linuxnix_infrastructure
cd linuxnix_infrastructure
touch linuxnix.tf
ls
The whole configuration of the terraform file is displayed below:
provider "aws" {
access_key = "ACCESS_KEY_HERE"
secret_key = "SECRET_KEY_HERE"
region = "ap-south-1"
}
resource "aws_instance" "linuxnix" {
ami = "ami-0d773a3b7bb2bb1c1"
instance_type = "t2.micro"
tags {
Name = "linuxnix"
}
}
output "ip" {
value = "${aws_instance.linuxnix.public_ip}"
}
Here in this configuration, let’s go in details:
- Provider: As “aws” which provides the cloud platform.
- Access Key and Secret Key: These values are used to authenticate terraform to create instances in AWS.
- Region: This is the AWS region where you want your instance to get created.
- Resource: As “aws_instance” which deals with creating ec2 instances in AWS.
- linuxnix: is the resource name for the same.
- ami: is the Amazon machine image ID for the instance.
- instance_type: is the flavor of the instance to be created.
- tags: to give a name to the virtual machine.
- Output: As the output which you want to fetch once the infrastructure is provisioned.
- value: getting the Public IP of the instance as the output.
TERRAFORM VERSION
It prints the terraform version.
terraform version
TERRAFORM INIT
This command initializes terraform into the new or existing project directory and installs various setting and data that will be used by subsequent commands.
terraform init
ls -la
TERRAFORM PLAN
This command generates and shows the execution plan. It shows the plan before creating the actual infrastructure.
terraform plan
TERRAFORM APPLY
This command is used to build or change infrastructure. It actually applies the configuration and creates an instance in your AWS console.
terraform apply
Once it has run the command successfully, we can see an instance up and running in our AWS Console.
TERRAFORM SHOW
This command is used to inspect the current state or plan.
terraform show
TERRAFORM REFRESH
This command updates the local file against the real resource.
terraform refresh
TERRAFORM DESTROY
It destroys terraform manages infrastructure. It will prompt you to enter “yes” if you want to destroy it.
terraform destroy
After it is completed, we can see the instance is terminated in the AWS console.
This completed the installation and configuration of Terraform and we have also created our first infrastructure using Terraform. If you want to learn basics about Terraform, click here. To read more on Terraform and its functionalities, click here
Stay tuned with us @ linuxnix.com.
Latest posts by Ankesh K (see all)
- Deep-Dive into Jenkins – What are all Jenkins functionalities ? - January 10, 2019
- What are different Maven Plugins and how to build a Maven project? - January 7, 2019
- What is a Maven POM File and what are different Maven Repositories? - January 3, 2019
- What is Maven and what are its benefits? - December 31, 2018
- What are the different ways to install Jenkins ? - December 27, 2018