将别名 AWS 提供程序传递给子模块 Terraform

3
我正在尝试将两个AWS Terraform提供程序传递给我的子模块。 我希望默认值保持未别名,因为我无法在父模块中的所有terraform资源中添加提供程序。
父模块------------------------------------------ versions.tf
terraform {
  required_version = "~> 1.0"

  backend "remote" {
    hostname     = "app.terraform.io"
    organization = "some-org"

    workspaces {
      prefix = "some-state-file"
    }
  }

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.0"
      configuration_aliases = [ aws.domain-management ]
    }
  }
}

provider "aws" {
  access_key = var.aws_access_key_id
  secret_key = var.aws_secret_access_key
  region     = var.aws_region

  default_tags {
    tags = {
      Application = var.application_name
      Environment = var.environment
    }
  }
}

provider "aws" {
  alias      = "domain-management"
  region     = var.domain_management_aws_region
  access_key = var.domain_management_aws_access_key_id
  secret_key = var.domain_management_aws_secret_access_key
}

module.tf(调用子模块)

module "vanity-cert-test" {
  source = "some-source"

  fully_qualified_domain_name = "some-domain.com"
  alternative_names           = ["*.${var.dns_zone.name}"]
  application_name            = var.application_name
  environment                 = var.environment
  service_name                = var.service_name
  domain_managment_zone_name  = "some-domain02.com"

  providers = {
    aws.domain-management = aws.domain-management
  }
}

子模块-------------------------------------------------------

versions.tf


该文件用于定义子模块的版本信息。
terraform {
  required_version = "~> 1.0"

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.0"
      confiuration_aliases = [aws.domain-management]
    }
  }
}

provider "aws" {
  alias = domain-management
}

route53.tf

# Create validation Route53 records
resource "aws_route53_record" "vanity_route53_cert_validation" {
  # use domain management secondary aws provider
  provider = aws.domain-management

  for_each = {
    for dvo in aws_acm_certificate.vanity_certificate.domain_validation_options : dvo.domain_name => {
      name   = dvo.resource_record_name
      record = dvo.resource_record_value
      type   = dvo.resource_record_type
    }
  }

  zone_id         = data.aws_route53_zone.vanity_zone.zone_id
  name            = each.value.name
  records         = [each.value.record]
  ttl             = 60
  type            = each.value.type
  allow_overwrite = true
}

这种情况的应用场景是在与证书DNS验证不同的账户中定义虚荣证书。目前运行时,我收到以下错误消息:

terraform plan -var-file=./application.tfvars


 Warning: Provider aws.domain-management is undefined
 
   on services/self-service-ticket-portal-app/ssl-certificate.tf line 33, in module "vanity-cert-test":
   33:     aws.domain-management = aws.domain-management
 
 Module module.services.module.self-service-ticket-portal-app.module.vanity-cert-test does not declare a provider named aws.domain-management.
 If you wish to specify a provider configuration for the module, add an entry for aws.domain-management in the required_providers block within the module.


 Error: missing provider module.services.module.self-service-ticket-portal-app.provider["registry.terraform.io/hashicorp/aws"].domain-management
1个回答

3
如果你的"父模块"是根模块,那么你不能在其中使用configuration_aliasesconfiguration_aliases只能在子模块中使用:

为了在模块内声明一个配置别名,以便从父模块接收备用提供程序配置,请将configuration_aliases参数添加到该提供程序的required_providers条目中。


Marcin,如果您在子资源中没有指定provider属性,它会使用根资源中的默认(非别名)提供程序吗? - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接