Terraform: Don't refresh datasources during a destroy.

Created on 23 Jun 2017  ยท  8Comments  ยท  Source: hashicorp/terraform

If I reference an external resource in a datasource and later on do a terraform destroy and the datasource lookup fails, then my destroy will fail. If I instruct Terraform to not refresh via -refresh=false, my destroy will complete successfully.

Terraform Version

Terraform v0.9.8

Terraform Configuration Files

data "aws_subnet" "instance" {
  id = "${var.instance_subnet_id}"
}

Expected Behavior

I should be able to destroy successfully.

Actual Behavior

If the datasource returns a 404, my destroy fails.

Steps to Reproduce

Please list the steps required to reproduce the issue, for example:

  1. Create a subnet externally.
  2. terraform apply
  3. Delete the subnet externally.
  4. terraform destroy. This will fail.
bug core v0.11 v0.9

Most helpful comment

Same thing is happening for ebs snapshot data source, if snapshot is deleted manually before terraform destroy. We are using terraform version 0.12.13

All 8 comments

This still occurs in terraform 0.11.7

Also happens with terraform 0.12

Same thing is happening for ebs snapshot data source, if snapshot is deleted manually before terraform destroy. We are using terraform version 0.12.13

Similar issue with RDS aws_db_snapshot datasource. https://www.terraform.io/docs/providers/aws/d/db_snapshot.html

Code

data "aws_db_snapshot" "latest_prod_snapshot" {
  db_instance_identifier = "${aws_db_instance.prod.id}"
  most_recent            = true
}

# Use the latest production snapshot to create a dev instance.
resource "aws_db_instance" "dev" {
  instance_class      = "db.t3.micro"
  name                = "mydbdev"
  snapshot_identifier = "${data.aws_db_snapshot.latest_prod_snapshot.id}"

  lifecycle {
    ignore_changes = ["snapshot_identifier"]
  }
}

Error

Error: Error refreshing state: 1 error occurred:
 * data.aws_db_snapshot.latest_prod_snapshot: 1 error occurred:
 * data.aws_db_snapshot.latest_prod_snapshot: data.aws_db_snapshot.latest_prod_snapshot: DBSnapshotNotFound: DBSnapshot mydbdev-2019-12-31-08-13 not found.
 status code: 404, request id: 9e3af4f3-5b8f-4dd2-87e6-07c75f40c63d

What's the solution?

terraform destroy -refresh=false will stop the refresh and destroy. See here: https://github.com/ComputeCanada/magic_castle/issues/40#issuecomment-584137330

The separate refresh walk has been removed, and resources are only refreshed as need during plan, so this should no longer be an issue in the upcoming 0.14 release.

Thanks!

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.

Was this page helpful?
0 / 5 - 0 ratings