Home > database >  Terraform For each loop on object and pass to child module
Terraform For each loop on object and pass to child module

Time:03-14

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.

Parent module: (variables.tf)

variable "lifecycle_rule_60_days" {
 type = object({
  life_id        = string
  prefix_val     = string
  bucket_enabled = bool
  expiration_list = object({
  expiration_days = number
})
})
 default = {
  life_id        = "abc"
  prefix_val     = "test"
  bucket_enabled = true
  expiration_list = {
  expiration_days = 20
    }
  }
}

Parent module: (main.tf)

module "xyz-parent-module" {
 source = "./aws-module/s3-bucket-module"
  for_each        = var.lifecycle_rule_60_days
   lifecycle_id   = each.value["life_id"]
   prefix_value   = each.value["prefix_val"]
   enabled_value  = each.value["bucket_enabled"]
   days_value     = each.value["expiration_days"]
}

Child module - s3-bucket-module: (variables.tf)

variable "rule_xyz" {
  type = object({
    lifecycle_id      = string
    prefix_value      = string
    enabled_value     = bool
    expiration_days   = object({
      days_value      = number
    })
  })

  default = {
    lifecycle_id      = "testing-bucket"
    prefix_value      = "dev"
    enabled_value     = true
    expiration_days   = {
      days_value      = 60
    }
  }
}

Child module - s3-bucket-module: (main.tf)

resource "aws_s3_bucket" "bucket_a" {
  bucket = "bucket_a"

    for_each  = var.rule_xyz
      id      = each.value["lifecycle_id"]
      prefix  = each.value["prefix_value"]
      enabled = each.value["enabled_value"]
      days    = each.value["days_value"]

}

After terraform plan I get the following errors:

Error: Unsupported argument in module "xyz-parent-module" 
An argument named "lifecycle_id" is not expected here. 
Error: Unsupported argument in module "xyz-parent-module" 
An argument named "prefix_value" is not expected here. 
Error: Unsupported argument in module "xyz-parent-module" 
An argument named "enabled_value" is not expected here 
Error: Unsupported argument in module "xyz-parent-module" 
An argument named "days_value" is not expected here

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.

CodePudding user response:

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
    }
  }
}
  • Related