An Introduction to Terraform

Posted By :Mahendra Singh |30th October 2020

    Infrastructure-as-Code (IaC) has become mainstream now among DevOps Engineers, IT Professionals, and Cloud Engineers. It is a process of managing and provisioning our IT infrastructure.

Here, we are going to learn about Terraform, Hashicorp is the creator of the Terraform It is an open-source infrastructure as Code tool and one of the most popular tools used to build, automate, versioning, and manage our infrastructure efficiently on public clouds like Amazon Web Services, Microsoft Azure, and Google Cloud Platform, etc. Cloud providers have also their own tools to manage infrastructure as code like CloudFormation(AWS), Azure Resource Manager templates(Azure), and deployment manager(GCP). If we talk about Terraform It's very popular and can work safely and efficiently. Terraform uses its own declarative language HCL(Hashicorp Configuration Language) which is very easy to work with it.

 

Terraform Architecture -

 

So mainly Terraform has two components that make up its architecture.

 

1.Terraform core -  Core uses two inputs sources in order to do its job first one is the Tf-config file written by the user where user can define what to create and configure and the second one is a state where Terraform keeps the up-to-date state of how the current setup of the infrastructure looks like. So basically core takes input and figures out the plan what needs to be done so it compares the configuration between the current state and desire state and does the required update according to the desired state in order to infrastructure setup.
 
2.Providers - Second part of the architecture is the providers for specific technologies for example, cloud providers like AWS, GCP, or other infrastructure as a service platform. Terraform has hundreds of providers for different technologies and each provider gives terraform user access to its resources so through AWS provider, you have hundreds of AWS resources access like EC2, IAM, etc.
And this way terraform helps you provision and manage the complete application setup from infrastructure. So once the core creates an execution plan based on input from the config file and states it then uses providers for specific technologies to execute the plan to connect to those platforms and carry out those execution steps.


Workflow/Lifecycle of Terraform

 

So how do you make Terraform make action it has commands you can execute to go through different stages and Terraform consists of these stages.
 

* Init - Terraform init to initialize the working directory where the user can write the configuration.

  $Terraform init

* Plan - It creates an execution plan that determines what actions are necessary to achieve the desired state.

  $terraform plan

* Apply - This is the command where actual execution happened . so with apply you can execute the plan.

  $terraform apply

* Destroy - This will destroy the whole setup removing one by one in the right order and cleaning up all the resources/infrastructure.

  $terraform destroy

 

Terraform Basics - Azure Citadel

Here Let's start with the setup and basic configuration of Terraform.

* Installation

 Download and install Terraform binary package from the Official Download Page of Hashicorp and move this executable binary in a folder to your operating system path.

Once this completed, check the version

   $ terraform -v
        Terraform v0.12.24

* Creating Our First Project By Provision AWS EC2 Instance Using Terraform.

 

So here, we are going to launch a AWS compute machine(Ec2) using terraform.

1. Create a working directory.

  $ mkdir aws-ec2-test

2. Go to this directory and create a configuration file that has steps to define providers and resources of AWS.

        provider "aws" {
     access_key = "A5KG6Fe5G5UD"
     secret_key = "S4gb656765ejYSJA4YZ+T6GY7H"
     region = "us-east-1"
    }
      resource "aws_instance" "terraform_test" {
      ami = "ami-0a634ae95e119863"
      instance_type = "t2.micro"
   }

 

3. Now initialize, plan, and apply terraform for the execution.

   $ terraform init
   $ terraform plan

   $ terraform apply

Go to your AWS EC2 dashboard, and you will see a new instance with the same name and id. now You have successfully launched an AWS EC2 instance using Terraform.


4. If you want to delete the above-deployed resources on AWS, can destroy it.

    $ terraform destroy

Now go to your AWS Ec2 dashboard, and you will see your newly created instance is deleted.

 

Conclusion

As we have seen Terraform can be used to orchestrate infrastructure to build, provision, and manage across multiple cloud providers(private or public).
Today It has become mainstream now among DevOps Engineers, IT Professionals, and Cloud Engineers.


About Author

Mahendra Singh

Mahendra is a DevOps Engineer with hands-on experience supporting automating and optimizing deployments in AWS, configuration management, and DevOps processes with Ansible, Github, Docker, Python, Nginx, and Linux.

Request For Proposal

[contact-form-7 404 "Not Found"]

Ready to innovate ? Let's get in touch

Chat With Us