使用terraform在container_definitions中为ECS任务使用列表变量



在terraform中,我试图将一个变量(列表(传递给我们构建的模块。此变量需要在container_definitions中的aws_ecs_task_definition资源中使用
现在我只是从一个定义为变量的空默认列表开始:

variable "task_enviornment" {
type        = "list"
default = []
}

我的ECS任务定义如下:

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的内容

我也尝试添加默认值,但收到了类似的错误消息。然而,我确实需要能够处理没有发送的值,所以是一个空列表。

variable "task_enviornment" {
type        = "list"
default = [
{
"name" = "BUCKET",
"value" = "test"
}
]
}

经过大量调查和全新的眼光,找到了解决方案。我不确定为什么要修复它,我觉得这可能是一个错误
需要做两件事来解决这个问题。

  1. 从变量定义中删除type = "list"

    variable "task_environment" { default = [] }

  2. 使用变量时删除引号:
    "environment": ${jsonencode(var.task_environment)},

以下解决方案应能在中工作

变量.tf

variable "app_environments_vars" {
type        = list(map(string))
default     = []
description = "environment variable needed by the application"
default = [
{
"name" = "BUCKET",
"value" = "test"
},{
"name" = "BUCKET1",
"value" = "test1"
}]

在您的任务定义中,您可以使用类似于的${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.app_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}
}
]
}
]

我的猜测是,您正试图使用类型"地图";如上所示,从类型规范中删除将起作用,而不是列表。示例:

  • Listrongample=[1,2,3]
  • Map_sample={key_name="值"}

参考:Terraform-类型约束

相关内容

  • 没有找到相关文章

最新更新