User Documentation

What YAML is

Upsun Beta

Access our newest offering - Upsun!

Get your free trial by clicking the link below.

Get your Upsun free trial

YAML is a human-readable format for data serialization. This means it can be used for structured data, like what you can find in configuration files.

Some basic rules about YAML files:

  • YAML files end in .yaml. Some other systems use the alternative .yml extension.
  • YAML is case-sensitive.
  • YAML is whitespace-sensitive and indentation defines the structure, but it doesn’t accept tabs for indentation.
  • Empty lines are ignored.
  • Comments are preceded by an octothorpe #.

Data types Anchor to this heading

YAML represents data through three primitive data structures:

  • Scalars (strings/numbers/booleans)
  • Mappings (dictionaries/objects)
  • Sequences (arrays/lists)

Scalars (strings/numbers/booleans) Anchor to this heading

The most straightforward data structure involves defining key–value pairs where the values are strings or integers.

So you could have a basic configuration for an app:
name: app
type: "golang:1.18"
disk: 1024

This results in three key–value pairs:

Key Value
name app
type golang:1.18
disk 1024

You can define strings either with or without quotes, which can be single ' or double ". Quotes let you escape characters (if double) and make sure the value is parsed as a string when you want it.

For example, you might be representing version numbers and want to parse them as strings. If you use version: 1.10, it’s parsed as an integer and so is treated the same as 1.1. If you use version: "1.10", it’s parsed as a string and isn’t treated as the same as 1.1.

Mappings (dictionaries/objects) Anchor to this heading

In addition to basic scalar values, each key can also represent a set of other key–value pairs. So you can define entire dictionaries of pairs.

The structure of the mapping is determined by the indentation. So children are indented more than parents and siblings have the same amount of indentation. The exact number of spaces in the indentation isn’t important, just the level relative to the rest of the map.

In contrast, when you define mappings, the order doesn’t matter.

So you could expand the configuration from before to add another mapping:
name: app
type: "golang:1.18"
disk: 1024

        start: ./bin/app
            passthru: true
            allow: false

This creates a web dictionary that has two dictionaries within it: commands and locations, each with their own mappings:

  • webcommandsstart: ./bin/app
  • weblocations'/'passthru: true and allow: false

Sequences (arrays/lists) Anchor to this heading

In addition to maps defining further key–value pairs, you can also use sequences to include lists of information.
                - index.html
                - index.htm
            passthru: true
            allow: false

You can also define sequences using a flow syntax:
            index: [index.html, index.htm]
            passthru: true
            allow: false

In either case, you get a list of values within index:

weblocations'/'indexindex.html and index.htm

Define multi-line strings Anchor to this heading

If you have a long string that spans multiple lines, use a pipe | to preserve line breaks. The new lines need to have at least the same indentation as the first (you can add more indentation that’s then preserved).

So you could add a multi-line string to a build key in the hooks map:
    build: |
        set -e
        cp a.txt b.txt        

And the resulting value preserves the line break. This lets you do things like enter small shell scripts within a YAML file.

hooksbuildset -e and cp a.txt b.txt

Reuse content Anchor to this heading

YAML supports internal named references, known as anchors, which can be referenced using an alias. This allows you to reuse YAML blocks in multiple places within a single file.

Define an anchor by adding &<NAME> to the start of a value, where <NAME> is a unique identifier. The anchor represents this entire value. Then refer to the anchor using *<NAME>.

The following example shows 4 different workers:
    queue1: &runner
        size: S
            start: python
    queue2: *runner
        <<: *runner
        size: M
        <<: *runner
        disk: 512
  • queue1 and queue2 are identical with the same size and commands properties.
  • queue3 is the same as queue1 except that it has a different value for size.
  • queue4 is the same as queue1 except that it has the disk property.

Note that you need to place an alias with <<: at the same level as the other keys within that value.

What’s next Anchor to this heading

Is this page helpful?