Terraform Refresh only not working

I have overwrite the zone in the terraform.tfvars, but while running the terraform plan -refresh-only , why it is not showing that I have changed the zone and it will recreate the instance, instead of that it is giving below output, that doesn’t make any sense. What i am missing here?

terraform plan -refresh-only
google_compute_instance.server: Refreshing state... [id=projects/eng-bloom-346707/zones/us-central1-a/instances/refreshonly]

Note: Objects have changed outside of Terraform

Terraform detected the following changes made outside of Terraform since the last "terraform apply" which may have affected this plan:

  # google_compute_instance.server has changed
  ~ resource "google_compute_instance" "server" {
        id                   = "projects/test/zones/us-central1-a/instances/refreshonly"
      + labels               = {}
        name                 = "refreshonly"
      + resource_policies    = []
      + tags                 = []

Hi @rohan.madangpv,

A “refesh-only” plan only pulls the latest data from the provider, to update the state to match the existing resources. It does not take into account any configuration changes. If you want to see the change which will be made based on configuration, then you want to run a normal plan.

Hello @jbardin ,
Thanks for your reply.

Actually I am following a tutorial in which they have changed the provider region, and after that they run refresh-only, it reflect that It is not able to find the instance, so It will delete it.
Same I have tried with GCP but it didn’t work.

It seems the GCP provider is still able to locate the instance even given a different default region. The reason the AWS example instance would be deleted when you refresh with different provider settings, is that the provider cannot locate any instance with the given ID, so reports that it is gone.

1 Like

Indeed, there is a key difference between the design of the GCP provider and the design of the AWS provider:

The AWS provider treats the region in the provider configuration essentially just as a shorthand for specifying the various service-specific endpoint URLs, avoiding the need to specify each service endpoint separately. Therefore if you change the region in the provider configuration, you just tell the provider to use different base URLs when making requests. If the newly-chosen endpoints don’t know about the objects previously recorded in the state then the API will return a “not found” error and the provider will believe that the object was deleted outside of Terraform.

The GCP provider instead treats the provider-level region setting only as a default region to use for any resource that doesn’t explicitly choose one; in the GCP provider you can, if you wish, specify a region directly inside each resource block and thus ignore the provider-level region altogether. A consequence of this is that the GCP provider tracks the selected region for each existing object as part of the state for that object, and so changing the default region in the provider configuration only affects which region might be used for any new objects being created, and not any objects that already exist and therefore already belong to a region.

1 Like

Thanks for a great explanation!!