Error: "network_interface": conflicts with vpc_security_group_ids

Error: "network_interface": conflicts with vpc_security_group_ids

我正在尝试创建一个带有 aws_network_interface 的 aws 实例,如下所示:

resource "aws_network_interface" "lustre-mds01" {
  subnet_id   = "${var.subnet_id}"
  private_ips = ["10.1.0.10"] 
}

resource "aws_instance" "lustre-mds01" {
  ami                    = "${var.ec2_ami}"
  instance_type          = "t2.nano"
  key_name               = "${var.key_name}"
  vpc_security_group_ids = [ "${var.vpc_security_group_id}" ]

  root_block_device {
    volume_type = "gp2"
    volume_size = 128
  }

  network_interface {
    network_interface_id = "${aws_network_interface.lustre-mds01.id}"
    device_index         = 0
  }
}

但是,这会导致:

Error: "network_interface": conflicts with vpc_security_group_ids

看来这有问题,但由于不活动,工单已关闭。我是一个 terraform 菜鸟,所以我不确定这看起来像是一个错误还是只是用户错误。

我的环境:

$ terraform -v
Terraform v0.12.2
+ provider.aws v2.15.0
+ provider.external v1.1.2
+ provider.local v1.2.2
+ provider.null v2.1.2

aws_network_interface resource 允许您为接口设置安全组(安全组由 ENI 限定范围,因此这是有道理的)所以如果您定义 network_interface 块,那么您将覆盖默认 ENI,因此无法在实例级别指定安全组。

所以在你的情况下你可能想要这样的东西:

resource "aws_network_interface" "lustre-mds01" {
  subnet_id       = "${var.subnet_id}"
  private_ips     = ["10.1.0.10"]
  security_groups = ["${var.vpc_security_group_id}"] 
}

resource "aws_instance" "lustre-mds01" {
  ami           = "${var.ec2_ami}"
  instance_type = "t2.nano"
  key_name      = "${var.key_name}"

  root_block_device {
    volume_type = "gp2"
    volume_size = 128
  }

  network_interface {
    network_interface_id = "${aws_network_interface.lustre-mds01.id}"
    device_index         = 0
  }
}

但是,我会质疑 为什么 你要在这里替换默认的 ENI,因为直接在 aws_instance resource 中设置实例的私有 IP 地址要简单得多相反:

resource "aws_instance" "lustre-mds01" {
  ami                    = "${var.ec2_ami}"
  instance_type          = "t2.nano"
  key_name               = "${var.key_name}"
  subnet_id              = "${var.subnet_id}"
  private_ip             = "10.1.0.10"
  vpc_security_group_ids = ["${var.vpc_security_group_id}"]

  root_block_device {
    volume_type = "gp2"
    volume_size = 128
  }
}

您还可能受益于使用数据源 select 您的 security group and AMI 而不是为这些传递不透明的 ID。这使他们能够更加自我记录。