Warm tip: This article is reproduced from serverfault.com, please click

amazon web services-创建多重资源的循环地形

(amazon web services - Terraform for loop with creating of multiply resources)

发布于 2020-11-30 00:00:38

我有简单glue jobaws
这是一个例子:

resource "aws_glue_job" "myjob1" {
  name     = "myjob1"
  role_arn = var.role_arn

  command {
    name = "pythonshell"
    python_version = 3
    script_location = "s3://mybucket/myjob1/run.py"
  }
}

它正在工作,但是如果我有类似list的东西myjob1,myjob2,myjob3,myjob4,myjob5
可能是bash的一个神秘例子:

listjobs="myjob1 myjob2 myjob3 myjob4 myjob5"

for i in ${listjobs}; do
resource "aws_glue_job" "$i" {
  name     = "$i"
  role_arn = var.role_arn

  command {
    name = "pythonshell"
    python_version = 3
    script_location = "s3://mybucket/$i/run.py"
  }
}
done

在Terraform中是真实的?

Questioner
Piduna
Viewed
0
Piduna 2020-11-30 15:27:32

我通过解决了这个问题for loopvariables.tf

variable "list_of_jobs" {
  default = ["myjob1","myjob2","myjob3"]
}

glue.tf

resource "aws_glue_job" "this" {
  for_each = toset(var.list_of_jobs)
  name     = each.value
  role_arn = var.role_arn

  command {
    name = "pythonshell"
    python_version = 3
    script_location = "s3://mybucket/${each.value}/run.py"
  }
}