|
1 | 1 |
|
| 2 | +//////////////////////////////////////////////// |
| 3 | +// The HTTPS certificate for the main website |
| 4 | + |
2 | 5 | resource "aws_acm_certificate" "https_certificate_for_our_domain" {
|
3 | 6 | // This certificate is for use by CloudFront, so it has to be created in the us-east-1 region (for some reason!)
|
4 | 7 | provider = aws.us-east-1
|
@@ -31,3 +34,54 @@ resource "aws_acm_certificate_validation" "certificate_validation_waiter" {
|
31 | 34 | certificate_arn = aws_acm_certificate.https_certificate_for_our_domain.arn
|
32 | 35 | validation_record_fqdns = [for record in aws_route53_record.example : record.fqdn]
|
33 | 36 | }
|
| 37 | + |
| 38 | + |
| 39 | +/////////////////////////////////////////////////////// |
| 40 | +// The HTTPS certificate for the www domain redirect |
| 41 | + |
| 42 | +resource "aws_acm_certificate" "https_certificate__www_domain_redirect" { |
| 43 | + count = (var.create_redirect_from_www_domain) ? 1 : 0 // Only create this HTTPS Certificate if "var.create_redirect_from_www_domain" is true |
| 44 | + |
| 45 | + // This certificate is for use by CloudFront, so it has to be created in the us-east-1 region (for some reason!) |
| 46 | + provider = aws.us-east-1 |
| 47 | + |
| 48 | + domain_name = "${var.dns_record_www_domain_including_dot}${data.aws_route53_zone.route_53_zone_for_our_domain.name}" |
| 49 | + validation_method = "DNS" |
| 50 | +} |
| 51 | + |
| 52 | +locals { |
| 53 | + dns_records_we_need_to_verify_www_domain__list = flatten([ |
| 54 | + for https_certificate in aws_acm_certificate.https_certificate__www_domain_redirect : [ |
| 55 | + for dvo in https_certificate.domain_validation_options : { |
| 56 | + domain_name = dvo.domain_name |
| 57 | + name = dvo.resource_record_name |
| 58 | + record = dvo.resource_record_value |
| 59 | + type = dvo.resource_record_type |
| 60 | + } |
| 61 | + ] |
| 62 | + ]) |
| 63 | + dns_records_we_need_to_verify_www_domain__map = { |
| 64 | + for i, record in local.dns_records_we_need_to_verify_www_domain__list: record.domain_name => record |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +resource "aws_route53_record" "dns_records_for_https_certificate_verification__www_domain_redirect" { |
| 69 | + for_each = local.dns_records_we_need_to_verify_www_domain__map |
| 70 | + |
| 71 | + allow_overwrite = true |
| 72 | + name = each.value.name |
| 73 | + records = [each.value.record] |
| 74 | + ttl = 60 |
| 75 | + type = each.value.type |
| 76 | + zone_id = data.aws_route53_zone.route_53_zone_for_our_domain.zone_id |
| 77 | +} |
| 78 | + |
| 79 | +resource "aws_acm_certificate_validation" "certificate_validation_waiter__www_domain_redirect" { |
| 80 | + count = (var.create_redirect_from_www_domain) ? 1 : 0 // Only create this HTTPS Certificate if "var.create_redirect_from_www_domain" is true |
| 81 | + |
| 82 | + // This certificate is for use by CloudFront, so it has to be created in the us-east-1 region (for some reason!) |
| 83 | + provider = aws.us-east-1 |
| 84 | + |
| 85 | + certificate_arn = aws_acm_certificate.https_certificate__www_domain_redirect[0].arn |
| 86 | + validation_record_fqdns = [for record in aws_route53_record.dns_records_for_https_certificate_verification__www_domain_redirect : record.fqdn] |
| 87 | +} |
0 commit comments