Terraform Part - 2

Terraform Part - 2

Implementation

In the previous article, we’ve learnt what terraform is, how it works and why to use it, right? If you’ve not aware of it, I please recommend you to get aware of this.

Now coming back to this article, we are going to learn how to implement Terraform. I’ve committed these files into GitHub repo, you can refer these to implement.

Requirements:

  1. Terraform Installed

  2. AWS or Azure CLI installed

  3. VS Code

  4. AWS Tool Kit Plugin (VS Code)

  5. Terrform Plugin

    • After you’ve installed the Terraform please go to any Directory and create a new one.

    • Open it in VS Code

    • And open the terminal in VS Code as ctrl + shift + `

    • Go to AWS Console and go to

Account AWS

IAM Dashboard

Create Access Key using This Button

  • Create an Access Key and go to the terminal on VS Code

  • give command as aws login and give the credentials which you’ve copied from the AWS Console.

  • After logging into your account using AWS CLI, go to your project directory and open VS Code.

  • After opening VS Code, create a file called main.tf for terraform.

Create a Directory and Main file in it

  • Run command as terraform -version

Output as Terraform Version

Now, copy paste this code,and let’s us understand the output of it.

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

# Configure the AWS Provider
provider "aws" {
  region = "us-east-1"
}
resource "aws_vpc" "Blog-demo" {
  cidr_block = "10.0.0.0/16"
}
  • After copy pasting the code, in the terminal give command as

    • terraform init to initialize the project

      Terraform iinit

  • terraform fmt to format the code files into easy and readable format

  • terraform validate to check code is syntactically correct or not

    Terraform validate

  • terraform plan to check what changes gonna it reflect in AWS Infra.

    Terraform plan

  • terraform apply to build the configuration which you’ve described as code

    Here it may ask you to approve by giving input as yes.4

    Terraform apply

  • To check whether it has created or not, just go to the AWS Console > VPC > Your VPCs > You’ll see a VPC has been created

    VPC Created

Now, let us try to understand what just happened.

  • This is the code in which we’ve described our infrastructure using HCL Language.

  • Firstly, we define the Terraform block and then our provider. This block is constant for all the providers but the source and the version changes accordingly.

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}
  • It is difficult to remember the code for all these resources which AWS Provides us, so it is recommended to use the Terraform AWS Documentation files from here.

  • In these files, they have written the templates for each resource, we just copy and paste on our code according to what the resources we are wanted.

  • And coming to the next module, is

# Configure the AWS Provider
provider "aws" {
  region = "us-east-1"
}
resource "aws_vpc" "Blog-demo" {
  cidr_block = "10.0.0.0/16"
}
  • Here, we just defined the AWS Region as us-east-1 , we can also use the other regions like ap-south-1, etc.

  • And the last module, in which we’ve created the AWS VPC - Virtual Private Cloud.

  • Like in HTML, we use here few tags like keywords which are supposed to refer to a single service which we are going to create on AWS

  • In the next line, we define the IP Address range

  • And by defining these, and saving these go to the terminal and follow the steps which mentioned before.

  • When you give command as terrform init you might have observed this file creation, right?

    lock.hcl file

  • When you give this command, Terraform installs the plugins of the AWS or any provider you mention in the files.

  • In the file .terraform.lock.hcl terraform stores the which version of external dependency provider we’ve used in the configuration to build the infrastructure in our case it is version = "~> 5.0"

  • Remember those commands which we’ve used before, we will be using it all the time of Terraform.

  • Now copy paste this code to create an EC2 Instance

# Create a EC2 Instance
resource "aws_instance" "example" {
  ami           = "ami-" # Specify an appropriate AMI ID
  instance_type = "t2.micro"
}
  • Get AMI ID by going to the Instance Creation Console, and just scroll down you'll find it near the type of System which you chose to create.

    AMI

  • After pasting it, save the file and in the comment section give the same commands.

  • No matter if you use command terraform init or not, it gonna execute now. Because we’ve already initialized the project, so we no need to use it now.

  • Here we have to discuss about two things,

Plan and Apply

  • After applying the plan command, we can observe that few lines in + in green, which indicates we are going to create this particular service or resource on AWS and also indicates there’s no error here for now.

  • In Apply command, instead of giving terraform apply use terraform apply -auto-approve to automatically run apply the changes on the infrastructure instead of waiting for our input as yes .

  • Here, it will show you the number of resources been added as compared to the last time.

  • It shows only one for you, no need to hesitate. Now, go to the Instances page and let us confirm whether it is successful or not?

    Destroy plan

  • We’ve successfully created it and the second thing which we are about to discuss is

State File

  • On the left side bar, we can see that a state file has created. Now, what is the purpose of it and why it need to be included in our project?

    State Files

  • Well, state file is used to keep track of our infrastructure.

  • As a DevOps engineer, we might work with multiple persons from multiple teams. Any person in the organization with the access may delete the resource which you’ve created from the console.

  • And we may not find which resource been deleted among multiple resources and configurations.

  • So, here our state files helps us to keep track of the infrastructure. If there is any change it automatically keeps the infra in the state which is defined in the file (IaC).

  • This only happens when we run it.

  • To summarize this in simple, State file used to keep the track of our infrastructe, any changes are made noted here and alsways tries to maintain the infra according to the Code we have written.

If we’ve deleted the resource from the configuration file itself, then it gonna change the state file as well.

Terraform Refresh

  • Or in a scenario where you deleted a resource which had defined from the console of your provider. And to reflect that change in our state file aswell, we have to give the command as terraaform refresh

  • We will discuss more commands in the upcoming articles, we have more to learn.

  • To not to miss the upcoming articles on Terraform, please follow me, subscribe to the newsletter.

Now, let us delete what we’ve just created by just giving command terraform plan -destroy

  • It enables us to know what is going to be removed from the configuration.
  • To actually delete them, give command as terraform destroy -auto-approve

After giving the command we get an output as Destroy Complete! Resources : 2 Destroyed.

Destroy plan

Destroy Apply

Here, the two resources are - VPC and an EC2 Instance which we’ve just created are deleted using the command terraform destroy -auto-approve

In the upcoming article, we’ll be learning the advanced concepts of Terraform. Our follow to learn the Terraform completely is Even Odd system.

For every Odd number article, we perform a practical of it in even number article.

At the end of the series, we’ll perform four projects which will give a complete revision to your terraform learnings.

If you have any queries regarding this article, please let me know through the Commend section.