Hello Guys,
We will briefly talk about a very powerful devops tool that is Terraform ,using which you can automate your infrastructure. It can integrate with any public cloud provider to help you automate your infrastructure on cloud.
What is Terraform
- It is an Infrastructure as Code service ( where it’s more easier to write your code than Cloud formation on AWS where you use Json or Yaml)
- Terraform helps you to automate your infrastructure by writing it as code in the form of templates which can be reused as many times as we want.
- It also helps to maintain state by integrating with Gitversion tools and once you make any change you can commit the change to Github or any similar tool .
- Supports major Cloud Platforms like AWS , Azure, Google Cloud, Alibaba, etc
Installing Terraform
You can download the package from the following link
https://www.terraform.io/downloads
I am using the linux server to demonstrate on how to install Terraform on Linux. You can also use the link above where you can find commands to to install Terraform on any platform .Hence, whichever version of Linux you use , you can follow the step by step instructions from this page. I am using an EC2 instance in AWS which is running Ubuntu.
Below are the commands used to install Terraform in order from top to bottom:
Commands for installing Terraform on Ubuntu Server
wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform



Prerequisite for creating resources on AWS
- We need to have an AWS account
- IAM user(Access and Secret key)
Few commands for getting started with Terraform

How a sample Terraform file looks like :
As you can see the file is very simple to write unlike the Json or Yaml. The beauty of Terraform is that you can use it to automate infrastructure by any cloud provider . So if you know how to write a template in Terraform you can create resources in any public cloud.

Creating a sample Terraform template :
We will create a sample terraform template which will launch an EC2 instance in AWS cloud. The important rule to creating a template is mention the provider or the plugin or cloud provider which you will be using :
- Step 1 : We need a provider and in our case its AWS
- Step 2 : We need to authenticate our credentials , hence we will create a profile. If you are unable to create a profile we can provide Access key and secret access key which is not recommended.
- Step 3: I have also installed the Terraform extension on my Visual Studio code to write code in a better way.
- Step 4 : We need to provide a region where your resources will be created/
- Step 5: We will then create a resource parameter and since its an AWS EC2, we need an AMI ID.
- Step 6 : We will then specify the instance type which is t2.micro in the sample template.
- Step 7: We will then tag our server
If get stuck in writing our Terraform template then we can go to the documentation which will help you in writing complex terraform templates. You can check the link below:
https://www.terraform.io/cdktf/concepts/providers-and-resources
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami
Below is the Terraform code for your reference:
provider "aws" {
profile = "myprofile"
region = "us-east-1"
}
resource "aws_instance" "demoec2" {
ami = "ami-052efd3df9dad4825"
instance_type = "t2.micro"
tags = {
Name = "Demoinstance"
}
}

Steps to perform on your EC2 server where you have installed Terraform
- Create a file using Vim and copy the code written above.
- Once the file has been created you will be able to see the code in your home folder in EC2 server as you can see below:

- Now we will run “Terraform init” command to initialize the template.
- Next command will be “Terraform plan”
- Then “Terraform apply”
When you click on apply , you will get a confirmation screen to type yes. Once you type yes , the resource will be created in your AWS account.
Nice blog… can you also included What is state file and where you should store the state file and why terraform need state file?
LikeLike