解决Terraform的错误:提供程序配置不存在
本文是链接巴尔日历2022年的最后一篇文章。
首先
我曾经在 Terraform 的 0.12 版本的存储库中进行工作,试图升级版本时遇到了”Error: Provider configuration not present”错误。由于找不到解决这个错误的日语文章,所以我将写下我自己解决的方法。
环境
-
- OS: macOS Ventura Version 13.0.1
-
- terraform:
before version: 0.12.0
after version: 0.13.7
provider: aws 4.39.0
「快点告诉我结论吧!」你说完这句话,就跳到解决步骤的总结部分!
错误内容
由于我想要使用 `for_each` 在 0.12 版本中无法使用,因此我升级到了 terraform 0.13,并进行了 `terraform init –reconfigure` 和 `terraform plan`。然后出现了以下错误…
$ terraform 0.13upgrade
...
$ terraform init --reconfigure
...
$ terafform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan,
but will not be persisted to local or remote state storage.
Error: Provider configuration not present
To work with data.aws_iam_policy_document.xxxxx
its original provider configuration at
provider["registry.terraform.io/-/aws"] is required,
but it has been removed.
This occurs when a provider configuration is removed
while objects created by that provider still exist in the state.
Re-add the provider configuration to destroy
data.aws_iam_policy_document.xxxxx,
after which you can remove the provider configuration again.
嗯,我在TOEIC考试中获得了375分,所以我会毫不犹豫地依赖谷歌翻译。
需要provider[“registry.terraform.io/-/aws”]的原始提供者配置,但它已被删除。
这是在提供者配置被删除时,由该提供者创建的对象仍然存在于状态中时才会发生的。
请重新添加提供者配置并销毁data.aws_iam_policy_document.xxxxx。然后才能再次删除提供者配置。
由于不太明白,我在搜索错误内容后,以下的文章在我浏览器的搜索结果中排在第一位。
如果将文章内容套用到我的情况中,我觉得删除[“registry.terraform.io/-/aws”]可能是可行的。
试试看
将实际尝试的内容直接记录下来。
我在评论中进行了简短的解释。
# providerを確認する
$ terraform providers
Providers required by configuration:
.
└── provider[registry.terraform.io/hashicorp/aws]
Providers required by state:
provider[registry.terraform.io/-/aws]
# 念の為リモートのstateをローカルに保存しておく
$ terraform state pull > terraform.tfstate.manualbackup20221225
# providerをリプレイスしてみる
$ terraform state replace-provider 'registry.terraform.io/-/aws' 'registry.terraform.io/hashicorp/aws'
Terraform will perform the following actions:
~ Updating provider:
- registry.terraform.io/-/aws
+ registry.terraform.io/hashicorp/aws
Changing 11 resources:
aws_wafv2_regex_pattern_set.xxx
data.aws_iam_policy_document.xxxxx
aws_iam_role_policy_attachment.xxx
aws_wafv2_web_acl.xxx
aws_backup_plan.xxx
aws_instance.xxx
aws_sqs_queue.xxx
aws_sqs_queue.xxx_xxx
aws_backup_selection.xxx
aws_backup_vault.xxx
aws_iam_policy.xxx
Do you want to make these changes?
Only 'yes' will be accepted to continue.
# yes入力
Enter a value: yes
# 改めてplanしても変わらずエラー...
$ terafform plan
# 出力は割愛
# ファイルを確認してみる
$tree -a
.
├── .terraform
│ ├── plugins
│ │ ├── darwin_amd64
│ │ │ ├── lock.json
│ │ │ └── terraform-provider-aws_v3.37.0_x5
│ │ ├── registry.terraform.io
│ │ │ ├── - # こいつはなんなんだ
│ │ │ │ └── aws
│ │ │ │ └── 4.41.0
│ │ │ │ └── darwin_amd64
│ │ │ │ └── terraform-provider-aws_v4.41.0_x5
│ │ │ └── hashicorp
│ │ │ └── aws
│ │ │ └── 4.41.0
│ │ │ └── darwin_amd64
│ │ │ └── terraform-provider-aws_v4.41.0_x5
│ │ └── selections.json
│ ├── terraform.tfstate
│ └── terraform.tfstate.manualbackup20221201-1506
├── .terraform-version
├── README.md
├── backup.tf
├── ...
# .terraform/registry.terraform.io/-/ が原因っぽい
# 削除前に念の為どこかにコピーしておく
$ cp -r .terraform/registry.terraform.io/-/ ~/
# registry.terraform.io/- を削除する
$ rm -rf .terraform/registry.terraform.io/-/
# 再度providerを確認すると [registry.terraform.io/-/aws] は消えている
$ terraform providers
Providers required by configuration:
.
└── provider[registry.terraform.io/hashicorp/aws]
Providers required by state:
provider[registry.terraform.io/hashicorp/aws]
# 再度planをすると別のエラーが出る
$ terraform plan
Error: Could not load plugin
Plugin reinitialization required. Please run "terraform init".
Plugins are external binaries that Terraform uses to access and manipulate resources.
The configuration provided requires plugins which can't be located,
don't satisfy the version constraints, or are otherwise incompatible.
Terraform automatically discovers provider requirements from your configuration,
including providers used in child modules. To see the
requirements and constraints, run "terraform providers".
Failed to instantiate provider "registry.terraform.io/hashicorp/aws" to obtain
schema: unknown provider "registry.terraform.io/hashicorp/aws"
# Please run "terraform init". と言われたとおり、initする
$ terraform init
Initializing the backend...
Initializing provider plugins...
- Using previously-installed hashicorp/aws v4.41.0
The following providers do not have any version constraints in configuration,
so the latest version was installed.
To prevent automatic upgrades to new major versions that may contain breaking
changes, we recommend adding version constraints in a required_providers block
in your configuration, with the constraint strings suggested below.
* hashicorp/aws: version = "~> 4.41.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.
$ terraform plan
# 成功!
解决步骤总结
-
- 删除附有”-providers”的providers文件中所列的错误。
示例 rm -rf .terraform/registry.terraform.io/-/
terraform init
有附加标签的目录是什么。
说实话,我不明白。
我知道的只有通过执行terraform 0.13升级产生的事情。
以下是我的粗略假设。
terraform 0.13升级后,将自动生成以下内容的terraform/versions.tf文件。
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
required_version = ">= 0.13"
}
hashicorp/aws 好像与现有目录冲突了,导致现有目录的名称变成了 – ?
由于没有确认 Terraform 0.13 升级之前的状态,所以不太清楚。
结束
请度过一个愉快的圣诞节!