Terraform Overview

Terraform Overview

ยท

3 min read

Infrastructure as a system

Hello Everyone, In this article, we will be covering Terraform (IaaS) First of all, let us understand what is Terraform?

  • It is used to manage and automate your infrastructure and platform and services that run on that platform
  • It's an open-source tool for infrastructure provisioning provisioning infrastructure is like create VPC, spin up servers, create AWS users and permissions and so on...
  • It can be used to automate provisioning, configuring and managing infrastructure
  • Terraform is relatively new, more advanced in orchestration
  • It uses declarative language(declarative = define what result you want) For example
    %[region="us-east-1โ€]
    

    How does it work?

    Terraform architecture has two important input sources
  • TF-config(what to create and configure) using the main.tf file we can create a config file with the help of declarative language we can declare all the things we want
  • state (current state vs the desired state) the cores are responsible for monitoring the state and looking at whether it matches with the desired state or not and working to match the state the user is looking for as soon as there is a change in the desired state we can apply and get the same as the current state

intro-terraform-workflow.png

Terraform command for different stages

  • refresh: It queries the infrastructure provider to get the current state
    terraform refresh
    
  • plan: creates an execution plan (what needs to be created/updated/destroyed)
    terraform plan
    
  • apply: executes the plan
    terraform apply
    
  • destroy: destroy the resources/infrastructure
    terraform destroy
    
    let's get some hands-on demo using the commands here we are going to launch an EC2 instance using Terraform will write the code using Hashicorp configuration language(HCl)

blog.png

prerequisite:

An AWS account.

Your AWS credentials

Configure the AWS CLI from your terminal.

blog4.png

Configuration basics

Step 1 : Open any editor of your choice and in my case, I am using VS code create a file with (.tf extension)

provider "aws" { 
    region="us-east-1"
}
//The provider block configures the specified provider, in this case, aws. 
A provider is a plugin that Terraform uses to create and manage your resources.//

resource "aws_instance" "terraform_ec2_example" {
    ami = "ami-062f7200baf2fa504"
    instance_type = "t2.micro"
    tags = {
        Name = "Terraform EC2"
    }
//Use resource blocks to define components of your infrastructure.
 A resource might be a physical or virtual component such as an EC2 instance//
}

Step 2 : Open the terminal and type in

Terraform init //Initialize the directory

The terraform init command is used to initialize a working directory containing Terraform configuration files. This is the first command that should be run after writing a new Terraform configuration Most Terraform providers are published separately from Terraform as plugins. During init, Terraform searches the configuration for both direct and indirect references to providers and attempts to install the plugins for those providers. terraform init will automatically find, download, and install the necessary provider plugins. In our case, the provider is AWS

step 3: plan command

Terraform plan

The terraform plan command creates an execution plan, which lets you preview the changes that Terraform plans to make to your infrastructure. It gives you the output in terms of +-~ Everything with + sign will get added Everything with - sign will get removed Everything with - sign will get modified

plan.png

step 4: apply command Theterraform apply command executes the actions proposed in a Terraform plan. It will ask you to confirm the changes type, yes and the EC2 instance will be created go back to your AWS Account and check the Instances section

Congratulations You have created an EC2 instance using Terraform ๐Ÿ‘๐Ÿป

ย