github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/website/docs/language/expressions/types.mdx (about)

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