hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.83k stars 9.18k forks source link

Replacing an aws_key_pair does not update related aws_instance(s) #10497

Open thrixton opened 5 years ago

thrixton commented 5 years ago

Community Note

Terraform Version

Terraform v0.12.10 provider.aws v2.30.0

Affected Resource(s)

aws_key_pair aws_instance

Terraform Configuration Files

Key

resource "aws_key_pair" "ec2_key_pair" {
  key_name   = "${var.project}-${var.environment}-ec2-key"
  public_key = "${var.ec2_key_public_data}"
}

Instance

resource "aws_instance" "ec2_test" {
  ami                         = "${var.test_ec2_ami}"
  instance_type               = "t2.micro"
  vpc_security_group_ids      = ["${var.security_group_id}"]
  subnet_id                   = "${var.subnet_id}"
  associate_public_ip_address = false
  key_name                    = "${aws_key_pair.ec2_key_pair.key_name}"

  tags        = merge(local.project_tags, local.test_resource_tags, { Name = "${upper(var.project)}-${upper(var.environment)} Test", type = "ec2-instance" })
  volume_tags = merge(local.project_tags, local.test_resource_tags, { type = "storage-ec2" })
}

Expected Behavior

When a key is replaced (such as when imported per #1092), the associated instances are not also updated. This is due to Terraform's diffing behaviour, as per Update/replace resource when a dependency is changed, #8099. This issue is just as an example for the issue above.

Actual Behavior

Terraform does not recreate the instance(s).

lorengordon commented 4 years ago

Where the linked issue #8099 is about wanting an update to trigger depends_on, this appears to be about a resource cycle that destroys a resource, and that destroy does not cause depends_on to resource cycle the dependent resource. Since depends_on is supposed to trigger these ordering operations, this one seems more like a bug.

justinretzolk commented 2 years ago

Hey @thrixton 👋 Thank you for taking the time to file this issue! Given that there's been a number of AWS provider releases since you initially filed it, can you confirm whether you're still experiencing this behavior?

oxcug commented 2 years ago

@justinretzolk can confirm. I just ran into this (found the ticket via search).