amazon-web-services - Terraform:在另一个资源中引用在 for_each 中创建的资源
问题描述
当我有一个托管区域时,我很容易创建该区域,然后通过按名称引用托管区域来在委派帐户中为该区域创建 NS 记录。
- 编辑为了避免混淆,这是我想要实现的,但对于多个托管区域并且域的所有者是管理帐户:https ://dev.to/arswaw/create-a-subdomain-in-amazon-route53 -in-2-minutes-3hf0
现在我需要创建多个托管区域并将名称服务器记录传递回父帐户,我不确定它是否可能(或如何)引用多个资源。阅读这篇文章可能没有多大意义,所以下面的代码就我所知。
我现在有一个for_each
循环,它将遍历字符串列表并为每个字符串创建一个托管区域,然后我想在另一个帐户中创建相应的 NS 记录,请注意我正在使用单独的提供程序provider = aws.management_account
连接到管理帐户和这适用于单个托管区域。
我不知道如何引用托管区域,是否有一些语法或我的方法错误?
resource "aws_route53_zone" "public_hosted_zone" {
for_each = local.aws_zones
name = "${each.value}.${var.domain}"
}
resource "aws_route53_record" "ns_records" {
for_each = local.aws_zones
provider = aws.management_account
allow_overwrite = true
name = "${each.value}.${var.domain}"
ttl = 30
type = "NS"
zone_id = data.aws_ssm_parameter.public_hosted_zone_id.value
records = [
aws_route53_zone.public_hosted_zone.name_servers[0], # Here is my old code which works for a single hosted zone but I cannot work out how to reference multiples created above
aws_route53_zone.public_hosted_zone.name_servers[1],
aws_route53_zone.public_hosted_zone.name_servers[2],
aws_route53_zone.public_hosted_zone.name_servers[3]
]
}
解决方案
由于你local.aws_zones
的设置为 ["dev", "test", "qa"],你aws_route53_zone.public_hosted_zone
将是一个带有键 "dev"、"test"、"qa" 的映射。
因此,要在您的 中使用它aws_route53_record
,您可以尝试:
resource "aws_route53_record" "ns_records" {
for_each = local.aws_zones
# other attributes
records = aws_route53_zone.public_hosted_zone[each.key].name_servers
}
推荐阅读
- datetime - 如何在 angular-bootstrap-datetimepicker 中更改日期时间格式
- javascript - Import image from './image.jpeg' results Failed to load module script 服务器以“image/jpegerror”的非 JavaScript MIME 类型响应
- c++ - C ++中标识符名称后跟()后的&是什么意思?
- reactjs - Meteor.logout() 导致 React 中的内存泄漏
- c++ - 如何分配不同的选择模式?
- json - 如何更改从 Go 服务器返回数据而不存储数据的格式
- python - 如何使用 Python 和托管标识/SAS 凭据从 VM 访问 Azure 存储帐户
- postgresql - 如何通过检查列的时间来创建 CASE WHEN
- android - Ionic 3 cordova 遇到设备/模拟器错误 [Android]
- entity-framework - 未使用的表列需要在实体框架对象类中声明为属性?