DEV Community

BPB Online
BPB Online

Posted on

Terraform Workflow: Simplified

Terraform is an open-source "Infrastructure as Code" tool by HashiCorp and is written in the GO language.

Terraform is an infrastructure automation tool that uses high-level configuration language previously called HashiCorp Configuration Language (HCL) to describe the desired state of the infrastructure on multiple cloud or on-premise environments. From this configuration, Terraform then generates a plan to reach the desired state and then executes the plan for infrastructure provisioning.

Currently, Terraform is one of the most popular open-source, cloud-agnostic Infrastructure-as-code (IaC) tools because it uses simple syntax, provisions infrastructure across multiple cloud and on-premises, and safely re-provision infrastructure for any configuration changes. Terraform is mostly used by DevOps engineers to automate infrastructure creation.

Terraform workflow
Terraform core workflow consists of lifecycle stages init, plan, apply, and destroy. These stages are executed as commands to perform the operation expected by them.

Image description

Here’s the description and command for the execution of these stages in the Terraform workflow:

Terraform init runs a number of initialization tasks to enable Terraform for use in the current working directory. This is usually executed only once per session.
# Initialize Terraform
$ terraform init

Terraform plan compares the desired Terraform state with the current state in the cloud and builds and displays an execution plan for infrastructure creation or destruction. This command is just for the creation of a plan for you to verify/review the work done by you and your teammates; it does not change the deployment. So, you can update the Terraform configuration if the plan is not as per your requirements.
$ terraform plan

Terraform apply executes the plan that is created, comparing the current and desired states. This will create or destroy your resources, which means it potentially changes the deployment by adding or removing resources based on changes in the plan. Terraform apply will run the plan command automatically to execute the most recent changes. However, you can always tell it to execute a specific terraform plan command by providing a plan file.
$ terraform apply

Terraform destroy will delete all resources that are created/managed by the Terraform environment you are working in.
$ terraform destroy

These are important sets of commands that you cannot work without, and they are dependent on the sequences shown in figure.

Hope this was helpful.

Discussion (0)