Terraform Core Commands

Looking to take your Terraform workflow to the next level? Check out our comprehensive guide to Terraform Core Commands!

In this post, we’ll highlight some of the most commonly used Terraform Core Commands. So whether you’re just getting started with Terraform or you’re looking to up your game, be sure to check out this guide!

Terraform commands

–help is help

Tab completion enabled by terraform -install-autocomplete

CommandWhat it does?
terraform -install-autocompleteEnables tab completion
-helpShows the help files
applyapply the changes
consolecreates and interactive console
destroydestroys the Terraform-managed infrastructure
fmtused to rewrite Terraform configuration files
force-unlockused to remove lock on the state for the current configuration
getused to download and update modules
graphused to generate a a graph
importused to import existing resources
initused to initilize a working directory
outputused to extract the value of an output variable
planused to create an execution plan
providersused to print information about providers (eg VMware, AWS, HyperV etc etc etc)
pushused to upload your configuration
refreshused to update the state of a file
show useds to provide output from a state or plan file
stateused for advanced state management
taintused to mark resources as tainted
validateused to validate the syntax of terraform files
untaintused to unmark a resourse as tainted
workspaceused to manage workspaces

Subcommands

CommandWhat it does
listused to list all existing workspaces
selectused to choose different workspaces
newused to create a new workspace
deleteused to delete and existing workspace

Terraform Syntax

The syntax of Terraform configurations is called HashiCorp Configuration Language (HCL). It is meant to strike a balance between human readable and editable as well as being machine-friendly. For machine-friendliness, Terraform can also read JSON configurations

HCL Syntax (HashiCorp Configuration Language)

Here is an example of HashiCorp Configuration Language (HCL)

# An AMI
variable "ami" {
description = "the AMI to use"
}
/* A multi
line comment. */
resource "aws_instance" "web" {
ami = "${var.ami}"
count = 2
source_dest_check = false
connection {
user = "root"
}
} 

Source: https://www.terraform.io/docs/configuration/syntax.html

JSON Syntax

Terraform also supports JSON which is a machine friendly language

{
"variable": {
"ami": {
"description": "the AMI to use"
}
},
"resource": {
"aws_instance": {
"web": {
"ami": "${var.ami}",
"count": 2,
"source_dest_check": false,
"connection": {
"user": "root"
}
}
}
}
} 

Source: https://www.terraform.io/docs/configuration/syntax.html

Variables

Type or VariableWhat it does
"var" prefixUser String Variable
Interpolate
"var.MAP["KEY"]User Map Variable
Value of Key
"${var.LIST}"Value of the list as a list
List elements by Index
"self.ATTRIBUTE"Own Resource
"TYPE.NAME.ATTRIBUTE"Other Resource
"data.TYPE.NAME.ATTRIBUTE"Data Source
MODULE.NAME.OUTPUT"Module outputs
"Count.FIELD"Count
"path.TYPE"Path
"terraform.FIELD"Terraform Meta
Elsewhere On TurboGeek:  Who & What is Hashicorp? How Did It Began?

You may also like...

4 Responses

  1. 29/01/2021

    […] Terraform core commands […]

  2. 19/10/2022

    […] is a tool that automates the configuration of infrastructure. It can help you manage your servers, databases, and applications more effectively. It can also […]

  3. 22/10/2022

    […] Terraform Core Commands […]

  4. 19/01/2023

    […] Terraform Core Commands […]

Leave a Reply

Your email address will not be published. Required fields are marked *