bgarcial
bgarcial

Reputation: 3213

Removing last record terraform state in a terraform workspace

I am using terraform cloud workspaces. By mistake, I upload the wrong terraform state to my workspace. Now my terraform plan is using it, but I don’t want to use it since that is not the state I wanted to get.

Let me explain with this image: I want to use the state from 10 months ago and not the New State I got recently:

enter image description here

I want to go back to my old state since in the new one there are no some resources and then they are being recreated in my terraform plan. I am trying to import every separate resource, by executing terraform import <RESOURCE-INSTANCE> <ID> command in this way:

terraform import azurerm_app_service_plan.fem-plan /subscriptions/d37e88d5-e443-4285-98c9-91bf40d514f9/resourceGroups/rhd-spec-prod-rhdhv-1mo5mz5r2o4f6/providers/Microsoft.Web/serverfarms/fem-plan-afd1
Acquiring state lock. This may take a few moments...
azurerm_app_service_plan.fem-plan: Importing from ID "/subscriptions/d37e88d5-e443-4285-98c9-91bf40d514f9/resourceGroups/rhd-spec-prod-rhdhv-1mo5mz5r2o4f6/providers/Microsoft.Web/serverfarms/fem-plan-afd1"...
azurerm_app_service_plan.fem-plan: Import prepared!
  Prepared azurerm_app_service_plan for import
azurerm_app_service_plan.fem-plan: Refreshing state... [id=/subscriptions/d37e88d5-e443-4285-98c9-91bf40d514f9/resourceGroups/rhd-spec-prod-rhdhv-1mo5mz5r2o4f6/providers/Microsoft.Web/serverfarms/fem-plan-afd1]


Error: Cannot import non-existent remote object

While attempting to import an existing object to
azurerm_app_service_plan.fem-plan, 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.

Releasing state lock. This may take a few moments...

But I got in the output, that resource does not exist, because terraform is using my latest New state where that resource is not included.

How can I use my old 10 months ago state?

If someone can point me out in the right path I will appreciate it.

Upvotes: 1

Views: 853

Answers (1)

Charles Xu
Charles Xu

Reputation: 31452

Maybe you can use the command terraform refresh to update the state file to match the current state of the resources. You can get the details of the command here.

Upvotes: 0

Related Questions