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

import blocks prevent retrying destroy on error #35151

Open
alext opened this issue May 13, 2024 · 0 comments
Open

import blocks prevent retrying destroy on error #35151

alext opened this issue May 13, 2024 · 0 comments
Assignees

Comments

@alext
Copy link

alext commented May 13, 2024

Terraform Version

Terraform v1.7.5
on linux_amd64

Terraform Configuration Files

Note: this example is using postgres database resources, but the problem is not specific to this provider/resource, but a general problem with imports in the destroy phase.

terraform {
  required_providers {
    postgresql = {
      source  = "cyrilgdn/postgresql"
      version = "~> 1.13"
    }
  }
}

provider "postgresql" {
  host      = "something.12345678.eu-west-2.amazonaws.com"
  username  = "admin"
  password  = "secret"
  superuser = false
}

resource "postgresql_database" "one" {
  name = "test_1"
}

import {
  to = postgresql_database.one
  id = "test_1"
}

resource "postgresql_database" "two" {
  name = "test_2"
}

import {
  to = postgresql_database.two
  id = "test_2"
}

With this configuration in a state where a previous destroy has partially completed, and has destroyed test_1, but has failed to destroy test_2 for any reason (eg network error, permissions error, or using -target), subsequent terraform destroy operations will fail when attempting to import the test_1 database that no longer exists.

Debug Output

https://gist.github.com/alext/e8dfff7527317b672194691dad52ec55

Expected Behavior

terraform destroy should have ignored the test_1 database and successfully deleted the test_2 database.

Actual Behavior

Terraform errored in the plan stage with the following:

$ TF_LOG_PATH=./debug.log TF_LOG=trace terraform destroy
postgresql_database.one: Preparing import... [id=test_1]
postgresql_database.one: Refreshing state... [id=test_1]
postgresql_database.two: Refreshing state... [id=test_2]
╷
│ Error: Cannot import non-existent remote object
│ 
│ While attempting to import an existing object to "postgresql_database.one", the provider detected that no
│ object exists with the given id. Only pre-existing objects can be imported; check that the id is correct
│ and that it is associated with the provider's configured region or endpoint, or use "terraform apply" to
│ create a new remote object for this resource.
╵

Steps to Reproduce

With a postgres database with a test_1 and test_2 database existing in it.

  1. Populate postgres provider details
  2. terraform init
  3. terraform apply
  4. terraform destroy -target postgresql_database.one (Simulate a destroy that's partially completed due to an error using -target)
  5. terraform destroy - This one will error

Additional Context

Details about the use-case:

We have an environment with an RDS instance that's restored from a snapshot (taken from a different environment). On creation we want terraform to import the database/user/schema resources so that it can ensure they're setup for this environment (passwords set correctly etc...). We're running into this issue when destroying the environment, if anything errors in the destroy, it's then in a state where we can't retry the destroy.

References

No response

@alext alext added bug new new issue not yet triaged labels May 13, 2024
@jbardin jbardin added core and removed new new issue not yet triaged labels May 13, 2024
@jbardin jbardin self-assigned this May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants