In Terraform how to populate the list of VPC IDs into the map. I want to find all VPCs for every region in a given account and for each VPC enable flow logs.How this can be done using Terraform
Using Terraform for every region in a given account, find all VPCs and for each VPC enable flow logs
Asked
Active
Viewed 756 times
1 Answers
1
There's an example to do exactly this in the AWS provider documentation, something like:
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}"
}
This would only cover one region, (whatever region you've configured in the provider), to do multiple regions, you need to instantiate multiple providers, one in each region:
# The default provider configuration
provider "aws" {
# ...
}
# Additional provider configuration for west coast region
provider "aws" {
alias = "west"
region = "us-west-2"
}
It then would make sense to create a module for your flow log resources and then pass in each provider like so:
# 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"
}
}
You would then repeat the module instantiation for the provider in each of the regions.

bodgit
- 4,751
- 16
- 27