I am trying to create multiple unique aws_acm_certificates using Terraform for_each,I created the acm certificates as modules for each of the unique certificate.
I am having a challenge outputing the certificates created, not sure of how to output for each of the modules.
This is my code. Would appreciate any help on how to create
locals {
process_domain_validation_options = var.process_domain_validation_options && var.acm_validation_method == "DNS"
}
resource "aws_acm_certificate" "cert" {
for_each = var.acm_certificate
domain_name = each.key.domain_name
subject_alternative_names = each.key.subject_alternative_names
validation_method = var.acm_validation_method
lifecycle {
create_before_destroy = true
}
tags = {
Name = "${var.tags}-var.environment"
}
}
data "aws_route53_zone" "default" {
count = local.process_domain_validation_options ? 1 : 0
zone_id = var.hosted_zone_id
name = try(length(var.hosted_zone_id), 0) == 0 ? var.domain_name : null
private_zone = var.route53_private_zone
}
resource "aws_route53_record" "cert_dns_validation" {
for_each = {
for dvo in aws_acm_certificate.cert.domain_validation_options : dvo.domain_name => {
name = dvo.resource_record_name
record = dvo.resource_record_value
type = dvo.resource_record_type
}
}
allow_overwrite = var.allow_validation_record_overwrite
zone_id = join("", data.aws_route53_zone.default.*.zone_id)
ttl = var.validation_record_ttl
name = each.value.name
type = each.value.type
records = [each.value.record]
}
resource "aws_acm_certificate_validation" "default" {
count = local.process_domain_validation_options && var.wait_for_certificate_issued ? 1 : 0
certificate_arn = aws_acm_certificate.cert.arn
validation_record_fqdns = [for record in aws_route53_record.cert_dns_validation : record.fqdn]
}
variable "acm_certificate" {
type = map(object({
domain_name = string
subject_alternative_names = string
}))
default = {
"key" = {
domain_name = "value"
subject_alternative_names = "value"
}
}
}
I'm not sure of a better way to do it.