簡體   English   中英

使用 Terraform 管理多個 AWS 區域

[英]Using Terraform to manage multiple AWS regions

有人可以給我一個示例,說明如何基於變量映射以編程方式創建 Terraform 提供程序別名? 這是我嘗試過的,但我收到以下錯誤:

variable "aws_regions" {
  default = [
    {
      region = "us-east-1"
      alias  = "default"
    },
    {
      region = "us-east-2"
      alias  = "useast2"
    },
    {
      region = "us-west-1"
      alias  = "uswest1"
    },
    {
      region = "us-west-2"
      alias  = "uswest2"
    },
    {
      region = "eu-central-1"
      alias  = "eucent1"
    }
  ]
}

provider "aws" {
  count  = "${length(var.aws_regions)}"
  region = "${lookup(var.aws_regions[count.index], "region")}"
  alias  = "${lookup(var.aws_regions[count.index], "alias")}"
}

# CloudWatch Log Groups
resource "aws_cloudwatch_log_group" "linux" {
  count    = "${length(var.aws_regions)}"
  provider = "aws.${lookup(var.aws_regions[count.index], "alias")}"

  name = "Linux"
}

錯誤:

$ terraform plan
* provider.aws.${lookup(var.aws_regions[count.index], "alias")}: count.index: count.index is only valid within resources

事實證明,Terraform 提供程序處理發生的很早,當前版本 (v.0.11.3) 目前不支持提供程序的變量插值。 我確實發現了一個不太糟糕的解決方法,但它需要大量的代碼重復。

主文件

# Default Region
provider "aws" {
  region  = "us-east-1"
  version = "~> 1.8"
}

provider "aws" {
  alias  = "us-east-1"
  region = "us-east-1"
}

provider "aws" {
  alias  = "us-east-2"
  region = "us-east-2"
}

provider "aws" {
  alias  = "us-west-1"
  region = "us-west-1"
}

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

provider "aws" {
  alias  = "eu-central-1"
  region = "eu-central-1"
}

# CloudTrail Setup in Default Region
module "cloudtrail" {
  source = "./cloudtrail"
}

# CloudWatch Setup per Region
module "us-east-1_cloudwatch" {
  source = "./cloudwatch"
  providers = {
    "aws.region" = "aws.us-east-1"
  }
}

module "us-east-2_cloudwatch" {
  source = "./cloudwatch"
  providers = {
    "aws.region" = "aws.us-east-2"
  }
}

module "us-west-1_cloudwatch" {
  source = "./cloudwatch"
  providers = {
    "aws.region" = "aws.us-west-1"
  }
}

module "us-west-2_cloudwatch" {
  source = "./cloudwatch"
  providers = {
    "aws.region" = "aws.us-west-2"
  }
}

module "eu-central-1_cloudwatch" {
  source = "./cloudwatch"
  providers = {
    "aws.region" = "aws.eu-central-1"
  }
}

雲表/main.tf

provider "aws" {
  alias = "region"
}

# CloudWatch Log Groups
resource "aws_cloudwatch_log_group" "linux" {
  name     = "Linux"
  provider = "aws.region"

  tags {
    OS = "Linux"
  }
}

使用工作區 - 它可用於可復制的用例,例如開發環境和多區域。 https://www.terraform.io/docs/state/workspaces.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM