在terraform中,我试图将变量(列表)传递给我们构建的模块。该变量需要在aws_ecs_task_definition
的{{1}}资源中使用。
现在,我只是从一个定义为变量的空默认列表开始:
container_definitions
我的ECS任务定义如下:
variable "task_enviornment" {
type = "list"
default = []
}
我对“环境”部分有疑问的部分:
resource "aws_ecs_task_definition" "ecs_task_definition" {
family = "${var.ecs_family}"
network_mode = "awsvpc"
task_role_arn = "${aws_iam_role.iam_role.arn}"
execution_role_arn = "${data.aws_iam_role.iam_ecs_task_execution_role.arn}"
requires_compatibilities = ["FARGATE"]
cpu = "${var.fargate_cpu}"
memory = "${var.fargate_memory}"
container_definitions =<<DEFINITION
[
{
"cpu": ${var.fargate_cpu},
"image": "${var.app_image}",
"memory": ${var.fargate_memory},
"name": "OURNAME",
"networkMode": "awsvpc",
"environment": "${jsonencode(var.task_enviornment)}",
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group" : "${aws_cloudwatch_log_group.fargate-logs.name}",
"awslogs-region": "us-east-1",
"awslogs-stream-prefix": "demo"
}
},
"portMappings": [
{
"containerPort": ${var.app_port},
"hostPort": ${var.app_port}
}
]
}
]
DEFINITION
}
我尝试了几种不同的方法来使其正常工作。
如果我这样做"environment": "${jsonencode(var.task_enviornment)}",
我得到"environment": "${jsonencode(var.task_enviornment)}",
如果我做ECS Task Definition container_definitions is invalid: Error decoding JSON: json: cannot unmarshal string into Go struct field ContainerDefinition.Environment of type []*ecs.KeyValuePair
或"environment": "${var.task_enviornment}",
我得到"environment": ["${var.task_enviornment}"],
然后,它仅输出At column 1, line 1: output of an HIL expression must be a string, or a single list (argument 8 is TypeList) in:
我也尝试添加默认值,并且收到类似的错误消息。但是我确实需要能够处理没有发送的值,因此是一个空列表。
container_definitions
答案 0 :(得分:0)
经过大量调查和新的眼光,他找到了解决方案。我不确定为什么可以解决此问题,我觉得这可能是一个错误。
需要做2件事来解决此问题。
从变量定义中删除type = "list"
。
variable "task_environment" {
default = []
}
使用变量时请删除引号:
"environment": ${jsonencode(var.task_environment)},
答案 1 :(得分:0)
以下解决方案应该有效
在variable.tf
variable "app_environments_vars" {
type = list(map(string))
default = []
description = "environment varibale needed by the application"
default = [
{
"name" = "BUCKET",
"value" = "test"
},{
"name" = "BUCKET1",
"value" = "test1"
}]
,在您的任务定义中,您可以使用类似于{p>的${jsonencode(var.app_environments_vars)}
container_definitions =<<DEFINITION
[
{
"cpu": ${var.fargate_cpu},
"image": "${var.app_image}",
"memory": ${var.fargate_memory},
"name": "OURNAME",
"networkMode": "awsvpc",
"environment": ${jsonencode(var.envoy_environments_vars)},
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group" : "${aws_cloudwatch_log_group.fargate-logs.name}",
"awslogs-region": "us-east-1",
"awslogs-stream-prefix": "demo"
}
},
"portMappings": [
{
"containerPort": ${var.app_port},
"hostPort": ${var.app_port}
}
]
}
]
答案 2 :(得分:0)
我的猜测是您正在尝试使用类型“映射”而不是列表,如上所示,从类型规范中删除将起作用。 示例: