terraform-aws-modules / terraform-aws-ec2-instance

Terraform module to create AWS EC2 instance(s) resources 🇺🇦
https://registry.terraform.io/modules/terraform-aws-modules/ec2-instance/aws
Apache License 2.0
754 stars 1.87k forks source link

terraform import of EC2 with static private IP network interface imported as empty array #326

Closed battmush closed 1 year ago

battmush commented 1 year ago

Description

In attempting to import the configuration of an EC2 instance which was provisioned via the AWS EC2 Console, we're running into an issue. The EC2 is created with a static-value private IP address, which creates an ENI under the covers, and sets the 'Delete on termination' param to 'true'. On importing, we first import the network interface, which checking the terraform.tfstate file is correct. Then, we import the EC2 instance, but we end up with an empty array for the value of network_interface. The result of this is that any future plan for the EC2 target would insist that the instance must be recreated because of perceived differences in the network interface.

We were able to resolve this by manually editing the terraform.tfstate file and modifying the network_interface, adding the following

             "network_interface": [
              {
                "delete_on_termination": true,
                "device_index": 0,
                "network_card_index": 0,
                "network_interface_id": "eni-035ceaa1ae8573477"
              }
            ],

Then, modifying the aws_instance config from

  network_interface {
    network_interface_id = aws_network_interface.eni-primary-qa-foo01.id
    device_index = 0
  }

to

  network_interface {
    network_interface_id = aws_network_interface.eni-primary-qa-foo01.id
    device_index = 0
    delete_on_termination = true
  }

⚠️ Note

Before you submit an issue, please perform the following first:

  1. Remove the local .terraform directory (! ONLY if state is stored remotely, which hopefully you are following that best practice!): rm -rf .terraform/
  2. Re-initialize the project root to pull down modules: terraform init
  3. Re-attempt your terraform plan or apply and check if the issue still persists

Versions

Reproduction Code [Required]

resource "aws_instance" "amwin-qa-foo01" {
  ami = "ami-0e38fa17744b2f6a5"
  instance_type = "t3.large"
  iam_instance_profile = var.ec2_iam_role_default
  key_name = var.ec2_keypair_name

  network_interface {
    network_interface_id = aws_network_interface.eni-primary-qa-foo01.id
  }

  root_block_device {
    volume_size = "50"
    volume_type = "gp3"
  }
}

resource "aws_network_interface" "eni-primary-qa-foo01" {
  subnet_id = data.aws_subnet.qa-foo-ec2-private-1b.id
  private_ips = ["10.123.25.200"]
}

Steps to reproduce the behavior:

  1. Create EC2 instance using AWS EC2 Console, explicitly setting a static IP address and using defaults to get 'Delete on termination' set to 'true'
  2. Perform terraform import of the created ENI
  3. Perform terraform import of the create EC2
  4. Perform terraform plan -target with the EC2 resource

Expected behavior

Expected network_interface element to be properly imported Excected the EC2 instance to not require recreation

Actual behavior

network_interface element is an empty array EC2 plan always requires recreation without manual editing of .tfstate

github-actions[bot] commented 1 year ago

This issue has been automatically marked as stale because it has been open 30 days with no activity. Remove stale label or comment or this issue will be closed in 10 days

github-actions[bot] commented 1 year ago

This issue was automatically closed because of stale in 10 days

github-actions[bot] commented 1 year ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.