Member-only story

Generating Dynamic JSON in Terraform

Manu Magalhães
8 min readMar 18, 2023

--

Bring some movement to your JSON (Image Credit: Ahmad Odeh)

Available for free in English & Portuguese at my personal website.

In this article I’ll use Step Functions as a case study, but you can do it with whatever resources you want.

What we’re going to do

The following JSON will become dynamic. We’ll replace static values with:

  1. A variable
  2. A dynamic list
  3. One or more dynamic objects

(I’ve added non idiomatic comments inside the code blocks, but it’s just to show what exactly we’re doing.)

# Reference JSON
{
"Comment": "My state machine",
"StartAt": "Choice",
"States": {
"Handle Notification": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"OutputPath": "$.Payload",
"Parameters": {
"Payload.$": "$",
"FunctionName": "my_function_name" <-- Case 1: Replace with dynamic string
},
"End": true
},
"Choice": {
"Type": "Choice",
"Choices": [ -------
{ |
"IsPresent": true, |
"Next": "SSM Execution-InstanceId", |
"Variable"…

--

--

Manu Magalhães
Manu Magalhães

Responses (1)