Search code examples
githubterraformterraform-provider-awsterraform0.12+terraform-provider-github

Terraform Import Issue: Objects have changed outside of Terraform


On tf version 1.0.3

I import a resource locally:

terraform import github_repository.terraform-xxxx-github_repo terraform-xxxx-github
github_repository.terraform-xxxx-github_repo: Importing from ID "terraform-xxxx-github"...
github_repository.terraform-xxxx-github_repo: Import prepared!
  Prepared github_repository for import
github_repository.terraform-xxxx-github_repo: Refreshing state... [id=terraform-xxxx-github]

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.

this is the resource config in repos.tf file:

resource "github_repository" "terraform-xxxx-github_repo" {
  name                   = "terraform-xxxx-github"
  visibility             = "private"
  has_projects           = true
  has_wiki               = true
  has_issues             = true
  has_downloads          = true
  allow_merge_commit     = true
  allow_squash_merge     = true
  allow_rebase_merge     = true
  archived               = false
  auto_init              = false
}

Now when locally I run tf init and tf plan I do see no changes:

Initializing the backend...

Initializing provider plugins...
- Reusing previous version of integrations/github from the dependency lock file
- Reusing previous version of hashicorp/aws from the dependency lock file
- Using previously-installed integrations/github v4.13.0
- Using previously-installed hashicorp/aws v3.54.0

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

and when I run tf plan:

github_repository.terraform-xxxx-github_repo: Refreshing state... [id=terraform-xxxx-github]

No changes. Your infrastructure matches the configuration.

Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.

However, the problem is that when I push my local into my github PR which is connected to Terraform running on Jenkins... in the Jenkins tf plan I see this:

10:59:30  + terraform plan -input=false -out=terraform.plan
10:59:33  github_repository.terraform-xxxx-github_repo: Refreshing state... [id=terraform-xxxx-github]
10:59:33  
10:59:33  Note: Objects have changed outside of Terraform
10:59:33  
10:59:33  Terraform detected the following changes made outside of Terraform since the
10:59:33  last "terraform apply":
10:59:33  
10:59:33    # github_repository.terraform-xxxx-github_repo has been deleted
10:59:33    - resource "github_repository" "terraform-xxxx-github_repo" {
10:59:33        - allow_merge_commit     = true -> null
10:59:33        - allow_rebase_merge     = true -> null
10:59:33        - allow_squash_merge     = true -> null
10:59:33        - archived               = false -> null
10:59:33        - auto_init              = false -> null
10:59:33        - default_branch         = "master" -> null
10:59:33        - visibility             = "private" -> null
10:59:33      }
10:59:33  
10:59:33  Unless you have made equivalent changes to your configuration, or ignored the
10:59:33  relevant attributes using ignore_changes, the following plan may include
10:59:33  actions to undo or respond to these changes.
10:59:33  
10:59:33  ─────────────────────────────────────────────────────────────────────────────
10:59:33  
10:59:33  Terraform used the selected providers to generate the following execution
10:59:33  plan. Resource actions are indicated with the following symbols:
10:59:33    + create
10:59:33  
10:59:33  Terraform will perform the following actions:
10:59:33  
10:59:33    # github_repository.terraform-xxxx-github_repo will be created
10:59:33    + resource "github_repository" "terraform-xxxx-github_repo" {
10:59:33        + allow_merge_commit     = true
10:59:33        + allow_rebase_merge     = true
10:59:33        + allow_squash_merge     = true
10:59:33        + archived               = false
10:59:33        + auto_init              = false
10:59:33        + default_branch         = (known after apply)
10:59:33        + visibility             = "private"
10:59:33      }
10:59:33  
10:59:33  Plan: 1 to add, 0 to change, 0 to destroy.

So it is not picking up the terraform import I did locally. I do not know why this is happening and have no idea how to fix this.


Solution

  • The solution to this was that the github personal access token being used did not have the correct permissions for the github account/repo. So using a new token with more access permissions was used and this issue was fixed.