Quantcast
Channel: Cloud Training Program
Viewing all articles
Browse latest Browse all 1891

Terraform Workflow and Its Use-Case

$
0
0

Terraform is an open-source, cloud-agnostic provisioning tool developed by HashiCorp and written in the GO language. In this blog, we will talk about the workflow of Terraform. It is extremely helpful in a team and can benefit you even if you work individually. A good workflow enables you to streamline a process, organize it, and make it less error-prone.

In this blog, I will be covering the workflow of terraform with an example of how to implement it:

Are you new to Terraform? Check out our blog post on Terraform for beginners

Basic Concept of Terraform Workflow

The workflows of Terraform are built on top of five key steps: Write, Init, Plan, Apply, and Destroy. Nevertheless, their details and actions vary between workflows.

Terraform Workflow

Write – this is where you create changes to the code.

Init – this is where you initialize your code to download the requirements mentioned in your code.

Plan – this is where you review changes and choose whether to simply accept them.

Apply – this is where you accept changes and apply them against real infrastructure.

Destroy – this is where to destroy all your created infrastructure.

Writing Terraform Code

The first thing in the terraform workflow is to start with writing your Terraform configuration just like you write code: in your editor of choice. It’s common practice to store your work in a version-controlled repository even when you’re just operating as an individual.

Note: Check out the blog on Terraform Installation Overview

terraform init

The first thing that you do after writing your code in Terraform is initializing the code using the command terraform init. This command is used to initialize the working directory containing Terraform configuration files. It is safe to run this command multiple times.

You can use the init command for:

  1. Plugin Installation
  2. Child Module Installation
  3. Backend Initialization

Note: If any of the above changes are done in the configuration then we need to run the init command to initialize the working directory again.

Once you run the terraform init command it will begin initializing the directory as shown in the below image.

terraform init

Also read, our blog on Terraform Providers Overview

terraform plan

After a successful initialization of the working directory and the completion of the plugin download, we can create an execution plan using terraform plan command, this is a handy way to check whether the execution plan matches your expectations without making any changes to real resources or to the state.

If the Terraform discovers no changes to resources, then the terraform plan indicates that no changes are required to the real infrastructure.

Terraform also helps to save the plan to a file for later execution with terrafom apply, which can be useful while applying automation with Terraform. This can be achieved by using -out argument.

terraform plan

terraform apply

Terraform apply command is used to create or introduce changes to real infrastructure. By default, apply scans the current working directory for the configuration and applies the changes appropriately. However, you’ll optionally give the path to a saved plan file that was previously created with terraform plan.

If you do not provides a plan file on the instruction, terraform apply will create a replacement plan automatically then prompt for approval to use it. If the created plan does not include any changes to resources or to root module output values then terraform apply will exit immediately, without prompting.

terraform apply

terraform destroy

The terraform destroy is used to destroy infrastructure governed by terraform. To check the behavior of terraform destroy command at any time we can use terraform plan -destroy command.

For the deletion of the particular resource and its dependencies -target flag can be used.

terraform destroy

Demo: Creating an AWS VPC

We will be performing a simple demo of creating an AWS VPC, to see the terraform workflow:

We can write our terraform configuration in Hashicorp Configuration Language (HCL) or in JSON. Terraform language syntax is built around two key syntax construct: arguments and block.

Note: To learn more about VPC check out our blog post on AWS Virtual Private Cloud (VPC)

1. Create a main.tf file in your favorite editor, and paste the provider block it in.

terraform provider

2. Paste the code of vpc resource in main.tf (Note: You can also name your vpc in AWS using name tag as shown below).

resource "aws_vpc" "demo" {
  cidr_block       = "10.0.0.0/16"
  instance_tenancy = "default"

  tags = {
    Name = "my-first-vpc"
  }
}

aws vpc

3. Run the terraform init command to initialize the code, and download the plugin.

terraform init

4. Run terraform apply on the terminal to execute the code, type yes when asked for confirmation.

terraform apply

5. We will get the below output once the vpc is created.

terraform apply

6.  Open AWS console, click on Services, and select VPC.

AWS Console

7. Click on Your VPCs and now check for the created VPC.

VPC

8. The last step is to delete the infrastructure, you can do this by using terraform destroy command.

terraform destroy

Related/References

Join FREE Masterclass

Join our FREE Masterclass to know more about Terraform and get access to all Hands-On labs that you must perform to clear the Terraform Certified Associate certification exam.

Click on the below image to Register Our FREE Masterclass Now!

The post Terraform Workflow and Its Use-Case appeared first on Cloud Training Program.


Viewing all articles
Browse latest Browse all 1891

Trending Articles