0
votes

I am using terraform oci provider. While I was trying to create compute instances using k8 nodepools I encountered failure , so I deleted that tfstate file and ran the script again which resulted in creation of few duplicate resources like vcn , LB . Due to few constraints I want to retain the old resources (the ones those were created during failed execution) and delete the new ones . Both are created with same name and new tfstate file is having the details of new (duplicate) resources. How can I delete the new duplicate resources and make tfstate file consistent with required resources?

1
It's unfair to say that terraform created duplicate resources as you admitted that you deleted the state file. There is no quick win here AFAIK. Use remote backends, which will save you in the future from this tragedy. I'm not sure of Oracle Cloud but each resource in the cloud must have some unique id. Names, tags could be duplicated. If the resources are limited, I would delete them via CLI/portal. Have you tried terraform destroy with a new state file, it must delete only newly created resources it created irrespective of the duplication? - harshavmb

1 Answers

2
votes

This is a good use case for Terraform Import.

As long as the resource supports it you can import the existing resource terraform import <resource.path> after running terraform delete -target <resource to delete>