user2611591
user2611591

Reputation:

Terraform For each loop on object and pass to child module

I have a AWS s3 lifecycle rule of complex type (object) in my variables.tf file and have assigned it to a variable. Afterwards, I am using for_each loop to iterate over the object and passing this variable from a parent module to a child module where s3 resource is being created where I am stuck and I am not sure if my approach is correct or not. I know that for_each loop only accepts maps and sets but I could not find any resource to convert object to map and I am confused if I should convert object into map in my case.

I am new to Terraform and using Terraform v1.0.5 and the errors below are not of any help which I also tried to search but of no luck. I am trying to implement it for the last two days .I would appreciate if someone could please guide me what I am doing wrong.

Upvotes: 0

Views: 1376

Answers (1)

Nandkishor
Nandkishor

Reputation: 148

Your parent module xyz-parent-module should pass the variable rule_xyz like this: -

module "xyz-parent-module" {
  source = "./aws-module/s3-bucket-module"

  for_each = var.lifecycle_rule_60_days
  rule_xyz = {
    lifecycle_id    = each.value["life_id"]
    prefix_value    = each.value["prefix_val"]
    enabled_value   = each.value["bucket_enabled"]
    expiration_days = {
      days_value = each.value["expiration_list"].expiration_days
    }
  }
}

Upvotes: 2

Related Questions