Terraform:如何支持不同的提供商

时间:2018-08-24 01:23:51

标签: terraform-provider-aws

我在名为myproject的目录中有一组Terraform代码:

\myproject\ec2.tf
\myproject\provider.tf
\myproject\s3.tf
      ....

provider.tf显示:

provider "aws" {
  region  = "us-west-1"
  profile = "default"
}

因此,如果我将Terraform应用于myproject文件夹,则会在us-west-1中以我的帐户启动一组AWS资源。

现在,我想介绍一个AWS Glue资源,该资源仅在us-west-2的其他区域可用。那我该如何布置胶水.tf文件呢?

当前,我将其存储在myproject下的子目录中,然后在该子目录中运行terraform apply

\myproject\glue\glue.tf
\myproject\glue\another_provider.tf

another_provider.tf是:

provider "aws" {
  region  = "us-west-2"
  profile = "default"
}

这是在不同区域存储文件启动资源的唯一方法吗?还有更好的方法吗?

如果没有更好的方法,那么我还需要在胶子文件夹中还有另一个后端文件,此外,myproject目录中的某些公共变量无法共享。

---------更新: 我点击了Phuong Nguyen发布的链接,

provider "aws" {
  region  = "us-west-1"
  profile = "default"
}

provider "aws" {
  alias  = "oregon"
  region = "us-west-2"
  profile = "default"
}

resource "aws_glue_connection" "example" {
  provider = "aws.oregon"
   ....
}

但是我看到了

Error: aws_glue_connection.example: Provider doesn't support resource:     aws_glue_connection

1 个答案:

答案 0 :(得分:1)

您可以使用提供者别名来定义多个提供者,例如

# this is default provider
provider "aws" {
  region  = "us-west-1"
  profile = "default"
}

# additional provider
provider "aws" {
  alias  = "west-2"
  region  = "us-west-2"
  profile = "default"
}

,然后在您的glue.tf中,您可以将别名提供者称为:

resource "aws_glue_job" "example" {
  provider = "aws.west-2"

  # ...
}

Multiple Provider Instances部分的更多详细信息:https://www.terraform.io/docs/configuration/providers.html