How to reinitiated local state when the infra is not changed?

Hey forks, thanks for the help in advance!

I previous used terraform to provisioned some infra. For some reason, I’ve lost the local files, so I git cloned the repo with .tf files and .local.hcl, and tried to re-initiate the terraform project again.

Here is my local main.tf file look like

provider "google" {
  project = "relier-prod"
  region  = "us-west1"
  zone    = "us-west1-b"
}

resource "google_container_cluster" "relier_prod_cluster" {
  name     = "relier-prod-cluster"
  location = "us-west1"

  enable_autopilot = true

  release_channel {
    channel = "REGULAR"
  }

  # Set `deletion_protection` to `true` will ensure that one cannot
  # accidentally delete this instance by use of Terraform.
  deletion_protection = true
}

resource "google_artifact_registry_repository" "relier_image_repository" {
  repository_id = "relier-image-repository"
  location      = "us-west1"
  format        = "DOCKER"
}

After cloned the repo, I run terraform init, then terraform plan, which shows that it will plan to create 2 resources, resource "google_artifact_registry_repository" "relier_image_repository" and resource "google_container_cluster" "relier_prod_cluster".

Then if I run terraform plan -refresh-only, it says: “No changes. Your infrastructure still matches the configuration.”

I then run terraform apply -refresh-only, it says: “No changes. Your infrastructure still matches the configuration.”.

However, if I now run terraform plan (without -refresh-only), it will still try to create these resources. And if I run terraform apply (without -refresh-only), it will error because the resource already existed.

How can bring these remote infra back to my locally state, so I can control my infra again with terraform?

Thanks!

Hi @d6u,

If you have lost the Terraform state data, then Terraform has no way to associate the real resources with the configuration, so it must assume the resources need to be created.

The only way to recapture these resources is to import them. The data you will need to complete the import may differ by resource type, and details can often be found in the resource documentation.