0

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?

1 Answers1

0

A dynamic block can only generate arguments that belong to the resource type, data source, provider or provisioner being configured. It is not possible to generate meta-argument blocks such as lifecycle and provisioner blocks, since Terraform must process these before it is safe to evaluate expressions.

This is from the docs and might explain why it's not working properly. To set gcp lifecycle policies dynamically you could make use a module (e.g. this)