Home > Mobile >  How to create multiple glue crawlers from terraform at the same time
How to create multiple glue crawlers from terraform at the same time

Time:10-14

I am deploying an array of crawlers from terraform, I want to have a list of 2 or more crawlers to deploy at the same time. I am using a foreach but for the dynamodb_target I can't find how to associate it to a specific crawler, that is, crawler 1 has table_name 1 as dynamodb_target an so on.

main.tf

resource "aws_glue_crawler" "example" {
  for_each = var.crawlerList

    database_name = each.value.database_name
    name          = each.value.crawler_name
    role          = each.value.role
    
  dynamodb_target {
    path = var.table_name
  }
}

variables.tf

variable "table_name" {
  type        = string
  description = "The name of the DynamoDB table to crawler."
}



variable "crawlerList" {
  type = map(object({
    database_name = string
    crawler_name  = string
    role          = string
  }))
}


var.tfvars


table_name = "ref_master_files"


crawlerList = {
  fisrt = ({
    database_name = "dm_web"
    crawler_name  = "example_one"
    role          = "xxxx"
  }),
  second = ({
    database_name = "dm_web"
    crawler_name  = "example_two"
    role          = "xxxx"
  }),
  third = ({
    database_name = "dm_web"
    crawler_name  = "example_third"
    role          = "xxxx"
  }),
}


Result

enter image description here

CodePudding user response:

It's little difficult to get what you want to achieve so I have two options.

  1. You want to assign every crawler different table_name. Then - expand your variable for another value:
variable "crawlerList" {
  type = map(object({
    database_name = string
    crawler_name  = string
    role          = string
    table_name    = string
  }))
}
  1. You have some table_names variable of multiple variables which you didn't attach. Then what you need to is somehow match them.

It could be something like this:

tables = ["table1", "table2"]
matches = {"table1": "first", "table2":"second"}

Then you would iterate like this:

resource "aws_glue_crawler" "example" {
  for_each = var.matches

    database_name = var.crawler_list[each.value].database_name
    name          = var.crawler_list[each.value].crawler_name
    role          = var.crawler_list[each.value].role
    
  dynamodb_target {
    path = var.tables[each.key]
  }
}
  • Related