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
Command | What it does? |
---|---|
terraform -install-autocomplete | Enables tab completion |
-help | Shows the help files |
apply | apply the changes |
console | creates and interactive console |
destroy | destroys the Terraform-managed infrastructure |
fmt | used to rewrite Terraform configuration files |
force-unlock | used to remove lock on the state for the current configuration |
get | used to download and update modules |
graph | used to generate a a graph |
import | used to import existing resources |
init | used to initilize a working directory |
output | used to extract the value of an output variable |
plan | used to create an execution plan |
providers | used to print information about providers (eg VMware, AWS, HyperV etc etc etc) |
push | used to upload your configuration |
refresh | used to update the state of a file |
show | useds to provide output from a state or plan file |
state | used for advanced state management |
taint | used to mark resources as tainted |
validate | used to validate the syntax of terraform files |
untaint | used to unmark a resourse as tainted |
workspace | used to manage workspaces |
Subcommands
Command | What it does |
---|---|
list | used to list all existing workspaces |
select | used to choose different workspaces |
new | used to create a new workspace |
delete | used 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 Variable | What it does |
---|---|
"var" prefix | User 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 |
4 Responses
[…] Terraform core commands […]
[…] is a tool that automates the configuration of infrastructure. It can help you manage your servers, databases, and applications more effectively. It can also […]
[…] Terraform Core Commands […]
[…] Terraform Core Commands […]