简体   繁体   English

使用 Terraform 管理多个 AWS 区域

[英]Using Terraform to manage multiple AWS regions

Could someone please give me an example of how to programmatically create Terraform provider aliases based on a variable map?有人可以给我一个示例,说明如何基于变量映射以编程方式创建 Terraform 提供程序别名? This is what I've tried, but I'm receiving the following error:这是我尝试过的,但我收到以下错误:

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"
}

Error:错误:

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

It turns out that Terraform provider processing takes place very early and the current version (v.0.11.3) doesn't currently support variable interpolation for providers.事实证明,Terraform 提供程序处理发生的很早,当前版本 (v.0.11.3) 目前不支持提供程序的变量插值。 I did discover a workaround that isn't too terrible, but it requires a lot of code duplication.我确实发现了一个不太糟糕的解决方法,但它需要大量的代码重复。

main.tf主文件

# 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"
  }
}

cloudwatch/main.tf云表/main.tf

provider "aws" {
  alias = "region"
}

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

  tags {
    OS = "Linux"
  }
}

Use workspaces - it can be used for replicable use cases such as dev environments and multi-regions.使用工作区 - 它可用于可复制的用例,例如开发环境和多区域。 https://www.terraform.io/docs/state/workspaces.html https://www.terraform.io/docs/state/workspaces.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用提供商在多个区域部署 Terraform 资源 - Deploy Terraform resource in multiple regions using providers 无法使用Terraform在AWS自动缩放组中的多个区域中创建实例 - Not able to create instances across multiple regions in AWS autoscaling group using terraform 使用 Terraform 在不同区域创建 AWS S3 存储桶 - AWS S3 buckets creation in different regions using Terraform 使用 Bitbucket 管道使用 AWS CodeDeploy 部署到多个 AWS 区域 - Using Bitbucket Pipelines to deploy to multiple AWS regions using AWS CodeDeploy AWS 资源跨区域兼容性 terraform - AWS resource compatibility across regions with terraform 在所有 AWS 区域部署 terraform 模块 - deploy terraform module all AWS regions Terraform在某些AWS区域上运行而在其他区域上失败 - Terraform works on some AWS regions and fails on others Terraform - 具有多个环境(区域)的多个帐户 - Terraform - Multiple accounts with multiple environments (regions) 如何使用 AWS CDK 在多个区域部署相同的堆栈 - How to deploy the same stack across multiple regions using AWS CDK 如何使用 python 从 AWS 的多个区域读取值? - How to read values from multiple regions of AWS using python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM