Skip to content

Commit d1235f2

Browse files
authored
Merge pull request #21 from chris-qa-org/support-creation-of-cloudfront-tls-certificates-and-route53-records
Support creation of CloudFront TLS certificates and Route53 records
2 parents 02cc39b + 1d1bed8 commit d1235f2

File tree

7 files changed

+94
-2
lines changed

7 files changed

+94
-2
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Terraform module to host a static site generated by Publii
2424

2525
| Name | Type |
2626
|------|------|
27+
| [aws_acm_certificate.cloudfront_frontend](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/acm_certificate) | resource |
2728
| [aws_cloudfront_distribution.frontend](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_distribution) | resource |
2829
| [aws_cloudfront_distribution.frontend_www_redirect](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_distribution) | resource |
2930
| [aws_cloudfront_function.frontend_viewer_request](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_function) | resource |
@@ -36,6 +37,9 @@ Terraform module to host a static site generated by Publii
3637
| [aws_kms_key.s3_bucket_frontend](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) | resource |
3738
| [aws_kms_key.s3_bucket_frontend_www_redirect](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) | resource |
3839
| [aws_lambda_permission.cloudfront_invalidation_frontend_alllow_s3](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_permission) | resource |
40+
| [aws_route53_record.cloudfront_frontend_tls_certificate_dns_validation](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_record) | resource |
41+
| [aws_route53_record.frontend](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_record) | resource |
42+
| [aws_route53_record.frontend_www_redirect](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_record) | resource |
3943
| [aws_s3_bucket.frontend](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket) | resource |
4044
| [aws_s3_bucket.frontend_www_redirect](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket) | resource |
4145
| [aws_s3_bucket.logs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket) | resource |
@@ -61,6 +65,7 @@ Terraform module to host a static site generated by Publii
6165
| [aws_s3_bucket_website_configuration.frontend_www_redirect](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_website_configuration) | resource |
6266
| [aws_wafv2_web_acl.cloudfront_waf](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/wafv2_web_acl) | resource |
6367
| [random_id.project](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/id) | resource |
68+
| [aws_route53_zone.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route53_zone) | data source |
6469
| [template_file.cloudfront_frontend_viewer_request_function](https://registry.terraform.io/providers/hashicorp/template/latest/docs/data-sources/file) | data source |
6570
| [template_file.frontend_bucket_cloudfront_read](https://registry.terraform.io/providers/hashicorp/template/latest/docs/data-sources/file) | data source |
6671
| [template_file.frontend_bucket_enforce_tls_statement](https://registry.terraform.io/providers/hashicorp/template/latest/docs/data-sources/file) | data source |
@@ -80,8 +85,9 @@ Terraform module to host a static site generated by Publii
8085
| <a name="input_cloudfront_enable_apex_to_www_redirect"></a> [cloudfront\_enable\_apex\_to\_www\_redirect](#input\_cloudfront\_enable\_apex\_to\_www\_redirect) | Enable CloudFront apex to www redirect | `bool` | `true` | no |
8186
| <a name="input_cloudfront_enable_ipv6"></a> [cloudfront\_enable\_ipv6](#input\_cloudfront\_enable\_ipv6) | Enable IPv6 on CloudFront | `bool` | `true` | no |
8287
| <a name="input_cloudfront_enable_waf"></a> [cloudfront\_enable\_waf](#input\_cloudfront\_enable\_waf) | Enable CloudFront WAF | `bool` | `true` | no |
83-
| <a name="input_cloudfront_tls_certificate_arn"></a> [cloudfront\_tls\_certificate\_arn](#input\_cloudfront\_tls\_certificate\_arn) | CloudFront TLS certificate ARN (must be created in us-east-1 region) | `string` | n/a | yes |
88+
| <a name="input_cloudfront_tls_certificate_arn"></a> [cloudfront\_tls\_certificate\_arn](#input\_cloudfront\_tls\_certificate\_arn) | CloudFront TLS certificate ARN (must be created in us-east-1 region) | `string` | `""` | no |
8489
| <a name="input_enable_publii_pretty_urls"></a> [enable\_publii\_pretty\_urls](#input\_enable\_publii\_pretty\_urls) | If you hae enabled 'Pretty URLs' in Publii, set this to true | `bool` | `false` | no |
90+
| <a name="input_route53_hosted_zone_options"></a> [route53\_hosted\_zone\_options](#input\_route53\_hosted\_zone\_options) | If you have a Route53 zone, the required DNS records can be created automatically. | <pre>object({<br> id = string<br> create_certificate_dns_validation_records = bool<br> create_site_url_dns_records = bool<br> })</pre> | <pre>{<br> "create_certificate_dns_validation_records": false,<br> "create_site_url_dns_records": false,<br> "id": ""<br>}</pre> | no |
8591
| <a name="input_s3_bucket_acl"></a> [s3\_bucket\_acl](#input\_s3\_bucket\_acl) | S3 bucket ACL | `string` | `"private"` | no |
8692
| <a name="input_site_url"></a> [site\_url](#input\_site\_url) | The desired site URL | `string` | n/a | yes |
8793

certificates.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
resource "aws_acm_certificate" "cloudfront_frontend" {
2+
provider = aws.useast1
3+
4+
count = local.cloudfront_tls_certificate_arn == "" ? 1 : 0
5+
6+
domain_name = local.site_url
7+
subject_alternative_names = local.cloudfront_enable_apex_to_www_redirect ? [
8+
"www.${local.site_url}"
9+
] : []
10+
11+
validation_method = "DNS"
12+
13+
lifecycle {
14+
create_before_destroy = true
15+
}
16+
}
17+

cloudfront-frontend.tf

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ resource "aws_cloudfront_distribution" "frontend" {
6868
}
6969

7070
viewer_certificate {
71-
acm_certificate_arn = local.cloudfront_tls_certificate_arn
71+
acm_certificate_arn = local.cloudfront_tls_certificate_arn == "" ? aws_acm_certificate.cloudfront_frontend.0.arn : local.cloudfront_tls_certificate_arn
7272
minimum_protocol_version = "TLSv1.2_2021"
7373
ssl_support_method = "sni-only"
7474
}
@@ -82,4 +82,8 @@ resource "aws_cloudfront_distribution" "frontend" {
8282
bucket = aws_s3_bucket.logs.bucket_domain_name
8383
prefix = "cloudfront/frontend/"
8484
}
85+
86+
depends_on = [
87+
aws_route53_record.cloudfront_frontend_tls_certificate_dns_validation
88+
]
8589
}

data.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
data "aws_route53_zone" "default" {
2+
count = local.route53_hosted_zone_options.id != "" ? 1 : 0
3+
4+
zone_id = local.route53_hosted_zone_options.id
5+
}

locals.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ locals {
77
cloudfront_enable_waf = var.cloudfront_enable_waf
88
cloudfront_enable_apex_to_www_redirect = var.cloudfront_enable_apex_to_www_redirect
99
enable_publii_pretty_urls = var.enable_publii_pretty_urls
10+
route53_hosted_zone_options = var.route53_hosted_zone_options
1011
}

route53.tf

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
resource "aws_route53_record" "cloudfront_frontend_tls_certificate_dns_validation" {
2+
count = local.cloudfront_tls_certificate_arn == "" ? (
3+
local.route53_hosted_zone_options.create_certificate_dns_validation_records ? 1 : 0
4+
) : 0
5+
6+
zone_id = data.aws_route53_zone.default.0.zone_id
7+
name = tolist(aws_acm_certificate.cloudfront_frontend.0.domain_validation_options)[0].resource_record_name
8+
type = tolist(aws_acm_certificate.cloudfront_frontend.0.domain_validation_options)[0].resource_record_type
9+
ttl = "86400"
10+
11+
records = [
12+
tolist(aws_acm_certificate.cloudfront_frontend.0.domain_validation_options)[0].resource_record_value,
13+
]
14+
}
15+
16+
resource "aws_route53_record" "frontend" {
17+
count = local.route53_hosted_zone_options.create_site_url_dns_records ? 1 : 0
18+
19+
zone_id = data.aws_route53_zone.default.0.zone_id
20+
name = local.cloudfront_enable_apex_to_www_redirect ? "www.${local.site_url}" : local.site_url
21+
type = "A"
22+
23+
alias {
24+
name = aws_cloudfront_distribution.frontend.domain_name
25+
zone_id = aws_cloudfront_distribution.frontend.hosted_zone_id
26+
evaluate_target_health = true
27+
}
28+
}
29+
30+
resource "aws_route53_record" "frontend_www_redirect" {
31+
count = local.route53_hosted_zone_options.create_site_url_dns_records ? (
32+
local.cloudfront_enable_apex_to_www_redirect ? 1 : 0
33+
) : 0
34+
35+
zone_id = data.aws_route53_zone.default.0.zone_id
36+
name = local.site_url
37+
type = "A"
38+
39+
alias {
40+
name = aws_cloudfront_distribution.frontend_www_redirect.0.domain_name
41+
zone_id = aws_cloudfront_distribution.frontend_www_redirect.0.hosted_zone_id
42+
evaluate_target_health = true
43+
}
44+
}

variables.tf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ variable "s3_bucket_acl" {
1212
variable "cloudfront_tls_certificate_arn" {
1313
description = "CloudFront TLS certificate ARN (must be created in us-east-1 region)"
1414
type = string
15+
default = ""
1516
}
1617

1718
variable "cloudfront_enable_ipv6" {
@@ -37,3 +38,17 @@ variable "enable_publii_pretty_urls" {
3738
type = bool
3839
default = false
3940
}
41+
42+
variable "route53_hosted_zone_options" {
43+
description = "If you have a Route53 zone, the required DNS records can be created automatically."
44+
type = object({
45+
id = string
46+
create_certificate_dns_validation_records = bool
47+
create_site_url_dns_records = bool
48+
})
49+
default = {
50+
id = ""
51+
create_certificate_dns_validation_records = false
52+
create_site_url_dns_records = false
53+
}
54+
}

0 commit comments

Comments
 (0)