HiveBrain v1.2.0
Get Started
← Back to all entries
debugterraformMinor

How to forcibly remove the resource created by the broken module?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
theresourcecreatedmodulebrokenremovehowforcibly

Problem

I've created few dns_cname_record resources, however Terraform DNS provider started to throw the errors like:


Error: update server is not set

I've created the bug report for it, as it prevents me from running any Terraform command. The error is basically shown on every Terraform command, including plan.

Now I want to clean up and get rid of this DNS module completely, and all references to it. So I've removed all my dns_cname_record resources from all .tf files. However there are some still resources stored in a state file which I want to get rid of.

$  terraform show | grep -w dns_cname_record
# dns_cname_record.foo-example: 
resource "dns_cname_record" "foo-example" {
# dns_cname_record.bar-example: 
resource "dns_cname_record" "bar-example" {


So I've tried to destroy it with force without refreshing state, but it failed as well:

```
$ TF_LOG=trace terraform destroy -target=dns_cname_record.foo-example -force -refresh=false
...
[TRACE] : eval: *terraform.EvalIf
[TRACE] : eval: *terraform.EvalApplyProvisioners
[TRACE] : eval: *terraform.EvalIf
[TRACE] : eval: *terraform.EvalIf
[TRACE] : eval: *terraform.EvalApply
[DEBUG] dns_cname_record.foo-example: applying the planned Delete change
[TRACE] GRPCProvider: ApplyResourceChange
[DEBUG] dns_cname_record.foo-example: apply errored, but we're indicating that via the Error pointer rather than returning it: update server is not set
[TRACE] : eval: *terraform.EvalWriteState
[TRACE] EvalWriteState: writing current state object for dns_cname_record.foo-example
[TRACE] : eval: *terraform.EvalApplyPost
[ERROR] : eval: *terraform.EvalApplyPost, err: update server is not set
[ERROR] : eval: *terraform.EvalSequence, err: update server is not set
[ERROR] : eval: *terraform.EvalOpFilter, err: update server is not set
[TRACE] [walkDestroy] Exiting eval tree: dns_cname_record.foo-example (destroy)
[TRACE] vertex "dns_cname_record.foo-example (destroy)": visit complete
[TRACE] dag/walk: upstream of "dns_cname_

Solution

The workaround is to pull the current state file, edit it and use it as a base line. For example:

$ terraform state pull > terraform.tfstate
$ vim terraform.tfstate # Carefully remove invalid entries.
$ python -mjson.tool terraform.tfstate # Validate JSON.


Note: In Vim, placing cursor on the opening bracket, hitting d% will remove the whole group.

Then plan and apply:

$ terraform plan -state=terraform.tfstate -refresh=false
$ terraform apply -state=terraform.tfstate -refresh=false


Alternatively, pull, edit and push your local state file into remote state:

$ terraform state pull > terraform.tfstate
$ vim terraform.tfstate # Remove invalid entries and increase the serial value.
$ terraform state push terraform.tfstate


For Azure, it's better to use terraform-provider-azurerm instead of general DNS provider (terraform-provider-dns).

Code Snippets

$ terraform state pull > terraform.tfstate
$ vim terraform.tfstate # Carefully remove invalid entries.
$ python -mjson.tool terraform.tfstate # Validate JSON.
$ terraform plan -state=terraform.tfstate -refresh=false
$ terraform apply -state=terraform.tfstate -refresh=false
$ terraform state pull > terraform.tfstate
$ vim terraform.tfstate # Remove invalid entries and increase the serial value.
$ terraform state push terraform.tfstate

Context

StackExchange DevOps Q#8409, answer score: 6

Revisions (0)

No revisions yet.