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
- Uncomment https://github.com/femiwiki/infra/blob/50eb0673787a8cab88041876632e45b6df6a2a57/github/repo.tf#L465-L472
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)
I’ve also been experiencing this issue, specifically when trying to import a
github_repositoryresource 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_TOKENthat I was missing.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_TOKENlocally, and was assuming it would be using Terraform Cloud’sGITHUB_TOKEN. My bad, but perhaps it would be helpful to have a message, at least at theTF_LOG=INFOthat clearly tells the use they haven’t setGITHUB_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 -versionorterraform providersthat only one provider is present. If more than is present, remove it withterraform 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.tfor something:My module is nested. You must provide a path to it.
terraform import -config=github/repos github_repository.embeddings_api embeddings_api, wheregithubandreposare 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_TOKENandGITHUB_OWNERin the CLI worked for me too. However,GITHUB_TOKENdoesn’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-onlypublic_reposcope if it’s public (otherwise you’re going to have to give it thereposcope).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.