Skip to content

Instantly share code, notes, and snippets.

@justinmklam
Created September 23, 2022 23:11
Show Gist options
  • Save justinmklam/12e70cff2a46f7ee721f6cf0a8393186 to your computer and use it in GitHub Desktop.
Save justinmklam/12e70cff2a46f7ee721f6cf0a8393186 to your computer and use it in GitHub Desktop.
Demo of transforming a complex object into a flattened list for use with for_each.
locals {
tables = {
interval_usage = ["read_end_datetime", "ingest_datetime"]
bill_detail = ["ingest_datetime"]
}
table_resources = { for _entry in flatten([
for k, v in local.tables : [
for partition in v: {
entity = k
partition_key = partition
}
]
]) : "${_entry.entity}.${_entry.partition_key}" => _entry}
}
# Resulting data object:
# table_resources = {
# "interval_usage.read_end_datetime" = {"entity": "interval_usage", "partition_key": "read_end_datetime"},
# "interval_usage.ingest_datetime" = {"entity": "interval_usage", "partition_key": "ingest_datetime"},
# "bill_detail.ingest_datetime" = {"entity": "bill_detail", "partition_key": "ingest_datetime"},
# }
resource "null_resource" "test" {
for_each = local.table_resources
triggers = {
timestamp = timestamp()
}
provisioner "local-exec" {
command = "echo key=${each.key},entity=${each.value.entity},partition_key=${each.value.partition_key}"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment