title: Terraform: Correctly assigning a static private IP to newly created instance
body: <p>Objective:</p>
  
  <p>I'm trying to create a number of EC2 instances and assign them each a static private ip address from a map variable. This address must be the primary address, and basically means I'm not using the DHCP assigned address provided by aws.</p>
  
  <p>Issue:</p>
  
  <p>The terraform plan succeeds, and I can create instances which show the static IP address assigned, <em>together</em> with the DHCP assigned address (in the aws console). When I ssh into the instance I see the primary address is the DHCP assigned address. there is a second ENI attached to the instance (eth1) but the static ip address is not there.</p>
  
  <p>The Question:</p>
  
  <p>How to have terraform create the instance, using the statically assigned IP address for the primary interface (eth0), instead of the default assigned DHCP address? Basically, how can I either:</p>
  
  <p>a) Create this interface with statically assigned interface at instance creation time or,
  b) Replace the existing primary interface IP address with a static one (or the entire ENI itself with a newly created one with the static IP address)</p>
  
  <p>Here are the details:</p>
  
  <p>I am using the module as follows in a separate main.tf file:</p>
  
  <p>a) create the instance</p>
  
  <pre><code>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"]}"
  }
  </code></pre>
  
  <p>b) I'm Assigning private IPs to the instance using a resource (outside the module bloc in main.tf):</p>
  
  <pre><code>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
      }
  }
  </code></pre>
  
  <p>NOTE: I have tried changing device_index to 0, but as expected, aws complains that there is already an eni attached at index 0:</p>
  
  <pre><code>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'.
  </code></pre>
  
  <h2>    status code: 400, request id: cbd9e36d-145f-45d4-934f-0a9c2f6e7768</h2>
  
  <p>Some additional information that may be useful: Link to my full module definition files:</p>
  
  <p>main definition file for the ec2 module:</p>
  
  <p><a href="http://pastebin.com/zXrZRrQ5" rel="noreferrer">http://pastebin.com/zXrZRrQ5</a></p>
  
  <p>main outputs.tf definition file for ec2 module:</p>
  
  <p><a href="http://pastebin.com/9t5zjfQS" rel="noreferrer">http://pastebin.com/9t5zjfQS</a></p>