Skip to main content

YAML Crash Course

YAML (which playfully stands for YAML Ain't Markup Language) is a human-readable data serialization language. Unlike JSON or XML, it doesn't use complex structural brackets or closing tags. Instead, YAML relies entirely on whitespace indentation and clean key-value syntax to map out structural hierarchies. It natively supports deeply nested objects, data arrays (lists), multi-line strings, and inline developer comments—making your Infrastructure as Code (IaC) incredibly clean and maintainable.

Key Takeaways

Structural Mechanics & Data Type Definitions

  • Indentation-Based Hierarchy: In YAML, space characters are everything. Sub-items or nested configurations are defined by indenting them further to the right relative to their parent keys. Crucial rule: You must use regular spaces for indentation, never tab characters. Tapping Tab will break the CloudFormation compilation compiler and crash your deployment instantly.
  • Key-Value Pairs: The absolute baseline building block. It maps a text key to a value using a colon followed by an mandatory space character (e.g., InstanceType: t3.micro).
  • Nested Maps / Objects: Created by stepping down a line and indenting a collection of sub-keys underneath a master key.
  • Arrays / Collections (Lists): Declared using a clean dash (-) followed by a space. This represents a collection item. A single block can house multiple dashes to create sequence lists.
  • Multi-line Text Blocks: If you are writing extensive startup bootstrap code (like an EC2 UserData shell script), you use the literal block operator vertical bar (|). This instructs the engine to preserve all line breaks and spacing exactly as written.
  • Inline Developer Comments: Declared using the hash symbol (#). Anything written on that line after the hash is treated as documentation and skipped by the CloudFormation deployment machine.

JSON vs YAML

❌ The JSON Pattern (Messy & Strict)

{
"Resources": {
"MyInstance": {
"Type": "AWS::EC2::Instance",
"Properties": {
"InstanceType": "t2.micro",
"SecurityGroups": [ "sg-123", "sg-456" ]
}
}
}
}

The YAML Pattern (Clean & Readable)

Resources:
MyInstance:
Type: "AWS::EC2::Instance"
Properties:
InstanceType: "t2.micro"
SecurityGroups: # This is a sequence array list
- "sg-123"
- "sg-456"

Exam Tips

  • The Tab Character Failure Trap: If an exam question describes a scenario where an engineer is tracking down why a newly coded CloudFormation template keeps throwing a parsing validation exception before it even talks to AWS, the issue is almost always a formatting error. Look for options pointing out that tab characters were accidentally used for nesting indentation instead of valid standard spaces.
  • Comments Capability: Remember that YAML supports inline developer notes (#), whereas standard JSON specifications do not natively support comments. This makes YAML highly favored in production pipelines for documented infrastructure setups.

Practice Test

Question: A cloud software developer is auditing a legacy AWS CloudFormation template. The template contains a list of subnet IDs associated with an Application Load Balancer resource block. When reading the YAML configuration, the developer notices the values are structured using consecutive prefix lines starting with a dash (-). Which programmatic data structure type is being defined by this syntax marker?

  • A. A multi-line literal text block
  • B. A conditional logic string map
  • C. A sequence array list
  • D. A nested structural map root key

Correct Answer: C. In YAML formatting syntax, consecutive lines prefixed with a dash (-) character represent items belonging to an sequential array list or collection set. CloudFormation parses this structure as an array input type.