Updated on 2023-08-11 GMT+08:00

Basic Syntax

The RFS configuration language is easy, highly readable, and compatible with the HCL and JSON syntax. This section describes the basic syntax and common functions of the HCL.

The RFS configuration language consists of arguments, blocks, expressions, and functions.

Arguments

Use an equal sign (=) to assign a value or expression to a particular name, which can contain letters, digits, underscores (_), and hyphens (-), but cannot start with a digit. For example:

image_id = "ad091b52-742f-469e-8f3c-fd81cadf0743"

Blocks

Aggregate multiple arguments and can contain another blocks. A block consists of type, label, and body. The format is as follows:
resource "myinstance" {
   name   = "myinstance"
    ......
    network {
        uuid = "55534eaa-533a-419d-9b40-ec427ea7195a"
    }
}

Before using a block, you must declare its type (resource and network in this example), where resource is the top-level block type and network is the nested block type. The top-level block type keywords supported by the HCL include provider, resource, data, variable, output, module, and locals.

Block labels are defined after the block type, and the number of block labels is determined by the block type. In the example, the resource block type expects one label: myinstance. The nested network type does not have block labels. The block body is defined at the last and delimited by the { and } characters. Other types can be nested in the block body to implement different layered structures.

Argument Types

The HCL supports the following argument types:

Basic types

  • string: consists of one or more Unicode characters, for example, hello.
  • number: can be an integer or a floating point number.
  • bool: can only be true or false.

The HCL can automatically convert the number and bool types to the string type based on the argument type. If a string can be represented as a value of the number or bool type, it can also be converted to the other two types. Arguments of the three types can be directly assigned values. For example:

disk_type = "SSD"
disk_size = 40
enable    = true

# Strings can be of the number and bool types.
disk_size = "40"
enable    = "true"

Set types

  • map(...): a set of data elements combined using key-value pairs. The key is of the string type, while the value can be of the string, number, or bool type. The values of all elements must be of the same type.
  • list(...): a set of data elements of the same type. The elements can be of the basic type or block type. The list index starts from 0.
  • set(...): similar to the list type. Elements in a set are unique and do not have any auxiliary identifier or sequence.
The map type is delimited in { and } and has flexible types. Key-value pairs can be connected using equal signs (=) or colons (:). If a key does not start with a digit, double quotation marks (") are not required. For multi-line mapping, key-value pairs can be separated by newline characters or commas (,). You are advised to use equal signs (=) to connect key-value pairs and separate them with newline characters. For example:
# Recommended format
tags = {
  foo = "bar"
  key = "value"
}

# Other formats
tags = {"foo" = "bar", "key" = "value"}
tags = {"foo" : "bar", "key" : "value"}
tags = {foo = "bar", key = "value"}
tags = {foo : "bar", key : "value"}
tags = {
  foo : "bar"
  key : "value"
}

The list type and set type are represented in the same way. The list/set whose elements are of the basic type is delimited using [ and ], and the list/set whose elements are of the block type is represented in the form of repeated blocks. For example:

# List whose elements are of the basic type
security_groups = ["default", "internal"]

# List whose elements are of the block type
network {
  uuid = "55534eaa-533a-419d-9b40-ec427ea7195a"
}
network {
  uuid = "ad091b52-742f-469e-8f3c-fd81cadf0743"
}

Special types

  • null: If a parameter is set to null, the parameter has no specified value. The HCL automatically ignores the parameter and uses the default value. Null is common in conditional expressions, for example, var.test==""? null: var.test, indicating that when the value of var.test is "", it is ignored.

Other Syntax

  • A single-line comment starts with # or //.
  • /* and */ are start and end delimiters for a comment that might span over multiple lines. Nested block comments are not supported.
  • Terraform configuration files are UTF-8 encoded. Terraform accepts non-ASCII characters in identifiers, comments, and string values.
  • A multi-line string starts with <<EOF, contains the string content in the middle, and ends with EOF. EOF can also be replaced with other characters. For example:
      ...
      website {
        ...
        routing_rules = <<EOF
    [{
        "Condition": {
            "KeyPrefixEquals": "docs/"
        },
        "Redirect": {
            "ReplaceKeyPrefixWith": "documents/"
        }
    }]
    EOF
      }
    }