terraform-provider-aws: Can't iterate over certificate_validation_records attributes of aws_apprunner_custom_domain_association resource
Community Note
- Please vote on this issue by adding a π reaction to the original issue to help the community and maintainers prioritize this request
- Please do not leave β+1β or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
- If you are interested in working on this issue or have submitted a pull request, please leave a comment
Terraform CLI and Terraform AWS Provider Version
on darwin_amd64
+ provider registry.terraform.io/hashicorp/aws v4.3.0
Affected Resource(s)
- aws_apprunner_custom_domain_association
Terraform Configuration Files
resource "aws_apprunner_custom_domain_association" "main" {
domain_name = "${local.domain_name}.${data.aws_route53_zone.main.name}"
service_arn = aws_apprunner_service.main.arn
enable_www_subdomain = true
}
resource "aws_route53_record" "main-www" {
name = local.domain_name
set_identifier = local.domain_name
type = "CNAME"
zone_id = data.aws_route53_zone.main.zone_id
ttl = 300
records = [aws_apprunner_service.main.service_url]
weighted_routing_policy {
weight = 90
}
}
resource "aws_route53_record" "main-cert" {
for_each = {
for entry in aws_apprunner_custom_domain_association.main.certificate_validation_records : entry.name => {
name = entry.name
record = entry.value
type = entry.type
}
}
allow_overwrite = true
zone_id = data.aws_route53_zone.main.zone_id
type = each.value.type
ttl = 300
name = each.key
records = [each.value.record]
}
Expected Behavior
The resource aws_route53_record.main-cert should be created properly. We should be able to iterate through aws_apprunner_custom_domain_association.main.certificate_validation_records dynamically.
Actual Behavior
β Error: Invalid for_each argument
β
β on modules/app_runner/main.tf line 94, in resource "aws_route53_record" "main-cert":
β 94: for_each = {
β 95: for entry in aws_apprunner_custom_domain_association.main.certificate_validation_records : entry.name => {
β 96: name = entry.name
β 97: record = entry.value
β 98: type = entry.type
β 99: }
β 100: }
β βββββββββββββββββ
β β aws_apprunner_custom_domain_association.main.certificate_validation_records is a set of object, known only after apply
β
β The "for_each" value depends on resource attributes that cannot be determined until apply, so Terraform cannot predict how many instances will be created. To work around this, use the -target argument to first apply only the resources that the
β for_each depends on.
Steps to Reproduce
terraform apply
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 39
- Comments: 20 (3 by maintainers)
Commits related to this issue
- :sparkles: AppRunner custom domain Note custom domain setup requires record validation. This got borrowed from: https://github.com/hashicorp/terraform-provider-aws/issues/23460#issuecomment-135635130... — committed to mynotif/mynotif-backend by AndreMiras 10 months ago
- :sparkles: AppRunner custom domain Note custom domain setup requires record validation. This got borrowed from: https://github.com/hashicorp/terraform-provider-aws/issues/23460#issuecomment-135635130... — committed to mynotif/mynotif-backend by AndreMiras 10 months ago
- :sparkles: AppRunner custom domain Note custom domain setup requires record validation. This got borrowed from: https://github.com/hashicorp/terraform-provider-aws/issues/23460#issuecomment-135635130... — committed to mynotif/mynotif-backend by AndreMiras 10 months ago
@justinretzolk The documentation does address this. However, I think most of us are boggled about why Terraform has trouble with this. Terraform does its own dependency mapping, and therefore should be able to run the
aws_route53_recordresource after theaws_apprunner_custom_domain_associationresource is created. After that resource is created, thecertificate_validation_recordsvalues are available for theaws_route53_recordresource to use.This is really not expected behavior. Due to dependency mapping, Terraform infers a promise that these values can be used in the same script. Just like how the
endpointfor anaws_db_instanceis available for use in the same configuration, so should thecertificate_validation_recordsmap be available for use within a script.As for solutions, for larger configurations, the
-targetworkaround is not even an option. For the other workaround:There are no examples of how to do this. Itβs a convoluted message and I personally canβt make sense of it (especially since the values are available at the time the script runs the resource blockβ¦again, due to dependency mapping). It would be great if in the documentation, there would be an example of how to define the map keys statically for use in the same config.
Is that something HashiCorp can provide?
I used the following code successfully @TechplexEngineer. As I understand it, the
tolist(...)(instead of direct array access) has Terraform create a runtime dependency as opposed to a statically known one - which is the default for array access, and obviously wonβt work because the DNS values arenβt known ahead of time - and allows validation to function correctly. This obviously wonβt work in case of multiple validation options, but honestly who has those πFull working example:
At the time of writing, AWS provides 3 certificate validation records. The above exmple works with 3 certificate validation records. It also creates the record to send traffic to the app via the custom domain.
The above example enables the following custom domain for the app:
custom-sub-domain.my-domain.com@anilmujagic
That will still result on the same error
The βcountβ value depends on resource attributes that cannot be determined β until apply, so Terraform cannot predict how many instances will be β created. To work around this, use the -target argument to first apply only β the resources that the count depends on
Hey @vmignot π Thank you for following up, and for pointing that out. Looking at the schema for these two resources, Iβd think that a similar
for_eachapproach could be used. Perhaps this is due to theaws_acm_certificateresource havingSet:defined. Iβm going to mark this as a bug so that we can look into it and determine whether this should be possible.I was able to reproduce this bug. In case it helps, I noticed that if you remove the
aws_route53_record.main-certresource then runapply, then re-add it, it works as expected.