Terraform-从提供程序获取区域并在资源名称中使用它

时间:2020-08-11 10:30:37

标签: terraform terraform-provider-aws

provider "aws" {
  alias  = "sin"
  region = "ap-southeast-1"
}

provider "aws" {
  alias  = "mum"
  region = "ap-south-1"
}

resource "aws_sns_topic" "alarms_sin" {
  provider = aws.sin

  name = "${local.name_prefix}-alarms-${GET_CURRENT_REGION}"
}

resource "aws_sns_topic" "alarms_mum" {
  provider = aws.mum

  name = "${local.name_prefix}-alarms-${GET_CURRENT_REGION}"
}

通常,您可以使用data "aws_region" "current" {}之类的数据块,但在这种情况下不起作用。

1 个答案:

答案 0 :(得分:2)

我能够通过在数据块中添加提供程序来解决它-

provider "aws" {
  alias  = "sin"
  region = "ap-southeast-1"
}

provider "aws" {
  alias  = "mum"
  region = "ap-south-1"
}

// Change
data "aws_region" "sin" {
  provider = "aws.sin"
}

data "aws_region" "mum" {
  provider = "aws.mum"
}

resource "aws_sns_topic" "alarms_sin" {
  provider = aws.sin

  name = "${local.name_prefix}-alarms-${data.aws_region.sin.name}"
}

resource "aws_sns_topic" "alarms_mum" {
  provider = aws.mum

  name = "${local.name_prefix}-alarms-${data.aws_region.mum.name}"
}