【问题标题】:Attach network interface device to ec2 instance AWS via Terraform通过 Terraform 将网络接口设备连接到 ec2 实例 AWS
【发布时间】:2018-01-03 19:40:16
【问题描述】:

我正在尝试在 Terraform 中附加网络接口设备。我已经尝试使用下面的代码进行测试,但是在创建实例时,AWS 在附加任何内容之前已经创建了一个 eni。

所以当你附加一些东西时,你最终会得到第二个 eni。我只需要一个主私有 IP,它会产生 2 个私有 IP。

resource "aws_network_interface" "test" {
  subnet_id = "${aws_subnet.public_a.id}"
  private_ips = ["10.0.0.50"]
  security_groups = ["${aws_security_group.web.id}"]
  attachment {
    instance = "${aws_instance.test.id}"
    device_index = 1
  }
}

【问题讨论】:

标签: amazon-web-services terraform


【解决方案1】:

这样做的方法是在创建 EC2 实例时在 terraform 中使用 network_interface 参数。您的 terraform 模板应如下所示:

provider "aws" {
    region = "us-east-2"
}

resource "aws_network_interface" "eni1" {
    subnet_id        = "subnet-1234567"
    private_ips      = ["172.31.16.17"]
}

resource "aws_instance" "testec2instance" {
    ami             = "ami-12345678"
    instance_type   = "t2.micro"
    tags {
        Name        = "test-eni-attach-instance-01"
    }
    network_interface {
        device_index            = 0
        network_interface_id    = "${aws_network_interface.eni1.id}"
    }
}

如果您只想为您的 EC2 实例指定确切的 IP 地址,您可以在创建实例时使用 terraform 中的 private_ip 参数。

【讨论】:

    猜你喜欢
    • 2023-01-29
    • 1970-01-01
    • 2019-01-20
    • 2019-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-10
    相关资源
    最近更新 更多