terraform-provider-github: terraform-import failure: Cannot import non-existent remote object

Terraform Version

Terraform v0.14.3 provider “registry.terraform.io/hashicorp/github” { version = “4.1.0” }

Affected Resource(s)

  • github_repository

Terraform Configuration Files

terraform {
  required_version = "~> 0.14.0"

  backend "remote" {
    organization = "femiwiki"

    workspaces {
      name = "github"
    }
  }

  required_providers {
    github = {
      source  = "hashicorp/github"
      version = "~> 4.0"
    }
  }
}

provider "github" {
  organization = "femiwiki"
}
resource "github_repository" "legunto" {
  name = "legunto"
  ...
}

Full configuration: https://github.com/femiwiki/infra/blob/50eb0673787a8cab88041876632e45b6df6a2a57/github/repo.tf#L465-L472

Debug Output

https://gist.github.com/lens0021/35d5e8997a389bfcd91fa6cc976d2fbc

Expected Behavior

A state which is created based on https://github.com/femiwiki/legunto is Imported successfully.

Actual Behavior

“Error: Cannot import non-existent remote object” is printed. See the debug output for details.

Steps to Reproduce

  1. Uncomment https://github.com/femiwiki/infra/blob/50eb0673787a8cab88041876632e45b6df6a2a57/github/repo.tf#L465-L472
  2. terraform import github_repository.legunto legunto

Important Factoids

Our team is using Terraform Cloud (https://app.terraform.io/).

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 6
  • Comments: 26 (6 by maintainers)

Commits related to this issue

Most upvoted comments

I’ve also been experiencing this issue, specifically when trying to import a github_repository resource into a child module.

If I run export GITHUB_OWNER=<org name>, the import works successfully.

@kfcampbell Thank you for your investigation.

I retried the import in few other ways, I have noticed it was GITHUB_TOKEN that I was missing.

$ export GITHUB_TOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
$ terraform import github_repository.legunto legunto
...
Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

It will be helpful if there could be a message for the missing token, Thanks!

I managed to solve the issue for me. It looks like I didn’t set GITHUB_TOKEN locally, and was assuming it would be using Terraform Cloud’s GITHUB_TOKEN. My bad, but perhaps it would be helpful to have a message, at least at the TF_LOG=INFO that clearly tells the use they haven’t set GITHUB_TOKEN. Thanks!

I have now solved this problem.

Several issues may have contributed.

  • I may or may not have started with the now-deprecated hashicorp/github. You must ensure with terraform -version or terraform providers that only one provider is present. If more than is present, remove it with terraform state replace-provider registry.terraform.io/hashicorp/github registry.terraform.io/integrations/github.

  • The docs for this provider note that a nested module must have a providers import there as well, not just at the root. Otherwise everything breaks horribly. Go into your subdirectories and put this in providers.tf or something:

# this must be included in each submodule or you will get horrible and strange errors
terraform {
  required_providers {
    github = {
      source  = "integrations/github"
      version = "~> 5.0"
    }
  }
}
  • My module is nested. You must provide a path to it. terraform import -config=github/repos github_repository.embeddings_api embeddings_api, where github and repos are both directories.

  • Finally, the resource was still not properly created. After import, Terraform wanted to destroy and to create the resource. Of course Github will not allow it to do so. Looking at terraform.tfstate, the new resource (my imported repo) was missing this at the top: "module": "module.repos",. All the other blocks had it. I manually added it and things worked. I had to do this for every imported resource.

god what a journey. Hope this helps and good luck.

Setting GITHUB_TOKEN and GITHUB_OWNER in the CLI worked for me too. However, GITHUB_TOKEN doesn’t have to be the one set in Terraform Cloud – in fact, you really shouldn’t do it for security reasons. Just create a GitHub PAT under your own account with the read-only public_repo scope if it’s public (otherwise you’re going to have to give it the repo scope).

The original code and comment mentions that we shouldn’t change the logic for how the configuration sets the owner without creating a new major release as it’d break backwards compatibility.

In the meantime, I’ve created this PR as a starting point to log which organization/owner is selected and therefore hopefully make it easier to see how the configuration is being interpreted. Do the folks on this thread see value/benefit in this? Feedback is appreciated.