Dynamic block of lifecycle_rule not working in terraform 1.0.6

The following block within a google_cloud_storage resource

  dynamic "lifecycle_rule" {
    for_each = var.bucket_lifecycle_rule
    content {
      condition {
        age        = lifecycle_rule.age
        with_state = lifecycle_rule.with_state
      }
      action {
        type          = lifecycle_rule.type
        storage_class = lifecycle_rule.storage_class
      }
    }
  }

with the following variable declaration


variable "bucket_lifecycle_rule" {
  description = "Lifecycle rules"
  type = list(object({
    age           = string
    with_state    = string
    type          = string
    storage_class = string
  }))
  default = [
    {
      age           = 30
      with_state    = "ANY"
      type          = "SetStorageClass"
      storage_class = "COLDLINE"
    },
    {
      age           = 120
      with_state    = "ANY"
      type          = "Delete"
      storage_class = ""
    },
  ]
}

errors out as follows:

│ Error: Unsupported attribute
│
│   on main.tf line 18, in resource "google_storage_bucket" "my_bucket":
│   18:         age        = lifecycle_rule.age
│
│ This object does not have an attribute named "age".
╵
╷
│ Error: Unsupported attribute
│
│   on main.tf line 19, in resource "google_storage_bucket" "my_bucket":
│   19:         with_state = lifecycle_rule.with_state
│
│ This object does not have an attribute named "with_state".
╵
╷
│ Error: Unsupported attribute
│
│   on main.tf line 22, in resource "google_storage_bucket" "my_bucket":
│   22:         type          = lifecycle_rule.type
│
│ This object does not have an attribute named "type".
╵
╷
│ Error: Unsupported attribute
│
│   on main.tf line 23, in resource "google_storage_bucket" "my_bucket":
│   23:         storage_class = lifecycle_rule.storage_class
│
│ This object does not have an attribute named "storage_class".

why is that?

Hi @panteliskar,

Inside a dynamic block the iterator symbol (lifecycle_rule in your case) is always an object with two attributes key and value. The value attribute contains the value of the current element of the source collection.

In your case then, where your elements are themselves objects, you can access them through lifecycle_rule.value, using expressions like lifecycle_rule.value.age.

(Since your for_each collection in this case is a list, lifecycle_rule.key will be the current element index, which is not typically useful but included for completeness/consistency. The key attribute plays a more significant role when the for_each collection is a map.)