如何在同一 Terraform 配置中使用 2 个提供程序?
How can I use 2 providers in the same terraform config?
我的 main.tf 文件以
开头
terraform {
required_version = ">= 0.13.7"
required_providers {
aws = {
source = "hashicorp/aws"
version = "= 2.32.0"
}
foobar = {
source = "terraform.foo.com/foo/bar"
}
}
}
这里的问题是 foo/bar 是我在本地开发的模块,所以我也有这个 terraformrc 文件:
provider_installation {
dev_overrides {
"terraform.foo.com/foo/bar" = "/Users/appuser/foobar/bin/darwin-amd64"
}
}
这是我 运行 在 运行ning ✗ terraform init
时遇到的错误
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/aws versions matching "2.32.0"...
- Finding latest version of terraform.foo.com/foo/bar...
Warning: Provider development overrides are in effect
The following provider development overrides are set in the CLI configuration:
- "terraform.foo.com/foo/bar" = "/Users/appuser/foobar/bin/darwin-amd64"
Error: Failed to query available provider packages
Could not retrieve the list of available versions for provider hashicorp/aws:
no available releases match the given constraints 2.32.0
Error: Failed to query available provider packages
Could not retrieve the list of available versions for provider
"terraform.foo.com/foo/bar": no available releases
match the given constraints
更新:当我删除 terraformrc 时,它似乎可以工作,但我无法以这种方式加载第二个提供程序(因为它依赖于覆盖):
terraform init
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/aws versions matching "2.32.0"...
- Finding latest version of hashicorp/foo/bar...
- Installing hashicorp/aws v2.32.0...
- Installed hashicorp/aws v2.32.0 (self-signed, key ID 34365D9472D7468F)
Partner and community providers are signed by their developers.
If you'd like to know more about provider signing, you can read about it here:
https://www.terraform.io/docs/plugins/signing.html
Error: Failed to query available provider packages
Could not retrieve the list of available versions for provider
hashicorp/foo/bar: provider registry registry.terraform.io does not
have a provider named registry.terraform.io/hashicorp/foo/bar
已在 TF docs:
中找到修复(即添加 direct {}
)
provider_installation {
# Use /home/developer/tmp/terraform-null as an overridden package directory
# for the hashicorp/null provider. This disables the version and checksum
# verifications for this provider and forces Terraform to look for the
# null provider plugin in the given directory.
dev_overrides {
"hashicorp/null" = "/home/developer/tmp/terraform-null"
}
# For all other providers, install them directly from their origin provider
# registries as normal. If you omit this, Terraform will _only_ use
# the dev_overrides block, and so no other providers will be available.
direct {}
}
其实我还在收
Error: Failed to query available provider packages
Could not retrieve the list of available versions for provider
hashicorp/foo/bar: could not connect to hashicorp: Failed
to request discovery document: Get
"https://hashicorp/.well-known/terraform.json": dial tcp: lookup hashicorp on
100.217.9.1:53: no such host
dev_overrides
设置,尽管由于其主题相似性而被放置在 provider_installation
块内,实际上是 runtime 设置而不是安装设置。具体来说,它要求 Terraform 忽略之前 select 提供程序 terraform init
的任何版本,并改为使用给定的覆盖提供程序。
不幸的是,只有当您覆盖已经有至少一个可用发布版本的提供程序时,此模型才能正常工作,因此 terraform init
可以 select 并安装该版本,然后 terraform apply
(例如)然后可以忽略 terraform init
安装的内容,并改用覆盖。 (Terraform 之所以如此,是因为 terraform init
的部分职责是更新 the Dependency Lock File,因此它需要为每个提供者找到至少一个 select 可用版本,以便生成完整的锁定文件。)
避免这种设计怪癖的一种方法是在本地目录中放置此提供程序的虚假“版本”,然后在 .terraformrc
文件中将其配置为该提供程序的文件系统镜像.
例如,如果您创建一个目录 /tmp/tf-workaround/terraform.foo.com/foo/bar/0.0.1/darwin_amd64
并将一个名为 terraform-provider-bar
的空文件放入其中,那么这应该足以让 terraform init
将其检测为可用的“已发布” " 提供程序的版本,如果给定这样的 CLI 配置:
provider_installation {
dev_overrides {
"terraform.foo.com/foo/bar" = "/Users/appuser/foobar/bin/darwin-amd64"
}
filesystem_mirror {
path = "/tmp/tf-workaround"
include = ["terraform.foo.com/foo/bar"]
}
direct {
exclude = ["terraform.foo.com/foo/bar"]
}
}
terraform init
然后应该找到占位符空文件并将其“安装”到 .terraform/providers
中。该空文件实际上不能用作有效插件,但没关系,因为 terraform apply
无论如何都会忽略它,而是使用 dev_overrides
中给出的目录。
但是,依赖锁文件将包含 terraform.foo.com/foo/bar
安装后的错误条目,因此如果您打算将此测试配置提交到版本控制,那么您可能希望手动删除该块以减少一次混淆这个提供者真的有一个版本。
此处不太复杂的工作方式是在开发期间使用 仅 包含该提供程序的配置测试提供程序,然后等到您在某处实际发布该提供程序在将其“真正”用作更大系统的一部分之前。在这种情况下,您通常会完全跳过 运行 terraform init
,因为唯一的外部依赖项是被覆盖的提供程序,因此不需要安装任何其他内容。
我的 main.tf 文件以
开头terraform {
required_version = ">= 0.13.7"
required_providers {
aws = {
source = "hashicorp/aws"
version = "= 2.32.0"
}
foobar = {
source = "terraform.foo.com/foo/bar"
}
}
}
这里的问题是 foo/bar 是我在本地开发的模块,所以我也有这个 terraformrc 文件:
provider_installation {
dev_overrides {
"terraform.foo.com/foo/bar" = "/Users/appuser/foobar/bin/darwin-amd64"
}
}
这是我 运行 在 运行ning ✗ terraform init
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/aws versions matching "2.32.0"...
- Finding latest version of terraform.foo.com/foo/bar...
Warning: Provider development overrides are in effect
The following provider development overrides are set in the CLI configuration:
- "terraform.foo.com/foo/bar" = "/Users/appuser/foobar/bin/darwin-amd64"
Error: Failed to query available provider packages
Could not retrieve the list of available versions for provider hashicorp/aws:
no available releases match the given constraints 2.32.0
Error: Failed to query available provider packages
Could not retrieve the list of available versions for provider
"terraform.foo.com/foo/bar": no available releases
match the given constraints
更新:当我删除 terraformrc 时,它似乎可以工作,但我无法以这种方式加载第二个提供程序(因为它依赖于覆盖):
terraform init
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/aws versions matching "2.32.0"...
- Finding latest version of hashicorp/foo/bar...
- Installing hashicorp/aws v2.32.0...
- Installed hashicorp/aws v2.32.0 (self-signed, key ID 34365D9472D7468F)
Partner and community providers are signed by their developers.
If you'd like to know more about provider signing, you can read about it here:
https://www.terraform.io/docs/plugins/signing.html
Error: Failed to query available provider packages
Could not retrieve the list of available versions for provider
hashicorp/foo/bar: provider registry registry.terraform.io does not
have a provider named registry.terraform.io/hashicorp/foo/bar
已在 TF docs:
中找到修复(即添加direct {}
)
provider_installation {
# Use /home/developer/tmp/terraform-null as an overridden package directory
# for the hashicorp/null provider. This disables the version and checksum
# verifications for this provider and forces Terraform to look for the
# null provider plugin in the given directory.
dev_overrides {
"hashicorp/null" = "/home/developer/tmp/terraform-null"
}
# For all other providers, install them directly from their origin provider
# registries as normal. If you omit this, Terraform will _only_ use
# the dev_overrides block, and so no other providers will be available.
direct {}
}
其实我还在收
Error: Failed to query available provider packages
Could not retrieve the list of available versions for provider
hashicorp/foo/bar: could not connect to hashicorp: Failed
to request discovery document: Get
"https://hashicorp/.well-known/terraform.json": dial tcp: lookup hashicorp on
100.217.9.1:53: no such host
dev_overrides
设置,尽管由于其主题相似性而被放置在 provider_installation
块内,实际上是 runtime 设置而不是安装设置。具体来说,它要求 Terraform 忽略之前 select 提供程序 terraform init
的任何版本,并改为使用给定的覆盖提供程序。
不幸的是,只有当您覆盖已经有至少一个可用发布版本的提供程序时,此模型才能正常工作,因此 terraform init
可以 select 并安装该版本,然后 terraform apply
(例如)然后可以忽略 terraform init
安装的内容,并改用覆盖。 (Terraform 之所以如此,是因为 terraform init
的部分职责是更新 the Dependency Lock File,因此它需要为每个提供者找到至少一个 select 可用版本,以便生成完整的锁定文件。)
避免这种设计怪癖的一种方法是在本地目录中放置此提供程序的虚假“版本”,然后在 .terraformrc
文件中将其配置为该提供程序的文件系统镜像.
例如,如果您创建一个目录 /tmp/tf-workaround/terraform.foo.com/foo/bar/0.0.1/darwin_amd64
并将一个名为 terraform-provider-bar
的空文件放入其中,那么这应该足以让 terraform init
将其检测为可用的“已发布” " 提供程序的版本,如果给定这样的 CLI 配置:
provider_installation {
dev_overrides {
"terraform.foo.com/foo/bar" = "/Users/appuser/foobar/bin/darwin-amd64"
}
filesystem_mirror {
path = "/tmp/tf-workaround"
include = ["terraform.foo.com/foo/bar"]
}
direct {
exclude = ["terraform.foo.com/foo/bar"]
}
}
terraform init
然后应该找到占位符空文件并将其“安装”到 .terraform/providers
中。该空文件实际上不能用作有效插件,但没关系,因为 terraform apply
无论如何都会忽略它,而是使用 dev_overrides
中给出的目录。
但是,依赖锁文件将包含 terraform.foo.com/foo/bar
安装后的错误条目,因此如果您打算将此测试配置提交到版本控制,那么您可能希望手动删除该块以减少一次混淆这个提供者真的有一个版本。
此处不太复杂的工作方式是在开发期间使用 仅 包含该提供程序的配置测试提供程序,然后等到您在某处实际发布该提供程序在将其“真正”用作更大系统的一部分之前。在这种情况下,您通常会完全跳过 运行 terraform init
,因为唯一的外部依赖项是被覆盖的提供程序,因此不需要安装任何其他内容。