github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/website/docs/configuration-0-11/syntax.html.md (about)

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