Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terraform destroys other resources when resources created by Count #18609

Closed
neeleshg opened this issue Aug 6, 2018 · 5 comments
Closed

Terraform destroys other resources when resources created by Count #18609

neeleshg opened this issue Aug 6, 2018 · 5 comments
Labels
bug core v0.11 Issues (primarily bugs) reported against v0.11 releases

Comments

@neeleshg
Copy link

neeleshg commented Aug 6, 2018

Terraform Version

Terraform v0.11.7
+ provider.aws v1.30.0
+ provider.statuscake v0.2.0
+ provider.template v1.0.0

Problem Description

I have created multiple aws instances with terraform with the help of Count.
When I try to destroy 1 single aws instance, it also deletes the resources related with other instances.

In below example, if I try to delete resource aws_instance.ca[1], it also tries to delete aws_volume_attachment.ca_attach_data_disk[0] along with aws_volume_attachment.ca_attach_data_disk[1].
Similarly for NLB Target Group attachment and CloudWatch monitors.

Terraform Configuration Files

aws_instance.tf

resource "aws_instance" "ca" {
  count                       = "${var.count}"
  key_name                    = "${var.key_name}"
  subnet_id                   = "${var.subnet_ids[0]}"
  availability_zone           = "${var.azs[0]}"
  vpc_security_group_ids      = ["${var.ca_sg_id}"]
  ami                         = "${data.aws_ami.amazon_linux.id}"
  instance_type               = "${var.instance_type}"
  iam_instance_profile        = "${var.instance_profile}"
  associate_public_ip_address = false
  user_data                   = "${data.template_file.userdata_ca.rendered}"
  root_block_device {
    volume_type           = "gp2"
    volume_size           = "${var.root_volume_size}"
    delete_on_termination = true
  }
resource "aws_ebs_volume" "ca_data_disk" {
  count             = "${var.count}"
  availability_zone = "${var.azs[0]}"
  size              = "${var.data_disk_size}"
  encrypted         = true
}

# EBS Volume attachment as data disk
resource "aws_volume_attachment" "ca_attach_data_disk" {
  count       = "${var.count}"
  device_name = "/dev/xvdm"
  volume_id   = "${aws_ebs_volume.ca_data_disk.*.id[count.index]}"
  instance_id = "${aws_instance.ca.*.id[count.index]}"
}

Expected Behavior

$ terraform destroy -target=aws_instance.ca[0]
- aws_instance.ca[0]
- aws_volume_attachment.ca_attach_data_disk[0]
- aws_ebs_volume.ca_data_disk[0]

Actual Behavior

terraform destroy -target=aws_instance.ca[0]
- aws_instance.ca[0]
- aws_volume_attachment.ca_attach_data_disk[0]
- aws_ebs_volume.ca_data_disk[0]
- aws_volume_attachment.ca_attach_data_disk[1]

Steps to Reproduce

Run

terraform destroy -target=aws_instance.ca[0]

Additional Context

I did not use element() to refer instance_id and volume_id as I found issue with them also.
Then I changed my code according to #3449

@mildwonkey mildwonkey added the bug label Sep 19, 2018
@mildwonkey
Copy link
Contributor

hi @neeleshg,

I'm sorry for the long silence, this issue slipped past me. Are you still having this issue, by any chance, or were you able to figure it out?

@mildwonkey mildwonkey added the waiting-response An issue/pull request is waiting for a response from the community label Sep 19, 2018
@neeleshg
Copy link
Author

Hi @mildwonkey
Thanks for your response.
Yes this issue is still there.
I guess, this is causing due to Dependencies in tfstate file.

Regards
Neelesh

@hashibot hashibot added core and removed waiting-response An issue/pull request is waiting for a response from the community labels Jul 15, 2019
@hashibot hashibot added the v0.11 Issues (primarily bugs) reported against v0.11 releases label Aug 29, 2019
@michaelkhan3
Copy link

Hi @mildwonkey

What's the status on this issue?

@mildwonkey
Copy link
Contributor

It's not really possible to comment on this issue without a complete reproduction case and the output of trace logs; our documentation for count (including when not to use it, if possible) has been updated quite a bit since this was opened: https://www.terraform.io/docs/configuration/resources.html#when-to-use-for_each-instead-of-count

Several bugs have been fixed since this time as well, so I am going to close this issue, but if anyone is still experiencing a problem while using terraform v0.13, please open a new issue and fill out the issue template entirely.
Thanks!

@ghost
Copy link

ghost commented Oct 13, 2020

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.

@ghost ghost locked as resolved and limited conversation to collaborators Oct 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug core v0.11 Issues (primarily bugs) reported against v0.11 releases
Projects
None yet
Development

No branches or pull requests

4 participants