Going through terraform common workflow commands.

Going through terraform common workflow commands.

In this blog, we will walk through four common Terraform commands that define the workflow of provisioning resources in the cloud.

These commands are:

  1. terraform init

  2. terraform plan

  3. terraform apply

  4. terraform destroy

What is terraform?

Terraform is an Infrastructure as Code tool developed by HashiCorp that helps DevOps engineers provision cloud infrastructure using a high-level language called HashiCorp Configuration Language (HCL).

It supports multiple cloud providers, including popular ones like AWS, Azure, GCP, OCI, and many more. It also supports private cloud environments built on VMware, OpenStack, and CloudStack.

Thus, Terraform is useful for managing hybrid and multi-cloud environments. By using the same tool and language to manage resources across various cloud environments, it ensures consistency in deployments.

A demo Terraform file:

# Configure the AWS Provider
provider "aws" {
  region = "us-west-2" # Specify the AWS region
}

# Create an EC2 instance
resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0" # Amazon Linux 2 AMI
  instance_type = "t2.micro"
    }
}

terraform init

This command initializes the working directory in your local environment. It downloads all the necessary packages and configuration files needed to interact with your specified cloud provider in the main.tf file. For example, if your cloud provider is AWS, it will download all the files and packages required to make changes in your AWS environment.

terraform plan

This command shows the changes Terraform would make to match the desired state of infrastructure specified in the main.tf file. It informs you about all the resources that would be added, updated, or deleted once the main.tf file is applied. It is a good practice to use this command before executing the main.tf file.

terraform apply

This command executes the changes required to achieve the infrastructure mentioned in the main.tf file. Based on the differences between the current state and the desired state, it performs the necessary operations to create, update, or delete resources.

terraform destroy

This command removes all the resources managed by Terraform. It destroys all the infrastructure defined in your configuration files.