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"
}
}
它正在工作,但如果我有类似于listmyjob1、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"
}
}
你的回答很好,谢谢。但是我通过循环解决了这个问题,请看我的帖子。