All articles

YAML Configuration Best Practices for Developers

5 min read
YAMLconfigurationvalidation

YAML is popular for configuration because it is compact and pleasant to read. Docker Compose, GitHub Actions, Kubernetes, CI pipelines, and many application frameworks use it. The same flexibility that makes YAML convenient can also make mistakes hard to spot.

Indentation is structure

YAML uses indentation to define nesting. Two lines that look visually close may represent completely different structures if one has an extra space. Choose two spaces or four spaces and keep the file consistent. Avoid tabs, because many YAML parsers reject them or treat them unpredictably.

Quote values that look ambiguous

Some values can be interpreted in surprising ways depending on the YAML version and parser. Strings like yes, no, on, off, and dates can be converted into booleans or timestamps. If the value is meant to stay a string, quote it explicitly.

Use arrays consistently

YAML supports block arrays with dashes and inline arrays with brackets. Block arrays are easier to review in pull requests because each item appears on its own line. Inline arrays are fine for short values, but large inline structures quickly become harder to read than JSON.

Separate secrets from normal config

YAML files often end up in source control, issue trackers, and chat screenshots. Do not store API keys, passwords, private tokens, or production credentials in ordinary YAML configuration. Prefer environment variables, secret managers, or encrypted configuration systems.

Keep comments helpful and current

Comments are one reason teams choose YAML over JSON for configuration. Use comments to explain why a setting exists, not merely what the key name already says. Outdated comments are worse than no comments because they create false confidence during reviews.

Validate generated YAML

Many CI systems and deployment tools fail because a generated YAML file is syntactically valid but semantically wrong. After templating, validate the final rendered YAML rather than only validating the template source. This catches missing variables, bad indentation, and unexpected empty values.

Convert to JSON when debugging shape

Converting YAML to JSON is a useful way to confirm the structure a parser sees. If the JSON output is not what you expected, the YAML indentation or scalar interpretation is probably wrong. This trick is especially helpful for nested CI and Kubernetes configuration.