模块中的 Terraform 提供者

问题描述 投票:0回答:1

我正在编写模块,模块属性之一是提供者。

resource "aws_route53_record" "sara-client" {
  provider = aws.naxex
  zone_id  = var.route53_zone_id
  name     = var.route53_api_domain
  type     = var.route53_zone_id_type

下面是我的子模块:

module "aws_route53_record" {
  source = "../modules"

  provider                   = aws.myalias
  route53_zone_id            = var.route53_zone_id
  route53_api_domain         = var.route53_api_domain
  route53_zone_id_type       = var.route53_zone_id_type
}

在我的提供者文件中,我有以下内容:

  region  = local.region
  alias   = "myalias"
  profile = "myalias"
  default_tags {
    tags = {
      Project     = local.projectName
      Environment = local.environment
      Managedby   = "Teraform"
    }
  }
}

当我尝试运行 Terraform 计划时遇到的错误是:

│ Error: Missing required provider configuration
│
│   on route53.tf line 1:
│    1: module "aws_route53_record" {
│
│ The child module requires an additional configuration for provider hashicorp/aws, with the local name "aws.myalias".
│
│ Refer to the module's documentation to understand the intended purpose of this additional provider configuration, and then add an entry for aws.myaliasin the "providers" meta-argument in the module block to
│ choose which provider configuration the module should use for that purpose.

我需要添加哪些额外配置才能使模块正常工作?

terraform
1个回答
0
投票

您似乎已经了解了 provider

 块中的 
resource
元参数,它指定了管理该特定资源的一个提供程序配置。

但是,

module
块的等效参数有点不同:一个模块通常包含多个
resource
块,这些块不一定都使用相同的提供程序,因此
modules
的等效项是 复数
providers
元参数
,可以一次将多个提供程序配置传递到子模块中。

虽然您没有将其包含在您的问题中,但我假设您也已经在

configuration_aliases
块中找到了
required_providers
参数,您已使用该参数声明此模块需要 AWS 提供商的“myalias”配置,写了这样的东西:

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"

      configuration_aliases = [
        aws.myalias,
      ]
    }
  }
}

您使用

configuration_aliases
声明的每个提供程序配置要求必须在调用此模块的
providers
块的
module
参数中具有匹配的条目。如果您只有这一个
aws.myalias
要求,并且您想将根模块的
aws.naxex
传递给它,那么您的
module
块应包含以下内容:

module "aws_route53_record" {
  source = "../modules"
  providers = {
    aws.myalias = aws.naxex
  }

  # ...
}

上面的配置意味着“每当子模块引用名为

aws.myalias
的提供程序时,请使用 this 模块的
aws.naxex
提供程序配置。这意味着当您的子模块使用
provider = aws.myalias
声明资源时,它将使用相同的资源您的根模块调用“naxex”的提供程序配置。

© www.soinside.com 2019 - 2024. All rights reserved.