Skip to content

Commit

Permalink
feat: Add support for custom domain on OVH
Browse files Browse the repository at this point in the history
remove comment
  • Loading branch information
haidaraM committed Feb 18, 2024
1 parent c4b4c36 commit 837c949
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
7 changes: 5 additions & 2 deletions frontend.tf
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ resource "aws_cloudfront_distribution" "website" {
comment = "cloudfront distribution for devops challenge"
price_class = var.cloudfront_price_class
default_root_object = "index.html"
aliases = [local.frontend_fqdn]

# As it's an SPA, we let the SPA handle access to files not found in the bucket
custom_error_response {
Expand Down Expand Up @@ -142,8 +143,10 @@ resource "aws_cloudfront_distribution" "website" {
}

viewer_certificate {
# Because we don't use a custom domain with certificate
cloudfront_default_certificate = true
cloudfront_default_certificate = false
acm_certificate_arn = aws_acm_certificate.cf_certificate.arn
minimum_protocol_version = "TLSv1.2_2021"
ssl_support_method = "sni-only"
}

logging_config {
Expand Down
13 changes: 13 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ terraform {
source = "hashicorp/aws"
version = "~> 5"
}

ovh = {
source = "ovh/ovh"
version = "~> 0.37"
}

archive = {
source = "hashicorp/archive"
version = "~> 2"
Expand All @@ -21,4 +27,11 @@ provider "aws" {
}
}

provider "aws" {
alias = "cloudfront-us-east-1"
region = "us-east-1"
}

provider "ovh" {
endpoint = "ovh-eu"
}
39 changes: 39 additions & 0 deletions r53-acm.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
locals {
ovh_domain_name = "haidara.io"
frontend_sub_domain = "demo-cloud-facile-${var.env}"
frontend_fqdn = "${local.frontend_sub_domain}.${local.ovh_domain_name}"
}


resource "aws_acm_certificate" "cf_certificate" {
provider = aws.cloudfront-us-east-1
domain_name = local.frontend_fqdn
validation_method = "DNS"
tags = merge({ Name = local.frontend_fqdn })

lifecycle {
create_before_destroy = true
}
}

resource "ovh_domain_zone_record" "cf_record" {
fieldtype = "CNAME"
subdomain = local.frontend_sub_domain
target = "${aws_cloudfront_distribution.website.domain_name}."
zone = local.ovh_domain_name
ttl = 60
}

resource "ovh_domain_zone_record" "cert_validation_record" {
fieldtype = "CNAME"
subdomain = replace(tolist(aws_acm_certificate.cf_certificate.domain_validation_options)[0].resource_record_name, ".${local.ovh_domain_name}.", "")
target = tolist(aws_acm_certificate.cf_certificate.domain_validation_options)[0].resource_record_value
zone = local.ovh_domain_name
ttl = 60
}

resource "aws_acm_certificate_validation" "validation" {
provider = aws.cloudfront-us-east-1
certificate_arn = aws_acm_certificate.cf_certificate.arn
validation_record_fqdns = ["${ovh_domain_zone_record.cert_validation_record.subdomain}.${ovh_domain_zone_record.cert_validation_record.zone}"]
}

0 comments on commit 837c949

Please sign in to comment.