resource "aws_acm_certificate" "cert" {
  domain_name       = "vault.${var.dns_info["private"]["zone"]}"
  validation_method = "DNS"

  tags = merge(var.standard_tags, var.tags)
}

resource "aws_acm_certificate_validation" "cert" {
  certificate_arn         = aws_acm_certificate.cert.arn
  validation_record_fqdns = [for record in aws_route53_record.cert_validation: record.fqdn]
}

resource "aws_route53_record" "cert_validation" {
  provider = aws.mdr-common-services-commercial

  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 = true
  name            = each.value.name
  records         = [each.value.record]
  ttl             = 60
  type            = each.value.type
  zone_id         = var.dns_info["public"]["zone_id"]
}