AWS SAM template with YAML boolean environment variable
Recently, I reviewed an AWS SAM template in which one of my team members added
an environment variable ENABLE_FOO (name isn’t important) with the value
true.
| |
Unquoted string are common in YAML, but this unquoted true isn’t a string,
it’s a boolean, a subtle difference they had not considered.
This wasn’t a case I had encountered or even considered before and it left me quite skeptical, mostly because I didn’t know what to expect in the actual environment variable.
Said team member had tested true and false values, and confirmed they were
passed as string "true" and "false" respectively.
However, while true and false are the booleans in YAML 1.2, most YAML
parsers will be somewhat compliant with YAML 1.1, which has 22 distinct values
for boolean type.
What would happen in the future when another team member comes along and uses
YES or On instead of true ?
So, that’s what I tested. I passed all those YAML 1.1 boolean values to a simple
python function with returns them in a JSON object from the os.environ dict.
| |
| |
Built it and ran it locally:
% sam build && sam local invoke | jq 'fromjson'
And this is the output. Most values give "true" and "false" as expected, the exceptions being the single letter booleans which give themselves as string value.
| |
All in all, this boolean as environment variable seems like a footgun to me, so we improved our internal SAM template standards with a new rule.
Text environment variables in CF & SAM templates must be set as explicit strings with quotation marks.
Finally, having applied the new rule to the original change, the value of ENABLE_FOO became unmistakably clear.
| |
1 which is also unambiguous and aligns with our best practices, but that was a good little exercise nonetheless.