FEATURES OPEN SOURCE ABOUT DOCS LOGIN REGISTER

Configuration 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. For general Terraform configurations, however, we recommend using the HCL Terraform syntax.

Terraform Syntax

Here is an example of Terraform’s HCL syntax:

# 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"
  }
}

Basic bullet point reference:

In addition to the basics, the syntax supports hierarchies of sections, such as the “resource” and “variable” in the example above. These sections are similar to maps, but visually look better. For example, these are nearly equivalent:

variable "ami" {
  description = "the AMI to use"
}

# is equal to:

variable = [{
  "ami": {
    "description": "the AMI to use",
  }
}]

Notice how the top stanza visually looks a lot better? By repeating multiple variable sections, it builds up the variable list. When possible, use sections since they’re visually clearer and more readable.

JSON Syntax

Terraform also supports reading JSON formatted configuration files. The above example converted to JSON:

{
	"variable": {
		"ami": {
			"description": "the AMI to use"
		}
	},

	"resource": {
		"aws_instance": {
			"web": {
				"ami": "${var.ami}",
				"count": 2,
				"source_dest_check": false,

				"connection": {
					"user": "root"
				}
			}
		}
	}
}

The conversion should be pretty straightforward and self-documented.

The downsides of JSON are less human readability and the lack of comments. Otherwise, the two are completely interoperable.


See the source of this document at Terraform.io