DEV Community


Terraform basic

krpmuruga profile image murugan ・2 min read


Terraform is an open-source infrastructure as a Code tool developed by HashiCorp. It is used to define and provision the complete infrastructure using an easy-to-learn declarative language.

It is an infrastructure provisioning tool where you can store your cloud infrastructure setup as codes. It’s very similar to tools such as CloudFormation, which you would use to automate your AWS infrastructure, but you can only use that on AWS. With Terraform, you can use it on other cloud platforms as well.

Terraform Lifecycle:

Terraform lifecycle consists of – init, plan, apply, and destroy.

  1. Terraform init initializes the (local) Terraform environment. Usually executed only once per session.

  2. Terraform plan compares the Terraform state with the as-is state in the cloud, build and display an
    execution plan. This does not change the deployment (read-only).

  3. Terraform apply executes the plan. This potentially changes the deployment.

  4. Terraform destroy deletes all resources that are governed by this specific terraform environment.

Terraform Core Concepts

Variables: Also used as input-variables, it is key-value pair used by Terraform modules to allow customization.

Provider: It is a plugin to interact with APIs of service and access its related resources.

Module: It is a folder with Terraform templates where all the configurations are defined

State: It consists of cached information about the infrastructure managed by Terraform and the related configurations.

Resources: It refers to a block of one or more infrastructure objects (compute instances, virtual networks, etc.), which are used in configuring and managing the infrastructure.

Data Source: It is implemented by providers to return information on external objects to terraform.

Output Values: These are return values of a terraform module that can be used by other configurations.

Plan: It is one of the stages where it determines what needs to be created, updated, or destroyed to move from real/current state of the infrastructure to the desired state

Apply: It is one of the stages where it applies the changes real/current state of the infrastructure in order to move to the desired state.

Terraform Command Line Interface

terraform –version -> Shows terraform version installed

Initialize infrastructure:

Command Description
terraform init Initialize a working directory
terraform init -input=true Ask for input if necessary
terraform init -lock=false Disable locking of state files during state-related operations


Command Description
terraform get downloads and update modules mentioned in the root module
terraform get -update=true modules already downloaded will be checked for updates and updated

Provision infrastructure:

Command Description
terraform plan Creates an execution plan
terraform apply Executes changes to the actual environment
terraform apply –auto-approve Apply changes without being prompted to enter "yes"

Format and validate Terraform Code:

Command Description
terraform fmt Format code as per HCL canonical standard
terraform validate validate configuration files for syntax

Terraform State Manipulation:

Command Description
terraform state list list all resources in the state file
terraform state mv move an item in the state file
terraform state rm Remove items from the state file
terraform state pull Pull current state and output to stdout
terraform state push Update remote state from a local state file
terraform state refresh



Discussion (1)

ashutoshkrris profile image
Ashutosh Krishna

Hey there !
I've built a blog website called iRead.
Would you like to write your blogs there?
You can add backlinks to your original works also.
Know More :
Demo Video :

Forem Open with the Forem app