github.com/ns1/terraform@v0.7.10-0.20161109153551-8949419bef40/website/source/docs/configuration/syntax.html.md (about) 1 --- 2 layout: "docs" 3 page_title: "Configuration Syntax" 4 sidebar_current: "docs-config-syntax" 5 description: |- 6 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. 7 --- 8 9 # Configuration Syntax 10 11 <a id="hcl"></a> 12 13 The syntax of Terraform configurations is called [HashiCorp Configuration 14 Language (HCL)](https://github.com/hashicorp/hcl). It is meant to strike a 15 balance between human readable and editable as well as being machine-friendly. 16 For machine-friendliness, Terraform can also read JSON configurations. For 17 general Terraform configurations, however, we recommend using the HCL Terraform 18 syntax. 19 20 ## Terraform Syntax 21 22 Here is an example of Terraform's HCL syntax: 23 24 ``` 25 # An AMI 26 variable "ami" { 27 description = "the AMI to use" 28 } 29 30 /* A multi 31 line comment. */ 32 resource "aws_instance" "web" { 33 ami = "${var.ami}" 34 count = 2 35 source_dest_check = false 36 37 connection { 38 user = "root" 39 } 40 } 41 ``` 42 43 Basic bullet point reference: 44 45 * Single line comments start with `#` 46 47 * Multi-line comments are wrapped with `/*` and `*/` 48 49 * Values are assigned with the syntax of `key = value` (whitespace 50 doesn't matter). The value can be any primitive (string, 51 number, boolean), a list, or a map. 52 53 * Strings are in double-quotes. 54 55 * Strings can interpolate other values using syntax wrapped 56 in `${}`, such as `${var.foo}`. The full syntax for interpolation 57 is [documented here](/docs/configuration/interpolation.html). 58 59 * Multiline strings can use shell-style "here doc" syntax, with 60 the string starting with a marker like `<<EOF` and then the 61 string ending with `EOF` on a line of its own. The lines of 62 the string and the end marker must *not* be indented. 63 64 * Numbers are assumed to be base 10. If you prefix a number with 65 `0x`, it is treated as a hexadecimal number. 66 67 * Boolean values: `true`, `false`. 68 69 * Lists of primitive types can be made with square brackets (`[]`). 70 Example: `["foo", "bar", "baz"]`. 71 72 * Maps can be made with braces (`{}`) and colons (`:`): 73 `{ "foo": "bar", "bar": "baz" }`. Quotes may be omitted on keys, unless the 74 key starts with a number, in which case quotes are required. Commas are 75 required between key/value pairs for single line maps. A newline between 76 key/value pairs is sufficient in multi-line maps. 77 78 In addition to the basics, the syntax supports hierarchies of sections, 79 such as the "resource" and "variable" in the example above. These 80 sections are similar to maps, but visually look better. For example, 81 these are nearly equivalent: 82 83 ``` 84 variable "ami" { 85 description = "the AMI to use" 86 } 87 88 # is equal to: 89 90 variable = [{ 91 "ami": { 92 "description": "the AMI to use", 93 } 94 }] 95 ``` 96 97 Notice how the top stanza visually looks a lot better? By repeating 98 multiple `variable` sections, it builds up the `variable` list. When 99 possible, use sections since they're visually clearer and more readable. 100 101 ## JSON Syntax 102 103 Terraform also supports reading JSON formatted configuration files. 104 The above example converted to JSON: 105 106 ```json 107 { 108 "variable": { 109 "ami": { 110 "description": "the AMI to use" 111 } 112 }, 113 114 "resource": { 115 "aws_instance": { 116 "web": { 117 "ami": "${var.ami}", 118 "count": 2, 119 "source_dest_check": false, 120 121 "connection": { 122 "user": "root" 123 } 124 } 125 } 126 } 127 } 128 ``` 129 130 The conversion should be pretty straightforward and self-documented. 131 132 The downsides of JSON are less human readability and the lack of 133 comments. Otherwise, the two are completely interoperable.