【发布时间】:2021-12-21 09:40:37
【问题描述】:
我需要在我的 AWS 组织中的所有账户/区域中部署 AWS GuardDuty。 我的第一步是将 GuardDuty 的管理委派给指定帐户。由于 GuardDuty 是一个区域资源,因此我需要在所有区域执行此类委派。请注意,我们几乎在 AWS 的每个区域开展业务。
这是完成此任务的蛮力方法:
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
这可行,但有几个缺点:
- 大量重复
- 我需要记住在添加新区域时更新此代码
问题:有没有更好的方法来做到这一点? 这里有一个关于这种情况的旧线程: https://github.com/hashicorp/terraform/issues/451
但是,当时似乎没有好的选择,所以我想看看现在是否有更好的选择。
理想情况下,我想利用data "aws_regions" "all" {} 循环现有区域并动态创建委托资源和提供者。
我尝试了@Matt Schuchard 建议的方法:
鉴于此资源:
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,产生以下错误:
Error: Failed to query available provider packages
Could not retrieve the list of available versions for provider hashicorp/each: ...
【问题讨论】:
-
您可以为资源执行此操作,但根据路线图,这仍在为提供商开发中。如果可以的话,我可以写出相应的答案。
-
@MattSchuchard,是的,请为资源部分提供答案。
标签: terraform terraform-provider-aws