Terraform:将静态私有IP正确分配给新创建的实例

Tra*_*ome 5 amazon-ec2 amazon-web-services terraform

目的:

我正在尝试创建许多EC2实例,并为它们分配一个来自映射变量的静态专用ip地址。该地址必须是主地址,基本上意味着我没有使用aws提供的DHCP分配的地址。

问题:

terraform计划成功,并且我可以创建实例,这些实例显示分配的静态IP地址以及 DHCP分配的地址(在aws控制台中)。SSH进入实例时,我看到主地址是DHCP分配的地址。有附加到实例(ETH1)第二ENI但静态IP地址是不存在。

问题:

如何让terraform使用主要接口的静态分配的IP地址(eth0)而不是默认分配的DHCP地址来创建实例?基本上,我怎么能之一:

a)在实例创建时使用静态分配的接口创建此接口,或b)用静态的IP地址替换现有的主接口IP地址(或用具有静态IP地址的新创建的IP地址替换整个ENI本身)

详细信息如下:

我在单独的main.tf文件中使用以下模块:

一)创建实例

module "ec2-hadoop-manager" {
  source                      = "../modules/ec2"
  ami_id                      = "${var.dw_manager["ami"]}"
  instance_type               = "${var.dw_manager["instance_type"]}"
  aws_region                  = "${var.region}"
  availability_zone           = "eu-west-1a"
  associate_public_ip_address = true
  role                        = "hadoop-manager"
  env                         = "${var.environment}"
  vpc                         = "${var.vpc_id}"
  security_group_ids          = "${var.aws_security_group_id["sg_id"]}"
  key_name                    = "${var.key_name}"
  subnet_id           = "${var.default_subnet}"
  number_of_instances = "${var.dw_manager["count"]}"
}
Run Code Online (Sandbox Code Playgroud)

B)我使用的资源(外main.tf模块集团)分配私有地址的实例:

resource "aws_network_interface" "private_ip" {
    count = "${var.dw_manager["count"]}"
    subnet_id = "${var.default_subnet}"
    private_ips = ["${lookup(var.dw_manager_ips, count.index)}"]
    security_groups = "${var.aws_security_group_id["sg_id"]}"
    attachment {
        instance = "${element(split(",", module.ec2-hadoop-manager.ec2_instance_ip), count.index)}"
        device_index = 1
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:我尝试将device_index更改为0,但是正如预期的那样,aws抱怨索引0处已经附加了eni:

Error applying plan:

3 error(s) occurred:

* aws_network_interface.private_ip.0: Error attaching ENI: InvalidParameterValue: Instance 'i-09f1371f798c2f6b3' already has an interface attached at device index '0'.
        status code: 400, request id: ed1737d9-5342-491a-85a5-e49e70b7503d
* aws_network_interface.private_ip.2: Error attaching ENI: InvalidParameterValue: Instance 'i-012bda6948bbe00c9' already has an interface attached at device index '0'.
        status code: 400, request id: 794c04fb-9089-4ad0-8f5d-ba572777575a
* aws_network_interface.private_ip.1: Error attaching ENI: InvalidParameterValue: Instance 'i-00ac215801de3aba8' already has an interface attached at device index '0'.
Run Code Online (Sandbox Code Playgroud)

状态码:400,请求ID:cbd9e36d-145f-45d4-934f-0a9c2f6e7768

一些其他有用的信息:链接到我的完整模块定义文件:

对于EC2模块主要定义文件:

http://pastebin.com/zXrZRrQ5

ec2模块的主要outputs.tf定义文件:

http://pastebin.com/9t5zjfQS

cat*_*sby 5

Terraform v0.9.4 附带了一项新功能aws_instance,让您可以通过新的配置选项分配给设备索引 0 network_interface

还有aws_network_interface_attachment,但我相信你想要上面的新network_interface选项aws_instance

示例配置:

resource "aws_network_interface" "foo" {
  subnet_id = "${aws_subnet.my_subnet.id}"
  private_ips = ["172.16.10.100"]
  tags {
    Name = "primary_network_interface"
  }
}

resource "aws_instance" "foo" {
    ami = "ami-22b9a343" // us-west-2
    instance_type = "t2.micro"
    network_interface {
     network_interface_id = "${aws_network_interface.foo.id}"
     device_index = 0
  }
}
Run Code Online (Sandbox Code Playgroud)