2014-07-28 19:43:00 +02:00
|
|
|
---
|
|
|
|
layout: "docs"
|
|
|
|
page_title: "Configuration Syntax"
|
|
|
|
sidebar_current: "docs-config-syntax"
|
2014-10-22 05:21:56 +02:00
|
|
|
description: |-
|
2017-04-05 17:29:27 +02:00
|
|
|
The syntax of Terraform configurations is custom. 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 Terraform
|
|
|
|
syntax.
|
2014-07-28 19:43:00 +02:00
|
|
|
---
|
|
|
|
|
|
|
|
# Configuration Syntax
|
|
|
|
|
2016-07-12 00:37:51 +02:00
|
|
|
The syntax of Terraform configurations is called [HashiCorp Configuration
|
|
|
|
Language (HCL)](https://github.com/hashicorp/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.
|
2014-07-28 19:43:00 +02:00
|
|
|
|
|
|
|
## Terraform Syntax
|
|
|
|
|
2016-07-12 00:37:51 +02:00
|
|
|
Here is an example of Terraform's HCL syntax:
|
2014-07-28 19:43:00 +02:00
|
|
|
|
2017-04-05 17:29:27 +02:00
|
|
|
```hcl
|
2014-07-28 19:43:00 +02:00
|
|
|
# An AMI
|
|
|
|
variable "ami" {
|
2016-07-12 00:37:51 +02:00
|
|
|
description = "the AMI to use"
|
2014-07-28 19:43:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* A multi
|
|
|
|
line comment. */
|
|
|
|
resource "aws_instance" "web" {
|
2016-07-12 00:37:51 +02:00
|
|
|
ami = "${var.ami}"
|
|
|
|
count = 2
|
|
|
|
source_dest_check = false
|
2014-07-28 19:43:00 +02:00
|
|
|
|
2016-07-12 00:37:51 +02:00
|
|
|
connection {
|
|
|
|
user = "root"
|
|
|
|
}
|
2014-07-28 19:43:00 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Basic bullet point reference:
|
|
|
|
|
|
|
|
* Single line comments start with `#`
|
|
|
|
|
|
|
|
* Multi-line comments are wrapped with `/*` and `*/`
|
|
|
|
|
|
|
|
* Values are assigned with the syntax of `key = value` (whitespace
|
2016-07-12 00:37:51 +02:00
|
|
|
doesn't matter). The value can be any primitive (string,
|
|
|
|
number, boolean), a list, or a map.
|
2014-07-28 19:43:00 +02:00
|
|
|
|
|
|
|
* Strings are in double-quotes.
|
|
|
|
|
|
|
|
* Strings can interpolate other values using syntax wrapped
|
|
|
|
in `${}`, such as `${var.foo}`. The full syntax for interpolation
|
2016-07-12 00:37:51 +02:00
|
|
|
is [documented here](/docs/configuration/interpolation.html).
|
2014-07-28 19:43:00 +02:00
|
|
|
|
2015-10-05 18:38:52 +02:00
|
|
|
* Multiline strings can use shell-style "here doc" syntax, with
|
2016-06-12 19:57:13 +02:00
|
|
|
the string starting with a marker like `<<EOF` and then the
|
|
|
|
string ending with `EOF` on a line of its own. The lines of
|
2015-10-05 18:38:52 +02:00
|
|
|
the string and the end marker must *not* be indented.
|
|
|
|
|
2014-07-28 19:43:00 +02:00
|
|
|
* Numbers are assumed to be base 10. If you prefix a number with
|
|
|
|
`0x`, it is treated as a hexadecimal number.
|
|
|
|
|
2015-01-25 05:38:15 +01:00
|
|
|
* Boolean values: `true`, `false`.
|
2014-07-28 19:43:00 +02:00
|
|
|
|
2016-07-12 00:37:51 +02:00
|
|
|
* Lists of primitive types can be made with square brackets (`[]`).
|
|
|
|
Example: `["foo", "bar", "baz"]`.
|
2014-07-28 19:43:00 +02:00
|
|
|
|
2016-07-12 00:37:51 +02:00
|
|
|
* Maps can be made with braces (`{}`) and colons (`:`):
|
|
|
|
`{ "foo": "bar", "bar": "baz" }`. Quotes may be omitted on keys, unless the
|
|
|
|
key starts with a number, in which case quotes are required. Commas are
|
|
|
|
required between key/value pairs for single line maps. A newline between
|
|
|
|
key/value pairs is sufficient in multi-line maps.
|
2014-07-28 19:43:00 +02:00
|
|
|
|
|
|
|
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:
|
|
|
|
|
2017-04-05 17:29:27 +02:00
|
|
|
```hcl
|
2014-07-28 19:43:00 +02:00
|
|
|
variable "ami" {
|
2016-07-12 00:37:51 +02:00
|
|
|
description = "the AMI to use"
|
2014-07-28 19:43:00 +02:00
|
|
|
}
|
2017-04-05 17:29:27 +02:00
|
|
|
```
|
2014-07-28 19:43:00 +02:00
|
|
|
|
2017-04-05 17:29:27 +02:00
|
|
|
is equal to:
|
2014-07-28 19:43:00 +02:00
|
|
|
|
2017-04-05 17:29:27 +02:00
|
|
|
```hcl
|
2014-07-28 19:43:00 +02:00
|
|
|
variable = [{
|
2016-07-12 00:37:51 +02:00
|
|
|
"ami": {
|
|
|
|
"description": "the AMI to use",
|
|
|
|
}
|
2014-07-28 19:43:00 +02:00
|
|
|
}]
|
|
|
|
```
|
|
|
|
|
2014-09-30 20:32:25 +02:00
|
|
|
Notice how the top stanza visually looks a lot better? By repeating
|
2015-01-14 18:38:08 +01:00
|
|
|
multiple `variable` sections, it builds up the `variable` list. When
|
2014-09-30 20:32:25 +02:00
|
|
|
possible, use sections since they're visually clearer and more readable.
|
2014-07-28 19:43:00 +02:00
|
|
|
|
|
|
|
## JSON Syntax
|
|
|
|
|
|
|
|
Terraform also supports reading JSON formatted configuration files.
|
|
|
|
The above example converted to JSON:
|
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
2017-04-05 17:29:27 +02:00
|
|
|
"variable": {
|
|
|
|
"ami": {
|
|
|
|
"description": "the AMI to use"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
"resource": {
|
|
|
|
"aws_instance": {
|
|
|
|
"web": {
|
|
|
|
"ami": "${var.ami}",
|
|
|
|
"count": 2,
|
|
|
|
"source_dest_check": false,
|
|
|
|
|
|
|
|
"connection": {
|
|
|
|
"user": "root"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-28 19:43:00 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
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.
|