Amazon web services 创建多个资源的环形地形

Amazon web services 创建多个资源的环形地形,amazon-web-services,for-loop,foreach,terraform,aws-glue,Amazon Web Services,For Loop,Foreach,Terraform,Aws Glue,我在aws 这是一个例子: resource "aws_glue_job" "myjob1" { name = "myjob1" role_arn = var.role_arn command { name = "pythonshell" python_version = 3 script_location = "s3://mybucket/myjob1/run

我在
aws

这是一个例子:

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

在地形中是真实的吗?

如果您的
列表作业
是:

variable "listjobs" {
  default = ["myjob1", "myjob2", "myjob3", "myjob4", "myjob5"]
}
然后,您可以在terraform中使用创建多个
aws\u glue\u job

resource "aws_glue_job" {

  count = length(var.listjobs)  

  name     = var.listjobs[count.index]

  role_arn = var.role_arn

  command {
    name = "pythonshell"
    python_version = 3
    script_location = "s3://mybucket/${var.listjobs[count.index]}/run.py"
  }

}

我通过
for loop
解决了这个问题。 在
variables.tf中

variable "list_of_jobs" {
  default = ["myjob1","myjob2","myjob3"]
}
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"
  }
}
glue.tf中

variable "list_of_jobs" {
  default = ["myjob1","myjob2","myjob3"]
}
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"
  }
}

你的回答很好,谢谢。但是我通过循环解决了这个问题,请看我的帖子。