Terraform:创建资源时替换函数在 for_each 内部不起作用
Terraform: Replace function not working inside for_each while creating a resource
在使用 for_each 创建资源之一时,我还从其他资源获取信息以将数据填充到资源创建中的属性之一。但是我需要替换 each.value 部分以正确匹配并从其他资源获得响应,但奇怪的是,替换功能似乎没有完成它的工作。任何帮助深表感谢。谢谢。
代码示例如下。创建 route 53 记录时,尝试从 cloud_front 资源获取信息。
resource "aws_route53_record" "redirect-records" {
for_each = var.redirects
zone_id = data.aws_route53_zone.main[each.value.domain_tf_alias].zone_id
name = each.value.source
type = "A"
alias {
name = **aws_cloudfront_distribution.main[replace(each.value.source,".","-")].domain_name**
zone_id = **aws_cloudfront_distribution.main[replace(each.value.source,".","-")].hosted_zone_id**
evaluate_target_health = false
}
}
预期:我需要 each.value.source say example from www.example.com 替换为 www-example-com,但这并没有发生.
Error: Invalid index
on rules.tf line 25, in resource "aws_route53_record" "redirect-records":
25: name = aws_cloudfront_distribution.main[replace(each.value.source,".","-")].domain_name # If acm domain exists then get from cloudFront else from S3
|----------------
| aws_cloudfront_distribution.main is object with 4 attributes
| each.value.source is "www.aclgrc-s3.com"
The given key does not identify an element in this collection value.
基于聊天讨论。
问题不在于 replace
函数。该功能完全符合预期。
此问题是由 var.redirects
和 aws_cloudfront_distribution.main
中的键 不匹配引起的。随后,解决方案是确保两个数据集具有匹配的键。
在使用 for_each 创建资源之一时,我还从其他资源获取信息以将数据填充到资源创建中的属性之一。但是我需要替换 each.value 部分以正确匹配并从其他资源获得响应,但奇怪的是,替换功能似乎没有完成它的工作。任何帮助深表感谢。谢谢。
代码示例如下。创建 route 53 记录时,尝试从 cloud_front 资源获取信息。
resource "aws_route53_record" "redirect-records" {
for_each = var.redirects
zone_id = data.aws_route53_zone.main[each.value.domain_tf_alias].zone_id
name = each.value.source
type = "A"
alias {
name = **aws_cloudfront_distribution.main[replace(each.value.source,".","-")].domain_name**
zone_id = **aws_cloudfront_distribution.main[replace(each.value.source,".","-")].hosted_zone_id**
evaluate_target_health = false
}
}
预期:我需要 each.value.source say example from www.example.com 替换为 www-example-com,但这并没有发生.
Error: Invalid index
on rules.tf line 25, in resource "aws_route53_record" "redirect-records":
25: name = aws_cloudfront_distribution.main[replace(each.value.source,".","-")].domain_name # If acm domain exists then get from cloudFront else from S3
|----------------
| aws_cloudfront_distribution.main is object with 4 attributes
| each.value.source is "www.aclgrc-s3.com"
The given key does not identify an element in this collection value.
基于聊天讨论。
问题不在于 replace
函数。该功能完全符合预期。
此问题是由 var.redirects
和 aws_cloudfront_distribution.main
中的键 不匹配引起的。随后,解决方案是确保两个数据集具有匹配的键。