cromega
08/12/2025, 11:20 AMcromega
08/12/2025, 11:22 AMvars:
subnet_ids: !terraform.state vpc ".subnets[] | select(.group == ""someservice"") | .id"
OR
vars:
subnet_ids: !terraform.state vpc "[.subnets[] | select(.group == ""someservice"") | .id]"cromega
08/12/2025, 11:26 AMcromega
08/12/2025, 11:26 AMcromega
08/12/2025, 11:27 AMAndriy Knysh (Cloud Posse)
08/12/2025, 1:57 PMsubnet_ids: !terraform.state vpc "[.subnets[] | select(.group == ""someservice"") | .id] | tojson"Andriy Knysh (Cloud Posse)
08/12/2025, 1:57 PMcromega
08/12/2025, 2:01 PM"db_subnets": "null\nnull\nnull"Andriy Knysh (Cloud Posse)
08/12/2025, 2:20 PMAndriy Knysh (Cloud Posse)
08/12/2025, 2:20 PMAndriy Knysh (Cloud Posse)
08/12/2025, 2:21 PMAndriy Knysh (Cloud Posse)
08/12/2025, 2:22 PMsubnet_ids: !terraform.state vpc "([.subnets[]? | select(.group == ""someservice"") | .id] | map(select(. != null))) | tojson"Andriy Knysh (Cloud Posse)
08/12/2025, 2:22 PMvpc output should have the following:Andriy Knysh (Cloud Posse)
08/12/2025, 2:23 PM.subnets
• .group
• each subnet should have .idcromega
08/12/2025, 2:24 PMsubnets:
"service-0": {
"id": id,
"cidr_block": "1.2.3.0/24",
"group": "service"
}
}cromega
08/12/2025, 2:24 PMAndriy Knysh (Cloud Posse)
08/12/2025, 2:35 PMsubnets is a list of maps?cromega
08/12/2025, 2:36 PMAndriy Knysh (Cloud Posse)
08/12/2025, 2:36 PMsubnet_ids: !terraform.state vpc "([.subnets? // {} | .[] | select(.group == ""service"") | .id] | map(select(. != null)) | tojson)"Andriy Knysh (Cloud Posse)
08/12/2025, 2:37 PMWhy this works:
.subnets? // {} handles missing subnets.
.[] iterates the map's values.
select(.group == "service") filters.
| .id extracts IDs.
[...] collects into one array.
map(select(. != null)) drops any nulls.
tojson returns a proper JSON list (["subnet-aaa","subnet-bbb"]) Terraform reads as list(string).cromega
08/13/2025, 9:52 AM"[.subnets[] | select(.group == ""someservice"") | .id] | tojson" gives me following values in the generated terraform.tfvars.json:
service:
db_subnets: |-
null
null
null
the values get passed as a multi-line stringcromega
08/13/2025, 9:52 AMcromega
08/13/2025, 9:54 AMcromega
08/13/2025, 9:55 AMcat subnets.json| yq '[.subnets[] | select(.group == "admin") | .id] | tojson'
[
"subnet-...",
"subnet-...",
"subnet-..."
]