在Terraform中,有没有一种方法可以为模块使用其他提供程序?

时间:2020-09-20 05:48:36

标签: terraform terraform-provider-aws

我使用https://github.com/cloudposse/terraform-aws-acm-request-certificate使用Terraform和AWS生成证书。

是否可以为模块使用其他提供程序?

我的意思是在第一个模块中,我想在us-east-1区域中执行此操作,而另一个模块,我想在us-east-2区域中执行该操作。

但是因为我有一个提供程序,所以可以在单个文件或单个目录中使用terraform吗?

provider "aws" {
  version = "~> 2.12.0"
  profile = "..."
  region  = "us-east-1"
}

module "example_com" {
  # Should be in us-east-1
  source                            = "git::https://github.com/cloudposse/terraform-aws-acm-request-certificate.git?ref=master"
  domain_name                       = "example.com"
  process_domain_validation_options = false
  ttl                               = "300"
  subject_alternative_names         = ["*.example.com"]
}

module "cdn_example_com" {

  # Should be in us-east-2
  source                            = "git::https://github.com/cloudposse/terraform-aws-acm-request-certificate.git?ref=master"
  domain_name                       = "cdn.example.com"
  process_domain_validation_options = false
  ttl                               = "300"
}

类似的东西:

provider "aws" "aws-east-1" {
  version = "~> 2.12.0"
  profile = "..."
  region  = "us-east-1"
}

provider "aws" "aws-east-2" {
  version = "~> 2.12.0"
  profile = "..."
  region  = "us-east-2"
}

module "example_com" {
  provider = "aws-east-1"

  source                            = "git::https://github.com/cloudposse/terraform-aws-acm-request-certificate.git?ref=master"
  domain_name                       = "example.com"
  process_domain_validation_options = false
  ttl                               = "300"
  subject_alternative_names         = ["*.example.com"]
}

module "cdn_example_com" {

  provider = "aws-east-2"

  # Should be in us-east-2
  source                            = "git::https://github.com/cloudposse/terraform-aws-acm-request-certificate.git?ref=master"
  domain_name                       = "cdn.example.com"
  process_domain_validation_options = false
  ttl                               = "300"
}

2 个答案:

答案 0 :(得分:2)

下面的链接可能会很方便地解决您的目的

Multi region terraform

答案 1 :(得分:2)

基于terraform documation,您可以创建不同的提供程序并将它们“传递”到模块。

在您的情况下,可能是以下情况:

provider "aws" {
  version = "~> 2.12.0"
  profile = "..."
  region  = "us-east-1"
}


# create second provider with alias of "us-east-2" 
provider "aws" {
  version = "~> 2.12.0"
  profile = "..."
  region  = "us-east-2"
  alias   = "us-east-2" 
}

#
# Then for your modules
#

# use default provider
module "example_com" {

  source                            = "git::https://github.com/cloudposse/terraform-aws-acm-request-certificate.git?ref=master"
  domain_name                       = "example.com"
  process_domain_validation_options = false
  ttl                               = "300"
  subject_alternative_names         = ["*.example.com"]
}


# use non-default provider
module "cdn_example_com" {

   providers = {
    aws = aws.us-east-2
  }

  # Should be in us-east-2
  source                            = "git::https://github.com/cloudposse/terraform-aws-acm-request-certificate.git?ref=master"
  domain_name                       = "cdn.example.com"
  process_domain_validation_options = false
  ttl                               = "300"
}