title: Incorrect Private IP EC2 Instance, Terraform
body: <p>Deploying an number of instances in AWS using terraform. The code is written to make use of specific private ip ranges. The code iterates through a range to provide the last two digits on an ip.</p>
  <pre><code>IP_AA_MGMT_Windows = [for i in range(1, var.Number_of_AA_Candidates +1 ) : format(&quot;%s%02d&quot;, &quot;10.10.8.1&quot;, i)]
  </code></pre>
  <p>For information the subnet this belongs to has the following CIDR allocation</p>
  <pre><code>cidr_block = &quot;10.10.8.0/22&quot;
  </code></pre>
  <p>This gives an ip range of 10.10.8.0 - 10.10.11.255</p>
  <p>The instance is created with no real problems. Expected private ip is allocated in an identical manner as the network interface.</p>
  <pre><code>resource &quot;aws_instance&quot; &quot;Windows&quot; {
      instance_type               = &quot;t2.large&quot;
      subnet_id                   = aws_subnet.windows.id
      vpc_security_group_ids = [aws_security_group.AA_Eng_Windows[count.index].id]
      key_name                    = aws_key_pair.ENG-DEV.id
      count                       = var.Number_of_AA_Candidates
      private_ip                  = local.IP_AA_WINLAN_Windows[count.index]
      associate_public_ip_address = false
  </code></pre>
  <p>An additional network interface is created and attached to the instance.</p>
  <pre><code>resource &quot;aws_network_interface&quot; &quot;Windows_Access_Interface&quot; {  
      subnet_id                   = aws_subnet.management.id
      private_ip                  = local.IP_AA_MGMT_Windows[count.index]
      security_groups             = [aws_security_group.Windows.id]
      count                       = var.Number_of_AA_Candidates
      attachment {
        instance                  = aws_instance.Windows[count.index].id
        device_index              = 1
      }
  </code></pre>
  <p>All deploys correctly according to terraform. Its not until you check the private ip's in AWS or through terraform state show that you realise the network interface resource is created but with an incorrect private ip, not the one provisioned in the code. NOTE. Terraform plan provides output suggesting no problems with ip allocation.</p>
  <p>Below is some of the output from the terrafrom show command.</p>
  <pre><code># aws_network_interface.Windows_Access_Interface[0]:
      resource &quot;aws_network_interface&quot; &quot;Windows_Access_Interface&quot; {
      interface_type            = &quot;interface&quot;
      private_ip                = &quot;10.10.10.72&quot;
      private_ip_list           = [
          &quot;10.10.10.72&quot;,
      ]
      private_ip_list_enabled   = false
      private_ips               = [
          &quot;10.10.10.72&quot;,
      ]
  </code></pre>
  <p>NOTE Some of the details in the show have intentionally been removed for security.</p>
  <p>The question now is, what is causing this?</p>