Terraform Introduction
- Subhabrata Datta

- Jun 15, 2020
- 3 min read
What's Terraform ? Why Terraform ? Few important terms ? Getting started with terraform ?

Ø What's Terraform ?
Terraform is an open-source Infrastructure as a Code (IAC) tool that has the ability to communicate with different tools/products like AWS, Google Cloud Platform, Azure, Alibaba, Kubernetes, Grafana and many others to provide the desired infrastructure platform/environment.
Ø Why Terraform ?
(a) To achieve true devops & agile setup, and also to reduce human errors we want to use automation as much as possible. For that we use code, instead of doing everything manually in the GUI. Also, the code serves as a document.
(b) We want to use a single common language. In a multi-cloud or hybrid multi-cloud setup, using different languages and syntaxes for different clouds say AWS, Azure, GCP, and then again for other tools like Kubernetes, it becomes very incovenient and complex. And also training the engineers and infrastructure guys on all the respective languages and syntaxes are time-consuming and costly. Terraform is the solution to all these and can interact with all these different tools.
Ø Few important terms in Terraform ?
(a) Providers : These are various tools/products terraform supports and communicate it like AWS, GCP., Azure, Kubernetes etc. Every provider has a plugin. Here's a compete list of providers in terraform: https://www.terraform.io/docs/providers/index.html
(b) Resources: Every resource represents an Infrastructure object e.g. AWS EC2 instance, AWS volume, Kubernetes Pod etc.
(c) Provisioners: These are used for specific actions on the local machine or remote machine. For example, 'local-exec' provisioner is used to run scripts on the local OS, and 'remote-exec' provisioner is used to run scripts remote OS.
Ø Getting started Terraform ?
Download terraform from https://www.terraform.io/downloads.html
Unzip it and put it in a folder in your system's path. You can use following command to checkout the system paths.
echo %pathNow check that terraform is set up properly and working fine with the following command.
terraform version
Create a working directory, where you will put the terraform codes. We should run our terraform commands in our working directory to execute the terraform codes inside the working directory. All terraform code files should end with .tf extensions.
The code used in terraform is called HCL or HashiCorp Language. Here's a sample terraform code that uses AWS provider and launches an AWS EC2 instance.
provider "aws" {
region = "ap-south-1"
profile = "fiero"
}
resource "aws_instance" "myweb" {
ami = "ami-0447a12f28fddb066"
instance_type = "t2.micro"
key_name = "dattakey"
security_groups = [ "launch-wizard" ]
}Pre-requisite for the code to run successfuly:
a) Profile "fiero" needs to be configured properly. I have discussed this in another post.
b) key_name and security_groups should also be defined in AWS beforehand.
Resource name is mandatory and not an option. In our example myweb is our resource name.
Provider plugins that are mentioned in the terraform codes (here AWS provider plugin) gets downloaded when we use the below command inside the work directory ->
terraform init
We need to use this command only once in the workspace, unless there is no change in resource.
Once download is completed we use below command to implement the code ->
terraform applyEnter value 'yes' when asked.

Finally our AWS EC2 instance is created/launched using terraform code.
Here is the snapshot of the EC2-instance from the AWS webGUI that got launched.

Congratulations on Starting with Terraform ! I Suggest you to go through the www.terraform.io for a deep-dive. Also I would like to thank LinuxWorld & my mentor Mr Vimal Daga for introducing me to Terraform




Comments