Movatterモバイル変換


[0]ホーム

URL:


HashiConf 2025Don't miss the live stream of HashiConf Day 2 happening now View live stream

The result of an expression is avalue. All values have atype, whichdictates where that value can be used and what transformations can beapplied to it.

Types

The Terraform language uses the following types for its values:

  • string: a sequence of Unicode characters representing some text, like"hello".
  • number: a numeric value. Thenumber type can represent both wholenumbers like15 and fractional values like6.283185.
  • bool: a boolean value, eithertrue orfalse.bool values can be used in conditionallogic.
  • list (ortuple): a sequence of values, like["us-west-1a", "us-west-1c"]. Identify elements in a list with consecutive whole numbers, starting with zero.
  • set: a collection of unique values that do not have any secondary identifiers or ordering.
  • map (orobject): a group of values identified by named labels, like{name = "Mabel", age = 52}.

Strings, numbers, and bools are sometimes calledprimitive types.Lists/tuples and maps/objects are sometimes calledcomplex types,structuraltypes, orcollection types. SeeType Constraints for a more detaileddescription of complex types.

Finally, there is one special value that hasno type:

  • null: a value that representsabsence oromission. If you set anargument of a resource tonull, Terraform behaves as though youhad completely omitted it — it will use the argument's default value if it hasone, or raise an error if the argument is mandatory.null is most useful inconditional expressions, so you can dynamically omit an argument if acondition isn't met.

Literal Expressions

Aliteral expression is an expression that directly represents a particularconstant value. Terraform has a literal expression syntax for each of the valuetypes described above.

Strings

Strings are usually represented by a double-quoted sequence of Unicodecharacters,"like this". There is also a "heredoc" syntax for more complexstrings.

String literals are the most complex kind of literal expression inTerraform, and have their own page of documentation. SeeStringsfor information about escape sequences, the heredoc syntax, interpolation, andtemplate directives.

Numbers

Numbers are represented by unquoted sequences of digits with or without adecimal point, like15 or6.283185.

Bools

Bools are represented by the unquoted symbolstrue andfalse.

Null

The null value is represented by the unquoted symbolnull.

Lists/Tuples

Lists/tuples are represented by a pair of square brackets containing acomma-separated sequence of values, like["a", 15, true].

List literals can be split into multiple lines for readability, but alwaysrequire a comma between values. A comma after the final value is allowed,but not required. Values in a list can be arbitrary expressions.

Lists and tuples each have different constraints on the types they allow. For more information on the types that each allows, refer totuples andlists.

Sets

Terraform does not support directly accessing elements of a set by index because sets are unordered collections. To access elements in a set by index, first convert the set to a list.

  1. Define a set. The following example specifies a set nameexample_set:

    variable "example_set" {  type= set(string)  default= ["foo", "bar"]}
  2. Use thetolist function to convert the set to a list. The following example stores the converted list as a local variable calledexample_list:

    locals {  example_list= tolist(var.example_set)}
  3. You can then reference an element in the list:

    output "first_element" {  value= local.example_list[0]}output "second_element" {  value= local.example_list[1]}

Maps/Objects

Maps/objects are represented by a pair of curly braces containing a series of<KEY> = <VALUE> pairs:

{  name= "John"  age= 52}

Key/value pairs can be separated by either a comma or a line break.

The values in a mapcan be arbitrary expressions.

The keys in a map must be strings; they can be left unquoted ifthey are a valididentifier, but must be quotedotherwise. You can use a non-literal string expression as a key by wrapping it inparentheses, like(var.business_unit_tag_name) = "SRE".

Indices and Attributes

Elements of list/tuple and map/object values can be accessed usingthe square-bracket index notation, likelocal.list[3]. The expression withinthe brackets must be a whole number for list and tuple values or a stringfor map and object values.

Map/object attributes with names that are valid identifiers can also be accessedusing the dot-separated attribute notation, likelocal.object.attrname.In cases where a map might contain arbitrary user-specified keys, we recommendusing only the square-bracket index notation (local.map["keyname"]).

More About Complex Types

In most situations, lists and tuples behave identically, as do maps and objects.Whenever the distinction isn't relevant, the Terraform documentation uses eachpair of terms interchangeably (with a historical preference for "list" and"map").

However, module authors and provider developers should understand thedifferences between these similar types (and the relatedset type), since theyoffer different ways to restrict the allowed values for input variables andresource arguments.

For complete details about these types (and an explanation of why the differenceusually doesn't matter), seeType Constraints.

Type Conversion

Expressions are most often used to set values for the arguments of resources andchild modules. In these cases, the argument has an expected type and the givenexpression must produce a value of that type.

Where possible, Terraform automatically converts values from one type toanother in order to produce the expected type. If this isn't possible, Terraformwill produce a type mismatch error and you must update the configuration with amore suitable expression. Automatic type conversion does not occur when usingtheequality operator.

Terraform automatically converts number and bool values to strings when needed.It also converts strings to numbers or bools, as long as the string contains avalid representation of a number or bool value.

  • true converts to"true", and vice-versa
  • false converts to"false", and vice-versa
  • 15 converts to"15", and vice-versa
Edit this page on GitHub

[8]ページ先頭

©2009-2025 Movatter.jp