awsvpc:网络配置对此任务定义的给定网络模式无效

awsvpc: Network Configuration is not valid for the given networkMode of this task definition

我的任务定义:

resource "aws_ecs_task_definition" "datadog" {
  family        = "${var.environment}-datadog-agent-task"
  task_role_arn = "arn:aws:iam::xxxxxxxx:role/datadog-role"

  container_definitions = <<EOF
[
  {
    "name": "${var.environment}-${var.datadog-identifier}",
    "network_mode" : "awsvpc",
    "image": "datadog/agent:latest",
    "portMappings": [
      {
...

我的服务定义:

resource "aws_ecs_service" "datadog" {
  name            = "${var.environment}-${var.datadog-identifier}-datadog-ecs-service"
  cluster         = "${var.cluster}"
  task_definition = "${aws_ecs_task_definition.datadog.arn}"

  network_configuration {
    subnets = flatten(["${var.private_subnet_ids}"])
  }

  # This allows running one for every instance
  scheduling_strategy = "DAEMON"
}

我收到以下错误 -

InvalidParameterException: Network Configuration is not valid for the given networkMode of this task definition

这里有我遗漏的东西吗?查看 Terraform 文档和 GitHub 问题,这应该有效。它与作为守护进程的 运行 Datadog 有关吗?

如果要定义使用该任务定义的服务的 network_configuration,则需要将 aws_ecs_task_definition's network_mode 设置为 awsvpc

这个在documentation for the network_configuration parameter of the aws_ecs_service resource中提到:

network_configuration - (Optional) The network configuration for the service. This parameter is required for task definitions that use the awsvpc network mode to receive their own Elastic Network Interface, and it is not supported for other network modes.

在您的情况下,您已将 network_mode 参数添加到 container 定义而不是 task 定义(a task 是 n 个容器的集合,它们被组合在一起共享一些资源)。 container definition schema 不允许 network_mode 参数。