I need to deploy AWS GuardDuty across all accounts/regions in my AWS Organization. My first step is to delegate GuardDuty's administration to a designated account. Since, GuardDuty is a regional resource, I need to perform such delegation across all regions. Note that we are operating in pretty much every region of AWS.
Here is a brute-force approach for accomplishing this task:
provider "aws" {
region = "us-east-1"
alias = "us-east-1"
}
provider "aws" {
region = "us-east-2"
alias = "us-east-2"
}
# Do the same for the remaining regions
resource "aws_guardduty_organization_admin_account" "us-east-1" {
provider = aws.us-east-1
admin_account_id = "123456789..."
}
resource "aws_guardduty_organization_admin_account" "us-east-2" {
provider = aws.us-east-2
admin_account_id = "123456789..."
}
# Do the same for the remaining regions
This works, but has several disadvantages:
- A lot of repetition
- I need to remember to update this code when a new region is added
Question: Is there a better way of doing this? There is an old thread on this scenario here: https://github.com/hashicorp/terraform/issues/451
However, it does not appear that a good options existed at that time, so I want to see if better options are available now.
Ideally, I want to leverage data "aws_regions" "all" {} to loop over the existing regions and dynamically create the delegation resources and providers.
I tried the approach suggested by @Matt Schuchard:
Given this resource:
resource "aws_guardduty_organization_admin_account" "us-east-1" {
# interpolate in for expression here since not allowed in provider argument
for_each = toset([for region in data.aws_regions.this.names : "aws.${region}"])
provider = each.value
admin_account_id = "123456789..."
}
Terraform 1.0.10, produces the following error:
Error: Failed to query available provider packages
Could not retrieve the list of available versions for provider hashicorp/each: ...