I try to update from terraform 0.12 to 0.13 and the aws provider from version 2 to 3. Now I struggle with accessing attributes on empty lists. Previously that was working, now I get errors.
If var.env
now is not "production"
, I get this error:
15: for dvo in aws_acm_certificate.cert.0.domain_validation_options : dvo.domain_name => {
|----------------
| aws_acm_certificate.cert is empty tuple
The given key does not identify an element in this collection value.
Previously, I used count
for the aws_route53_record
, too, but since I’m supposed to use for_each
, which can’t be used with count
.
Here the resources
resource "aws_acm_certificate" "cert" {
domain_name = "*.example.com"
subject_alternative_names = ["example.com"]
validation_method = "DNS"
lifecycle {
create_before_destroy = true
}
count = var.env == "production" ? 1 : 0
}
resource "aws_route53_record" "aws_3" {
for_each = {
for dvo in aws_acm_certificate.cert.0.domain_validation_options : dvo.domain_name => {
name = dvo.resource_record_name
record = dvo.resource_record_value
type = dvo.resource_record_type
}
}
zone_id = data.aws_route53_zone.example_com.zone_id
ttl = 300
name = each.value.name
records = [each.value.record]
type = each.value.type
}
# resource "aws_route53_record" "aws_2" {
# name = aws_acm_certificate.cert.0.domain_validation_options[0].resource_record_name
# type = aws_acm_certificate.cert.0.domain_validation_options[0].resource_record_type
# zone_id = data.aws_route53_zone.example_com.zone_id
# records = [aws_acm_certificate.cert.0.domain_validation_options[0].resource_record_value]
# ttl = 300
#
# count = var.env == "production" ? 1 : 0
# }
How can I fix this so it’s working even if var.env
is not "production"
?
Thank you