'Create google storage bucket using terraform and set life cycle rule for the specified buckets

I have a terraform module to create some storage buckets with specified permissions to users now I need to specify some life cycle rules for some of the buckets, and I need to map between two variables as follows:

variable.tf

variable "buckets" {
  description = "List of buckets"
  type        = list(string)
  default     = []
}

variable "bucket_deletion_rules" {
  description = "Ad-hoc Bucket deletion rules"
  type        = set(object({}))
  default     = [{}]
}

bucket.tf

resource "google_storage_bucket" "data_bucket" {
  for_each                    = toset(var.buckets)
  name                        = each.key
  project                     = var.project
  location                    = var.region
  uniform_bucket_level_access = true
  versioning {
    enabled = false
  }

  dynamic "lifecycle_rule" {
    for_each = var.bucket_deletion_rules
    content {
      action {
        type = "Delete"
      }
      condition {
        age  = lifecycle_rule.value
      }
    }
  }
}

The above code does not work correctly! The idea is to define variables like this:

  buckets = [
    "bucket-mhmd-test1",
    "bucket-mhmd-test2",
    "bucket-mhmd-test3"
  ]
  bucket_deletion_rules = [
    {
    "bucket-mhmd-test1" = 50
    "bucket-mhmd-test2" = 10
    }
  ]

I've tried to put a nested for_each inside the dynamic, but I couldn't make it possible to compare the bucket names with bucket deletion rules. So now how I can define the variables and do the equalization between that two variables to apply the rules to the desired buckets?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source