如何在同一个 terraform 配置中使用 2 个提供程序?

Posted

技术标签:

【中文标题】如何在同一个 terraform 配置中使用 2 个提供程序?【英文标题】:How can I use 2 providers in the same terraform config? 【发布时间】:2022-01-16 01:46:16 【问题描述】:

我的 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"
  


这是我在运行✗ 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

【问题讨论】:

这与配置中提供者的数量无关。 AWS 提供商的版本约束需要修复,而自定义提供商可能完全缺少版本。试试看。 如果我删除第二个提供者并删除 terraformrc 文件(即,请参阅上面的编辑),它确实有效。 【参考方案1】:

dev_overrides 设置尽管由于其主题相似性而被放置在 provider_installation 块内,但实际上是 运行时 设置而不是安装设置。具体来说,它要求 Terraform 忽略之前选择的任何版本的提供程序 terraform init,并改用给定的覆盖提供程序。

不幸的是,此模型仅在您覆盖已经有至少一个可用发布版本的提供程序时才能正常工作,以便terraform init 可以选择并安装该版本,但随后terraform apply(例如)可以忽略terraform init 安装了什么,并改用覆盖。 (Terraform 这样做是因为terraform init 的部分职责是更新the Dependency Lock File,因此它需要为每个提供程序找到至少一个可选版本才能生成完整的锁定文件。)

避免这种设计怪癖的一种方法是将此提供程序的虚假“发布”放置在本地目录中,然后在 .terraformrc 文件中将其配置为该提供程序的文件系统镜像。

例如,如果您创建一个目录 /tmp/tf-workaround/terraform.foo.com/foo/bar/0.0.1/darwin_amd64 并在其中放入一个名为 terraform-provider-bar 的空文件,那么如果给定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,因为唯一的外部依赖项是被覆盖的提供程序,因此不需要安装任何额外的东西。

【讨论】:

【参考方案2】:

在 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

【讨论】:

以上是关于如何在同一个 terraform 配置中使用 2 个提供程序?的主要内容,如果未能解决你的问题,请参考以下文章

使用Terraform部署代码和管理配置

如何使用 Terraform 配置 EKS ALB

如何在 Terraform 中配置 CloudWatch Lambda Insights

如何配置 Azure 应用服务以使用 terraform 从 ACR 中提取图像?

如何使用 terraform 生成 yml 配置文件

如何在 VS Code 中配置 terraform 代码的对齐和缩进?