Home > database >  Terraform access list within map
Terraform access list within map

Time:07-21

I would like to create one crawler for each element in config (content1, content2), which should crawl the respective paths. I have managed to create two crawlers, but each one iterates over 5 paths in total and repeats the same ones over an over again. Crawler content2 should only crawl a single path. How can I achieve that?

I have the following object:

module1/main.tf

config = {
    content1 = {
        paths = ["a", "b", "c", "d"]
    }
    content2 = {
        paths = ["e"]
    }
}

I would like to access this in glue/main.tf:

locals {
    elements = flatten([
        for element in var.config: [
            for p in element["paths"]: {
                path = p
           }
        ]
    ])
}

resource "aws_glue_crawler" "example" {
    for_each = var.config
    database_name = each.key
    name = each_key
    role = var.my_role

    dynamic "s3_target {
        for_each = { for index, value in local.elements: index => value }
        content { 
            path = s3_target.value.topic
        }
    }
}

CodePudding user response:

You don't need locals for that. You can do this directly with var.config:

resource "aws_glue_crawler" "example" {
    for_each = var.config
    database_name = each.key
    name = each.key
    role = var.my_role

    dynamic "s3_target" {
        for_each = each.value.paths
        content { 
            path = s3_target.value
        }
    }
}
  • Related