Hi All,
I am trying to create a Google Storage buckets with below block on .tfvars and the respective implementation code. I some have I am unable to make the dynamic block work which is being used to add the multiple life cycle rules to a storage bucket.
.tfvars code :
# Cloud storage buckets
storage_buckets = [
# this 1st bucket is only defined in DEV tf vars. reason: this bucket is a onetime creation for all DWH cloud artifacts under ecx-cicd-tools project.
{
name = "ecx-dwh-artefacts"
localtion = "AUSTRALIA-SOUTHEAST1"
force_destroy = false
project = "ecx-cicd-tools"
storage_class = "STANDARD"
versioning = {
enabled = false
}
labels = {
app = "alation"
project = "resetx"
team = "dwh"
}
uniform_bucket_level_access = false
folders = ["alation/","alation/packages/","alation/packages/archive/",
"alation/backups/","alation/backups/data/","alation/backups/data/DEV/","alation/backups/data/PROD/"]
lifecycle_rule = {}
}
,
{
name = "eclipx-dwh-dev"
localtion = "AUSTRALIA-SOUTHEAST1"
force_destroy = false
project = "eclipx-dwh-dev"
storage_class = "STANDARD"
versioning = {}
labels = {
app = "dataflow"
project = "resetx"
team = "dwh"
}
uniform_bucket_level_access = false
folders = ["Data/","Data/stagingCustomDataFlow/","Data/temp/","Data/templatesCustomDataFlow/"]
lifecycle_rule = {}
},
{
name = "ecx-dwh-artefacts-test"
localtion = "AUSTRALIA-SOUTHEAST1"
force_destroy = false
project = "ecx-cicd-tools"
storage_class = "STANDARD"
versioning = {
enabled = false
}
labels = {
app = "alation"
project = "resetx"
team = "dwh"
}
uniform_bucket_level_access = false
folders = ["alation/","alation/packages/","alation/packages/archive/",
"alation/backups/","alation/backups/data/","alation/backups/data/DEV/","alation/backups/data/PROD/"]
# this can have multiple rules like rule_1 , rule_2
lifecycle_rule = {
rule_1 = {
action = {
type = "Delete"
}
condition = {
matches_suffix = ["_backup.tar.gz"]
}
}
}
}
]
Implementation logic :
resource "google_storage_bucket" "cloud_storage" {
for_each = {for gcs in var.storage_buckets : gcs.name => gcs}
name = each.value.name
location = lookup(each.value, "location", "AUSTRALIA-SOUTHEAST1")
project = data.google_project.existing_projects[each.value.project].project_id
force_destroy = lookup(each.value, "force_destroy", false)
storage_class = lookup(each.value, "storage_class", "STANDARD")
labels = merge(
lookup(each.value, "labels", {}),
{
managed_by = "terraform"
}
)
dynamic "versioning" {
for_each = [for version in [lookup(each.value, "versioning", null)] : version if version != null]
content {
enabled = lookup(versioning.value, "enabled", true)
}
}
dynamic "lifecycle_rule" {
# for_each = [for rule in [lookup(each.value, "lifecycle_rule", null)] : rule if rule != null]
for_each = [for rule in [lookup(each.value, "lifecycle_rule", null)] : rule if rule != null]
content {
action {
type = lifecycle_rule.value["action"]["type"]
storage_class = lookup(lifecycle_rule.value["action"], "storage_class", null)
}
condition {
# matches_suffix = lookup(lifecycle_rule.value["condition"], "matches_suffix", null)
age = lookup(lifecycle_rule.value["condition"], "age", null)
}
}
}
Below are the errors I am facing :
Error: Invalid index
│
│ on storage.tf line 43, in resource “google_storage_bucket” “cloud_storage”:
│ 43: type = lifecycle_rule.value[“action”][“type”]
│ ├────────────────
│ │ lifecycle_rule.value is empty map of object
│
│ The given key does not identify an element in this collection value.
╵
╷
│ Error: Invalid index
│
│ on storage.tf line 43, in resource “google_storage_bucket” “cloud_storage”:
│ 43: type = lifecycle_rule.value[“action”][“type”]
│ ├────────────────
│ │ lifecycle_rule.value is map of object with 1 element
│
│ The given key does not identify an element in this collection value.
╵
╷
│ Error: Invalid index
│
│ on storage.tf line 43, in resource “google_storage_bucket” “cloud_storage”:
│ 43: type = lifecycle_rule.value[“action”][“type”]
│ ├────────────────
│ │ lifecycle_rule.value is empty map of object
│
│ The given key does not identify an element in this collection value.
╵
╷
│ Error: Invalid index
│
│ on storage.tf line 44, in resource “google_storage_bucket” “cloud_storage”:
│ 44: storage_class = lookup(lifecycle_rule.value[“action”], “storage_class”, null)
│ ├────────────────
│ │ lifecycle_rule.value is empty map of object
│
│ The given key does not identify an element in this collection value.
╵
╷
│ Error: Invalid index
│
│ on storage.tf line 44, in resource “google_storage_bucket” “cloud_storage”:
│ 44: storage_class = lookup(lifecycle_rule.value[“action”], “storage_class”, null)
│ ├────────────────
│ │ lifecycle_rule.value is map of object with 1 element
│
│ The given key does not identify an element in this collection value.
╵
╷
│ Error: Invalid index
│
│ on storage.tf line 44, in resource “google_storage_bucket” “cloud_storage”:
│ 44: storage_class = lookup(lifecycle_rule.value[“action”], “storage_class”, null)
│ ├────────────────
│ │ lifecycle_rule.value is empty map of object
│
│ The given key does not identify an element in this collection value.
╵
╷
│ Error: Invalid index
│
│ on storage.tf line 48, in resource “google_storage_bucket” “cloud_storage”:
│ 48: age = lookup(lifecycle_rule.value[“condition”], “age”, null)
│ ├────────────────
│ │ lifecycle_rule.value is empty map of object
│
│ The given key does not identify an element in this collection value.
╵
╷
│ Error: Invalid index
│
│ on storage.tf line 48, in resource “google_storage_bucket” “cloud_storage”:
│ 48: age = lookup(lifecycle_rule.value[“condition”], “age”, null)
│ ├────────────────
│ │ lifecycle_rule.value is map of object with 1 element
│
│ The given key does not identify an element in this collection value.
╵
╷
│ Error: Invalid index
│
│ on storage.tf line 48, in resource “google_storage_bucket” “cloud_storage”:
│ 48: age = lookup(lifecycle_rule.value[“condition”], “age”, null)
│ ├────────────────
│ │ lifecycle_rule.value is empty map of object
│
│ The given key does not identify an element in this collection value.
Can someone please assist to make the dynamic block logic get corrected ? rest of the code is working fine, its just that dynamic block have issues
Thank you.