github.com/iaas-resource-provision/iaas-rpc@v1.0.7-0.20211021023331-ed21f798c408/website/docs/language/expressions/types.html.md (about)

     1  ---
     2  layout: "language"
     3  page_title: "Types and Values - Configuration Language"
     4  ---
     5  
     6  # Types and Values
     7  
     8  The result of an expression is a _value_. All values have a _type_, which
     9  dictates where that value can be used and what transformations can be
    10  applied to it.
    11  
    12  ## Types
    13  
    14  The Terraform language uses the following types for its values:
    15  
    16  * `string`: a sequence of Unicode characters representing some text, like
    17    `"hello"`.
    18  * `number`: a numeric value. The `number` type can represent both whole
    19    numbers like `15` and fractional values like `6.283185`.
    20  * `bool`: a boolean value, either `true` or `false`. `bool` values can be used in conditional
    21    logic.
    22  * `list` (or `tuple`): a sequence of values, like
    23    `["us-west-1a", "us-west-1c"]`. Elements in a list or tuple are identified by
    24    consecutive whole numbers, starting with zero.
    25  * `map` (or `object`): a group of values identified by named labels, like
    26    `{name = "Mabel", age = 52}`.
    27  
    28  Strings, numbers, and bools are sometimes called _primitive types._ Lists/tuples and maps/objects are sometimes called _complex types,_ _structural types,_ or _collection types._
    29  
    30  Finally, there is one special value that has _no_ type:
    31  
    32  * `null`: a value that represents _absence_ or _omission._ If you set an
    33    argument of a resource or module to `null`, Terraform behaves as though you
    34    had completely omitted it — it will use the argument's default value if it has
    35    one, or raise an error if the argument is mandatory. `null` is most useful in
    36    conditional expressions, so you can dynamically omit an argument if a
    37    condition isn't met.
    38  
    39  ## Literal Expressions
    40  
    41  A _literal expression_ is an expression that directly represents a particular
    42  constant value. Terraform has a literal expression syntax for each of the value
    43  types described above.
    44  
    45  ### Strings
    46  
    47  Strings are usually represented by a double-quoted sequence of Unicode
    48  characters, `"like this"`. There is also a "heredoc" syntax for more complex
    49  strings.
    50  
    51  String literals are the most complex kind of literal expression in
    52  Terraform, and have their own page of documentation. See [Strings](./strings.html)
    53  for information about escape sequences, the heredoc syntax, interpolation, and
    54  template directives.
    55  
    56  ### Numbers
    57  
    58  Numbers are represented by unquoted sequences of digits with or without a
    59  decimal point, like `15` or `6.283185`.
    60  
    61  ### Bools
    62  
    63  Bools are represented by the unquoted symbols `true` and `false`.
    64  
    65  ### Null
    66  
    67  The null value is represented by the unquoted symbol `null`.
    68  
    69  ### Lists/Tuples
    70  
    71  Lists/tuples are represented by a pair of square brackets containing a
    72  comma-separated sequence of values, like `["a", 15, true]`.
    73  
    74  List literals can be split into multiple lines for readability, but always
    75  require a comma between values. A comma after the final value is allowed,
    76  but not required. Values in a list can be arbitrary expressions.
    77  
    78  ### Maps/Objects
    79  
    80  Maps/objects are represented by a pair of curly braces containing a series of
    81  `<KEY> = <VALUE>` pairs:
    82  
    83  ```hcl
    84  {
    85    name = "John"
    86    age  = 52
    87  }
    88  ```
    89  
    90  Key/value pairs can be separated by either a comma or a line break.
    91  
    92  The values in a map
    93  can be arbitrary expressions.
    94  
    95  The keys in a map must be strings; they can be left unquoted if
    96  they are a valid [identifier](/docs/language/syntax/configuration.html#identifiers), but must be quoted
    97  otherwise. You can use a non-literal string expression as a key by wrapping it in
    98  parentheses, like `(var.business_unit_tag_name) = "SRE"`.
    99  
   100  ## Indices and Attributes
   101  
   102  [inpage-index]: #indices-and-attributes
   103  
   104  Elements of list/tuple and map/object values can be accessed using
   105  the square-bracket index notation, like `local.list[3]`. The expression within
   106  the brackets must be a whole number for list and tuple values or a string
   107  for map and object values.
   108  
   109  Map/object attributes with names that are valid identifiers can also be accessed
   110  using the dot-separated attribute notation, like `local.object.attrname`.
   111  In cases where a map might contain arbitrary user-specified keys, we recommend
   112  using only the square-bracket index notation (`local.map["keyname"]`).
   113  
   114  ## More About Complex Types
   115  
   116  In most situations, lists and tuples behave identically, as do maps and objects.
   117  Whenever the distinction isn't relevant, the Terraform documentation uses each
   118  pair of terms interchangeably (with a historical preference for "list" and
   119  "map").
   120  
   121  However, module authors and provider developers should understand the
   122  differences between these similar types (and the related `set` type), since they
   123  offer different ways to restrict the allowed values for input variables and
   124  resource arguments.
   125  
   126  For complete details about these types (and an explanation of why the difference
   127  usually doesn't matter), see [Type Constraints](/docs/language/expressions/type-constraints.html).
   128  
   129  ## Type Conversion
   130  
   131  Expressions are most often used to set values for the arguments of resources and
   132  child modules. In these cases, the argument has an expected type and the given
   133  expression must produce a value of that type.
   134  
   135  Where possible, Terraform automatically converts values from one type to
   136  another in order to produce the expected type. If this isn't possible, Terraform
   137  will produce a type mismatch error and you must update the configuration with a
   138  more suitable expression.
   139  
   140  Terraform automatically converts number and bool values to strings when needed.
   141  It also converts strings to numbers or bools, as long as the string contains a
   142  valid representation of a number or bool value.
   143  
   144  * `true` converts to `"true"`, and vice-versa
   145  * `false` converts to `"false"`, and vice-versa
   146  * `15` converts to `"15"`, and vice-versa
   147