简体   繁体   English

AWS ECS 错误“Container.name 包含无效字符”

[英]AWS ECS error "Container.name contains invalid characters"

I am setting up an ECS task using Terraform and am encountering an error.我正在使用 Terraform 设置 ECS 任务,但遇到错误。 The error is “Error: failed creating ECS Task Definition (web-2048-task): ClientException: Container.name contains invalid characters.”错误是“错误:创建 ECS 任务定义 (web-2048-task) 失败:ClientException:Container.name 包含无效字符。”

Here is my task code:这是我的任务代码:

resource "aws_ecs_task_definition" "aws-ecs-task" {
  family = "${var.app_name}-task"
  container_definitions = file("templates/task.json")

  requires_compatibilities = ["FARGATE"]
  network_mode             = "awsvpc"
  memory                   = "2048" #2 GB
  cpu                      = "1024" #1 vCPU
  execution_role_arn       = aws_iam_role.ecsTaskExecutionRole.arn
  task_role_arn            = aws_iam_role.ecsTaskExecutionRole.arn

  tags = {
    Name        = "${var.app_name}-ecs-td"
    Environment = var.app_environment
  }
}

Here is the json task definition code:这是 json 任务定义代码:

[{
    "container_name": "${var.app_name}",
    "name": "${var.app_name}",
        "image": "${data.aws_ecr_repository.aws-ecr.repository_url}:latest",
        "essential": true
    },

   {
        "portMappings": [{
            "containerPort": 80,
            "hostPort": 80
        }],
        "cpu": 1024,
        "memory": 2048,
        "networkMode": "awsvpc"
    }
]

I tried replacing the ${var.app_name} in the json to be the name of the app, which is 'web-2048`, but the error changes to “Container.name should not be null or empty.”我尝试将 json 中的 ${var.app_name} 替换为应用程序的名称,即“web-2048”,但错误更改为“Container.name should not be null or empty”。 When I change the app name back to a variable, then I get the above error again.当我将应用程序名称更改回变量时,我再次收到上述错误。

I checked the Terraform Registry for aws_ecs_task_definition but didn't see any info regarding container names.我检查了 Terraform 注册表中的aws_ecs_task_definition ,但没有看到有关容器名称的任何信息。 Same for the Aws developer guide section on task definitions but didn't find anything that helped.与关于任务定义的Aws 开发人员指南部分相同,但没有找到任何帮助。

Can I get some guidance on this?我可以得到一些指导吗?

I managed to reproduce the error.我设法重现了错误。 It is because the variables are not replaced with values in the task.json file as you are using the file built-in function:这是因为变量没有替换为task.json文件中的值,因为您使用的是内置file function:

file reads the contents of a file at the given path and returns them as a string. file读取给定路径的文件内容,并将它们作为字符串返回。

Because of that, the "name": "${var.app_name}" is read as a string literal.因此, "name": "${var.app_name}"被读取为字符串文字。 Since that is the case and as per the documentation [1]:既然是这种情况,并且根据文档 [1]:

Up to 255 letters (uppercase and lowercase), numbers, hyphens, and underscores are allowed最多允许 255 个字母(大写和小写)、数字、连字符和下划线

The .. , $ and {} are not allowed and that is why you are getting the ClientException: Container.name contains invalid characters. , ${}是不允许的,这就是您收到ClientException: Container.name contains invalid characters. error.错误。

In order to fix this, I suggest using the templatefile built-in function [2].为了解决这个问题,我建议使用内置的templatefile文件 function [2]。 It will require some changes to the code:这将需要对代码进行一些更改:

resource "aws_ecs_task_definition" "aws-ecs-task" {
  family = "${var.app_name}-task"
  container_definitions = templatefile("templates/task.json", {
    app_name       = var.app_name
    repository_url = data.aws_ecr_repository.aws-ecr.repository_url
  })

  requires_compatibilities = ["FARGATE"]
  network_mode             = "awsvpc"
  memory                   = "2048" #2 GB
  cpu                      = "1024" #1 vCPU
  execution_role_arn       = aws_iam_role.ecsTaskExecutionRole.arn
  task_role_arn            = aws_iam_role.ecsTaskExecutionRole.arn

  tags = {
    Name        = "${var.app_name}-ecs-td"
    Environment = var.app_environment
  }
}

This will also require slight modifications in the task.json file:这也需要在task.json文件中稍作修改:

[{
      "name": "${app_name}",
      "image": "${repository_url}:latest",
      "essential": true
    },

   {
      "portMappings": [{
         "containerPort": 80,
         "hostPort": 80
      }],
        "cpu": 1024,
        "memory": 2048,
        "networkMode": "awsvpc"
    }
]

What templatefile will do is replace the placeholder variables you are passing to it ( app_name and repository_url ) inside of the JSON file with the values you provide. templatefile所做的是用您提供的值替换 JSON 文件中传递给它的占位符变量( app_namerepository_url )。 Additionally, you might consider renaming the template file into something like task.json.tftpl .此外,您可以考虑将模板文件重命名为类似task.json.tftpl的文件。 The call to the templatefile function in that case would have to be fixed to:在这种情况下,对templatefile文件 function 的调用必须固定为:

  container_definitions = templatefile("templates/task.json.tftpl", {
    app_name       = var.app_name
    repository_url = data.aws_ecr_repository.aws-ecr.repository_url
  })

[1] https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html [1] https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html

[2] https://developer.hashicorp.com/terraform/language/functions/templatefile [2] https://developer.hashicorp.com/terraform/language/functions/templatefile

Have you checked if the substitution to the name of your container image contains only valid character and not having '.'您是否检查过对容器图像名称的替换是否仅包含有效字符而没有“。” in it?在里面? The specific error you are getting appears to be because you have included the '.'您收到的特定错误似乎是因为您包含了“。” character in your name attribute.名称属性中的字符。 From the above docs:从上面的文档:

Up to 255 letters (uppercase and lowercase), numbers, hyphens, and underscores are allowed最多允许 255 个字母(大写和小写)、数字、连字符和下划线

If that doesn't resolve 'Container.name should not be null or empty' issue, it could be some other reasons, some in the past has mistakenly define the key-value pair labeled in the environment variable key, double check if it's "name".如果那不能解决'Container.name should not be null or empty'的问题,可能是其他原因,过去有些人错误地定义了环境变量key中标记的键值对,仔细检查它是否是“姓名”。 { "name": "xxx", "value": "yyy" }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM