File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -56,6 +56,62 @@ resource "aws_cloudfront_distribution" "cf_distribution" {
5656 }
5757}
5858
59+ resource "aws_cloudfront_distribution" "redirect_www" {
60+ origin {
61+ domain_name = aws_s3_bucket. redirect_www . website_endpoint
62+ origin_id = " redirect-origin"
63+ custom_origin_config {
64+ http_port = 80
65+ https_port = 443
66+ origin_protocol_policy = " http-only"
67+ origin_ssl_protocols = [" TLSv1.2" ]
68+ }
69+ }
70+
71+ enabled = true
72+ is_ipv6_enabled = true
73+ comment = " Redirect www.casteels.dev to casteels.dev"
74+ default_root_object = " index.html"
75+
76+ aliases = [
77+ " www.casteels.dev"
78+ ]
79+
80+ default_cache_behavior {
81+ allowed_methods = [" GET" , " HEAD" ]
82+ cached_methods = [" GET" , " HEAD" ]
83+ target_origin_id = " redirect-origin"
84+
85+ forwarded_values {
86+ query_string = false
87+
88+ cookies {
89+ forward = " none"
90+ }
91+ }
92+
93+ viewer_protocol_policy = " redirect-to-https"
94+ }
95+
96+ price_class = " PriceClass_100"
97+
98+ viewer_certificate {
99+ acm_certificate_arn = aws_acm_certificate_validation. blog_cert . certificate_arn
100+ ssl_support_method = " sni-only"
101+ minimum_protocol_version = " TLSv1.2_2021"
102+ }
103+
104+ restrictions {
105+ geo_restriction {
106+ restriction_type = " none"
107+ }
108+ }
109+
110+ tags = {
111+ Environment = " production"
112+ }
113+ }
114+
59115resource "aws_cloudfront_cache_policy" "website_cache_policy" {
60116 name = " website-cache-policy"
61117 default_ttl = 50
Original file line number Diff line number Diff line change @@ -30,6 +30,7 @@ provider "aws" {
3030resource "aws_acm_certificate" "blog_cert" {
3131 provider = aws. us-east-1
3232 domain_name = " casteels.dev"
33+ subject_alternative_names = [" www.casteels.dev" ]
3334 validation_method = " DNS"
3435 lifecycle {
3536 create_before_destroy = true
@@ -70,3 +71,16 @@ resource "aws_route53_record" "blog" {
7071 evaluate_target_health = false
7172 }
7273}
74+
75+ # Route 53 DNS Record
76+ resource "aws_route53_record" "www" {
77+ zone_id = " Z003579951UE5LAOF83P"
78+ name = " www.casteels.dev"
79+ type = " A"
80+
81+ alias {
82+ name = aws_cloudfront_distribution. cf_distribution . domain_name
83+ zone_id = aws_cloudfront_distribution. cf_distribution . hosted_zone_id
84+ evaluate_target_health = false
85+ }
86+ }
Original file line number Diff line number Diff line change @@ -26,3 +26,16 @@ resource "aws_s3_bucket_policy" "blog" {
2626 }]
2727 })
2828}
29+
30+ resource "aws_s3_bucket" "redirect_www" {
31+ bucket = " www.casteels.dev"
32+ }
33+
34+ resource "aws_s3_bucket_website_configuration" "redirect_www" {
35+ bucket = aws_s3_bucket. redirect_www . id
36+
37+ redirect_all_requests_to {
38+ host_name = " casteels.dev"
39+ protocol = " https"
40+ }
41+ }
You can’t perform that action at this time.
0 commit comments