对给定帐户中的每个区域使用 Terraform,查找所有 VPC,并为每个 VPC 启用流日志

对给定帐户中的每个区域使用 Terraform,查找所有 VPC,并为每个 VPC 启用流日志

在 Terraform 中,如何将 VPC ID 列表填充到地图中。我想查找给定帐户中每个区域的所有 VPC,并为每个 VPC 启用流日志。如何使用 Terraform 完成此操作

答案1

有一个例子可以做到这一点AWS 提供商文档, 就像是:

data "aws_vpcs" "foo" {}

resource "aws_flow_log" "test_flow_log" {
  count = "${length(data.aws_vpcs.foo.ids)}"
  ...
  vpc_id = "${element(data.aws_vpcs.foo.ids, count.index)}"
  ...
}

output "foo" {
  value = "${data.aws_vpcs.foo.ids}"
}

这只能覆盖一个区域(无论您在提供商中配置了哪个区域),要覆盖多个区域,您需要实例化多个提供商,每个区域一个:

# The default provider configuration
provider "aws" {
  # ...
}

# Additional provider configuration for west coast region
provider "aws" {
  alias  = "west"
  region = "us-west-2"
}

然后为流日志资源创建一个模块是有意义的传递每个提供者像这样:

# The default "aws" configuration is used for AWS resources in the root
# module where no explicit provider instance is selected.
provider "aws" {
  region = "us-west-1"
}

# A non-default, or "aliased" configuration is also defined for a different
# region.
provider "aws" {
  alias  = "usw2"
  region = "us-west-2"
}

# An example child module is instantiated with the _aliased_ configuration,
# so any AWS resources it defines will use the us-west-2 region.
module "example" {
  source    = "./example"
  providers = {
    aws = "aws.usw2"
  }
}

然后,您将在每个区域中为提供程序重复模块实例化。

相关内容