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