Recover Terraform Linode State

I've deleted my Terraform state file and I'm trying to recover it.

(Actually, I'd rather never have a state file--it means there's two sources ground truth, when there's really only the one.)

I've done a terraform import to get back the state of a Linode orignally created by Terraform. But when I invoke terraform plan, it says it has to replace the Linode, because:
+ image = "linode/debian9" # forces replacement

and this happens because:

This value can not be imported. Changing image forces the creation of a new Linode Instance.

https://registry.terraform.io/providers/linode/linode/latest/docs/resources/instance

(There's also the matter of root password--I expected the same issue but it's not griping about that. Problem for another magic moment.)

So now what? How do I get Terraform (or the Linode provider) to accept that everything's OK? Is there some way I can simultaneously provide and not provide the image name? Because I surely do need it when standing up a new instance!

1 Reply

It looks like the issue is that certain attributes of a Linode instance, like the image, cannot be imported into the Terraform state. So even though you imported the existing Linode instance, Terraform still wants to recreate it because the configuration specifies an image.

The Terraform documentation below explains that changing the image forces the creation of a new Linode instance. So essentially, Terraform treats the image as an immutable attribute -- if it doesn't match what is already deployed, Terraform will rebuild the instance.

To avoid this, you need to make sure the image in your Terraform configuration matches what is already deployed. You can find the image ID by looking in the Linode Dashboard.

As for saving the Terraform state, you can use a remote backend like Linode Object Storage is a good option. Here are a few key points:

terraform {
  backend "s3" {
    bucket = "<YOUR BUCKET>" 
    key    = "path/to/my/state"
    region = "<REGION>" 

    skip_credentials_validation = true
    access_key = "<ACCESS KEY>"
    secret_key = "<SECRET KEY>"
  }
}

This allows you to save the state file remotely instead of locally. Some benefits:

  • State is stored securely in the cloud and can be shared access for a team
  • Prevents loss of state file

I'll also recommend checking out the blog post for a more in-depth walkthrough of configuring a remote state backend:

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct