2

我有一个创建多个 EC2 实例的 terraform 模板。然后,我在 AWS 控制台中创建了一些 Elastic Network 接口,并将它们作为本地人添加到 terraform 模板中。现在,我想将适当的 ENI 映射到实例,因此我添加了局部变量和变量,如下所示。

locals {
  instance_ami = {
    A  = "ami-11111"
    B = "ami-22222"
    C  = "ami-33333"
    D   = "ami-4444"
  }
}

variable "instance_eni" {
  description = "Pre created Network Interfaces"
  default = [
    {
      name = "A"
      id   = "eni-0a15890a6f567f487"
    },
    {
      name = "B"
      id   = "eni-089a68a526af5775b"
    },
    {
      name = "C"
      id   = "eni-09ec8ad891c8e9d91"
    },
    {
      name = "D"
      id   = "eni-0fd5ca23d3af654a9"
    }
  ]
}


resource "aws_instance" "instance" {
  for_each = local.instance_ami

  ami           = each.value
  instance_type = var.instance_type
  key_name      = var.keypair

  root_block_device {
    delete_on_termination = true
    volume_size           = 80
    volume_type           = "gp2"
  }


  dynamic "network_interface" {
    for_each = [for eni in var.instance_eni : {
      eni_id = eni.id
    }]

    content {
      device_index          = 0
      network_interface_id  = network_interface.value.eni_id
      delete_on_termination = false
    }
  }
}

我收到以下错误:

错误:启动源实例时出错:InvalidParameterValue:每个网络接口都需要唯一的设备索引。状态码:400,请求 ID:4a482753-bddc-4fc3-90f4-2f1c5e2472c7

我认为 terraform 很难将所有 4 个 ENI 仅附加到单个实例。应该怎么做才能将 ENI 附加到单个实例?

4

1 回答 1

2

您在问题中共享的配置是要求 Terraform 管理四个实例,每个实例都有四个与之关联的网络接口。这以两种不同的方式存在问题:

  • 每个实例上的所有网络接口都配置了相同的device_index,这是无效的,并且是此处报告的错误消息。
  • 即使您要解决这个问题,它也会尝试将相同的四个网络接口附加到四个不同的 EC2 实例,这是无效的:每个网络接口一次只能附加到一个实例。

为了解决这个问题并获得您想要的行为,您只需要一个network_interface块,每个实例的内容都不同:

locals {
  instance_ami = {
    A = "ami-11111"
    B = "ami-22222"
    C = "ami-33333"
    D = "ami-4444"
  }
}

variable "instance_eni" {
  description = "Pre created Network Interfaces"
  default = [
    {
      name = "A"
      id   = "eni-0a15890a6f567f487"
    },
    {
      name = "B"
      id   = "eni-089a68a526af5775b"
    },
    {
      name = "C"
      id   = "eni-09ec8ad891c8e9d91"
    },
    {
      name = "D"
      id   = "eni-0fd5ca23d3af654a9"
    }
  ]
}

locals {
  # This expression is transforming the instance_eni
  # value into a more convenient shape: a map from
  # instance key to network interface id. You could
  # also choose to just change directly the
  # definition of variable "instance_eni" to already
  # be such a map, but I did it this way to preserve
  # your module interface as given.
  instance_network_interfaces = {
    for ni in var.instance_eni : ni.name => ni.id
  }
}

resource "aws_instance" "instance" {
  for_each = local.instance_ami

  ami           = each.value
  instance_type = var.instance_type
  key_name      = var.keypair

  root_block_device {
    delete_on_termination = true
    volume_size           = 80
    volume_type           = "gp2"
  }

  network_interface {
    device_index          = 0
    network_interface_id  = local.instance_network_interfaces[each.key]
    delete_on_termination = false
  }
}

现在每个实例只有一个网络接口,每个都附加到输入变量中给定的相应 ENI ID。参考each.key并且each.value是我们如何在使用资源时声明的每个实例之间创建差异for_each;我们不需要任何其他重复结构,除非我们想要创建嵌套重复,例如为每个实例拥有动态数量的网络接口。

于 2020-01-24T18:32:49.417 回答