Learn how to handle optional attributes like missing permission entries when parsing JSON in Terraform using jsondecode and the try function to safely assign defaults.
---
This video is based on the question https://stackoverflow.com/q/79385710/ asked by the user 'user438431' ( https://stackoverflow.com/u/438431/ ) and on the answer https://stackoverflow.com/a/79388486/ provided by the user 'Matthew Schuchard' ( https://stackoverflow.com/u/5343387/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: terraform: how to allow variable (missing) attributes using jsondecode
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to drop me a comment under this video.
---
Introduction
When working with JSON data in Terraform, sometimes certain keys might be missing, such as an optional permissions list. Terraform's jsondecode function will parse the JSON, but missing keys can cause issues when you expect a list or object.
This post explains how to safely handle such missing attributes using the try function to ensure your Terraform code can process JSON data regardless of these absent keys.
Problem Scenario
Given a JSON structure where some nodes have a permissions attribute (a list) and others do not:
[[See Video to Reveal this Text or Code Snippet]]
You need to flatten and iterate over this data using Terraform locals and resources, but the missing permissions key on some entries can cause errors like:
[[See Video to Reveal this Text or Code Snippet]]
This typically happens when Terraform expects a value of a certain type but finds a null or missing key.
The Solution: Use try to Provide a Safe Default
Terraform's try function attempts to evaluate an expression and falls back to the next if it fails. This allows you to provide a default value when an attribute is missing or otherwise inaccessible.
How it works:
[[See Video to Reveal this Text or Code Snippet]]
If folder.permissions exists, use its value.
If folder.permissions is missing or causes an error, default to an empty list [].
This technique avoids type errors and ensures your local values always have consistent types, which helps in flattening lists and mapping over them in resources.
Improved Terraform Local Example
[[See Video to Reveal this Text or Code Snippet]]
This way, permissions will always be a list (empty or with elements), preventing type conflicts downstream.
Summary
Use try(expression, fallback) in Terraform to handle missing keys gracefully.
For JSON-decoded data, this is crucial when optional lists or maps might be absent.
Assigning an empty list as a fallback for missing permissions ensures consistent data structure.
Adopting this pattern will make your Terraform configurations more robust when working with variable JSON structures.