现在,我需要创建多个托管区域,并将名称服务器记录传递回父帐户,但我不确定是否有可能(或如何)引用多个资源.读这篇文章可能没有多大意义,所以下面的代码是我所掌握的.
Now I need to create multiple hosted zones and pass the nameserver records back to the parent account and I am not sure if its possible (or how if it is) to reference multiple resources. Reading this probably doesn't make a whole lot of sense, so the code below is as far as I have got.
我现在有一个 for_each
循环,该循环将遍历字符串列表并为每个字符串创建一个托管区域,然后我想在另一个帐户中创建相应的NS记录,请注意,我是使用单独的提供程序 provider = aws.management_account
连接到管理帐户,这对于单个托管区域可以正常工作.
I now have a for_each
loop which will loop over a list of strings and create a hosted zone for each string, and I want to then create corresponding NS records in another account, notice that I am using a separate provider provider = aws.management_account
to connect to the management account and this works fine for a single hosted zone.
我不知道如何引用托管区域,是否有某些语法或者我的方法不正确?
I do not know how to reference the hosted zones, is there some syntax for this or is my approach wrong?
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"的映射.
Since your local.aws_zones
is set ["dev", "test", "qa"], your aws_route53_zone.public_hosted_zone
will be a map with keys "dev", "test", "qa".
因此,要在 aws_route53_record
中使用它,可以尝试:
Therefore, to use it in your aws_route53_record
, you can try:
resource "aws_route53_record" "ns_records" {
for_each = local.aws_zones
# other attributes
records = aws_route53_zone.public_hosted_zone[each.key].name_servers
}
这篇关于Terraform:在另一个资源中引用在for_each中创建的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!