Terraform 导入问题:对象已在 Terraform 之外更改

Terraform Import Issue: Objects have changed outside of Terraform

tf 版本 1.0.3

我在本地导入一个资源:

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.

这是 repos.tf 文件中的资源配置:

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
}

现在,当我在本地 运行 tf init 和 tf plan 时,我没有看到任何变化:

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.

当我 运行 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.

但是,问题是当我将我的本地推送到我的 github PR 时,它连接到 Jenkins 上的 Terraform 运行ning...在 Jenkins tf 计划中我看到了这个:

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.

所以它没有获取我在本地执行的 terraform 导入。我不知道为什么会这样,也不知道如何解决这个问题。

此问题的解决方案是所使用的 github 个人访问令牌没有 github account/repo 的正确权限。 因此使用了具有更多访问权限的新令牌并修复了此问题。