Introduction

Terraform is one of the most popular Infrastructure-as-code (IaC) tool, used by DevOps teams to automate infrastructure tasks. It is used to automate the provisioning of your cloud resources. Terraform is an open-source, cloud-agnostic provisioning tool developed by HashiCorp and written in GO language.

STEP 1:

Create one EC2 instance in AWS based on the requirements.

STEP 2:

Login to the server and download the terraform using the official link: https://developer.hashicorp.com/terraform/install

We can download Terraform based on the Operating system.

For ex: Linux (Ubuntu/Debian)

wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg –dearmor -o /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

STEP 3:

Check the version using the below command.

terraform –version

STEP 4:

Using Terraform we can create infrastructure for GCP, AWS, KUBERNETES, Azure, Alibaba Cloud and ORACLE Cloud infrastructure.

Now we are going to create AWS infrastructure using Terraform.

STEP 5:

In the Terraform official website there is an option called Registry In that option there are multiple cloud providers. Now we are going to create infrastructure in AWS. Click on AWS.

STEP 6:

Download the VisualStudio code and create one file in the example.tf format

Now we are going to create code for VPC creation in AWS.

Below is the sample code for EC2 creation In the Terraform official website there is an option called Registry under this we can select the provider under that there is an option called Documentation. We need to select the stable version. We can get the sample code for all Infrastructure creation like EC2, VPC, SG, Nategateway, Internet gateway, subnet, route table, s3, IAM.. etc

STEP 7:

We need to configure AWS CLI in server using below command.

apt install awscli
aws configure
After this command we need to mention the access key and secret access key or another way we can create a roles with ec2 full access and attach the role to the ec2 instance.

STEP 8:

In the server create one directory under that directory create one file example.tf.

Copy the below code inside the example.tf.

# Terraform Block
terraform {
  required_providers {
    aws = {
      source  = “hashicorp/aws”
      version = “~> 3.0”
    }
  }
}
 
# Provider Block
provider “aws” {
  region  = “ap-south-1”
  profile = “default”
}
 
# Create EC2 Instance
resource “aws_instance” “my-ec2-vm” {
  ami               = “ami-079b5e5b3971bd10d”
  instance_type     = “t2.micro”
  availability_zone = “ap-south-1b”
  #availability_zone = “ap-south-1a”
  tags = {
    “Name” = “web”
    “tag1” = “Update-test-1”   
  }
}

STEP 9:

The Terraform init prepares your working directory for use with Terraform, ensuring that all necessary dependencies are available and correctly configured.

terraform init

STEP 10:

The purpose of the below command is to preview the changes Terraform will make to your infrastructure, without actually applying them.

terraform plan

STEP 11:

The terraform apply is the command that actually makes changes to your infrastructure, based on the execution plan you’ve created and confirmed.

terraform apply

STEP 12:

The Terraform destroy is a powerful command that completely removes all resources managed by Terraform. It should be used with care, especially in production environments, as it will permanently delete infrastructure

terraform destroy

Conclusion

Terraform is a robust and flexible tool for managing infrastructure as code. Its ability to work across multiple cloud providers, its clear execution plans, and its state management make it a vital tool for modern DevOps practices. Whether you’re managing small environments or large-scale infrastructure, Terraform provides the tools needed to define, deploy, and maintain infrastructure efficiently and reliably.

Leave a Reply