将字符串列表传递到模块中,然后创建数组Terraform



我目前正在创建一个Terraform模块,用于创建一些ECS任务。我希望能够传递一个命令列表,这样我就可以为某个任务指定命令字段。

我目前将我的列表传递到我的模块中,如下所示:

module "test" {
source = "git@github.com:test/deploy.git"
task_count = 3
entryPoints = [
"run, -c, /app/node/node_0, --bootnode, true",
"run, -c, /app/node/node_1",
"run, -c, /app/node/node_2",
]
}

然后我想把每个命令都传递到它的任务中:

data "template_file" "test" {
template = file("${path.module}/templates/test.json.tpl")
count = var.task_count
vars = {
entryPoint = split(",", element(var.entryPoints, count.index)),
}
}

我希望element(var.entryPoints, count.index)意味着它将在中通过的第一项任务

"run", "-c", "/app/node/node_0", "--bootnode", "true"

然后对于第二个任务

"run", "-c", "/app/node/node_1"

etc

然而,我收到了错误:

Error: Incorrect attribute value type
on .terraform/modules/test/ecs_task_definition.tf line 5, in data "template_file" "test":
5:   vars = {
12:   entryPoint = split(",", element(var.entryPoints, count.index)),
15:   }
Inappropriate value for attribute "vars": element "entryPoint": string
required.

maint.tf

variable "entrypoints_list" {
default = [
"run, -c, /app/node/node_0, --bootnode, true",
"run, -c, /app/node/node_1",
"run, -c, /app/node/node_2",
]
}
locals {
entryPoints = [for key in var.entrypoints_list : split(",", key)]
}
data "template_file" "container_definition" {
count = length(local.entryPoints)
template = file("${path.module}/templates/container-definition.json.tpl")
vars = {
entryPoint             =  jsonencode(element(local.entryPoints,count.index))
}
}
output "container-definition" {
value = data.template_file.container_definition[*].rendered
}

container-definition.json.tpl


{
"entryPoint": ${entryPoint}
}

terraform plan

+ container-definition = [
+ jsonencode(
{
+ entryPoint = [
+ "run",
+ " -c",
+ " /app/node/node_0",
+ " --bootnode",
+ " true",
]
}
),
+ jsonencode(
{
+ entryPoint = [
+ "run",
+ " -c",
+ " /app/node/node_1",
]
}
),
+ jsonencode(
{
+ entryPoint = [
+ "run",
+ " -c",
+ " /app/node/node_2",
]
}
),
]

根据您的需求,您可以在模块中移动代码。

相关内容

最新更新