0

我的 terragrunt/terraform 代码有如下问题。我不知道检索for_each循环创建的两个爬虫的正确方法。

通常我用 for 和 创建它count。我无法在我的操作触发器 (main.tf) 中检索正确的值。

terragrunt 文件(输入):

inputs = {
  glue_crawler = {
      crawler    = {
        crawler_name     = "test",
        description      = "test crawler"
        },
      crawler1   = {
        crawler_name     = "test2",
        description      = "test2 crawler"
        }
    }
}

主文件

#crawler declaration
resource "aws_glue_crawler" "default" {
  for_each = var.glue_crawler

  database_name = aws_glue_catalog_database.database.name
  name          = "Crawler_${each.value.crawler_name}"
  description   = each.value.description
  role          = aws_iam_role.svc-glue-crawler.id
  table_prefix  = "raw_"
  tags          = var.tags

  s3_target {
    path = "${var.s3_glue_name}/${each.value.crawler_name}"
  }

 configuration = jsonencode(var.crawler_configuration)
}

...

#trigger
resource "aws_glue_trigger" "my_trigger" {
  name         = var.trigger_name
  schedule     = "cron(00 01 * * ? *)"
  type         = "SCHEDULED"
  enabled      = "false"
  tags         = var.tags

  actions {
    job_name   = aws_glue_crawler.default[0].name
  }

  actions {
    job_name   = aws_glue_crawler.default[1].name
  }

变量.tf

variable "glue_crawler" {
  type = map(object({
    crawler_name = string
    description   = string
  }))
  default     = {}
  description = "glue crawler definitions."
}

当我运行此代码时,出现以下错误:

Error: Invalid index

  on main.tf line 294, in resource "aws_glue_trigger" "my_trigger":  294:     job_name   = aws_glue_crawler.default[0].name
    |----------------
    | aws_glue_crawler.default is object with 2 attributes

The given key does not identify an element in this collection value.


Error: Invalid index

  on main.tf line 298, in resource "aws_glue_trigger" "my_trigger":  298:     job_name   = aws_glue_crawler.default[1].name
    |----------------
    | aws_glue_crawler.default is object with 2 attributes

The given key does not identify an element in this collection value.
4

1 回答 1

1

当您使用for_each而不是count您需要使用键而不是索引访问特定元素时。所以这将是crawlerandcrawler1而不是0and1在你的例子中:

resource "aws_glue_crawler" "default" {
  for_each = var.glue_crawler

  database_name = aws_glue_catalog_database.database.name
  name          = "Crawler_${each.value.crawler_name}"
  description   = each.value.description
  role          = aws_iam_role.svc-glue-crawler.id
  table_prefix  = "raw_"
  tags          = var.tags

  s3_target {
    path = "${var.s3_glue_name}/${each.value.crawler_name}"
  }

 configuration = jsonencode(var.crawler_configuration)
}

...

#trigger
resource "aws_glue_trigger" "my_trigger" {
  name         = var.trigger_name
  schedule     = "cron(00 01 * * ? *)"
  type         = "SCHEDULED"
  enabled      = "false"
  tags         = var.tags

  actions {
    job_name   = aws_glue_crawler.default["crawler"].name
  }

  actions {
    job_name   = aws_glue_crawler.default["crawler1"].name
  }
}

但当然,这只适用于特定的输入。相反,您应该考虑使actions参数动态化并for_each在此处也使用爬虫:

resource "aws_glue_trigger" "my_trigger" {
  name         = var.trigger_name
  schedule     = "cron(00 01 * * ? *)"
  type         = "SCHEDULED"
  enabled      = "false"
  tags         = var.tags

  dynamic "actions" {
    for_each = aws_glue_crawler.default

    content {
      job_name = actions.name
    }
  }
}
于 2020-11-23T12:42:18.880 回答