Skip to content

Commit 39f8d9b

Browse files
authored
Merge pull request #183 from rtfpessoa/update-terraform
Update terraform
2 parents 1906abd + c76a421 commit 39f8d9b

File tree

1 file changed

+92
-25
lines changed

1 file changed

+92
-25
lines changed

terraform/main.tf

Lines changed: 92 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,26 @@ provider "aws" {
66
}
77

88
provider "aws" {
9-
alias = "nvirginia"
9+
alias = "nvirginia"
1010
profile = "${var.aws_profile}"
11-
region = "us-east-1"
11+
region = "us-east-1"
1212
}
1313

1414
terraform {
1515
backend "s3" {
16-
region = "us-east-1"
17-
encrypt = true
18-
bucket = "terraform-state-bucket.rtfpessoa.xyz"
16+
region = "us-east-1"
17+
encrypt = true
18+
bucket = "terraform-state-bucket.rtfpessoa.xyz"
1919
dynamodb_table = "terraform-state-table"
20-
key = "diff2html.xyz"
20+
key = "diff2html.xyz"
2121
}
2222
}
2323

2424
resource "aws_acm_certificate" "cert" {
25-
provider = "aws.nvirginia"
26-
domain_name = "${var.domain}"
25+
provider = "aws.nvirginia"
26+
domain_name = "${var.domain}"
2727
subject_alternative_names = ["*.${var.domain}"]
28-
validation_method = "DNS"
28+
validation_method = "DNS"
2929

3030
lifecycle {
3131
create_before_destroy = true
@@ -34,24 +34,24 @@ resource "aws_acm_certificate" "cert" {
3434

3535
resource "aws_route53_record" "root_domain" {
3636
zone_id = "${var.hosted_zone_id}"
37-
name = "${var.domain}"
38-
type = "A"
37+
name = "${var.domain}"
38+
type = "A"
3939

4040
alias {
41-
name = "${aws_cloudfront_distribution.cdn.domain_name}"
42-
zone_id = "${aws_cloudfront_distribution.cdn.hosted_zone_id}"
41+
name = "${aws_cloudfront_distribution.cdn.domain_name}"
42+
zone_id = "${aws_cloudfront_distribution.cdn.hosted_zone_id}"
4343
evaluate_target_health = false
4444
}
4545
}
4646

4747
resource "aws_route53_record" "www_domain" {
4848
zone_id = "${var.hosted_zone_id}"
49-
name = "www.${var.domain}"
50-
type = "A"
49+
name = "${local.www_domain}"
50+
type = "A"
5151

5252
alias {
53-
name = "${aws_cloudfront_distribution.cdn.domain_name}"
54-
zone_id = "${aws_cloudfront_distribution.cdn.hosted_zone_id}"
53+
name = "${aws_cloudfront_distribution.www_cdn.domain_name}"
54+
zone_id = "${aws_cloudfront_distribution.www_cdn.hosted_zone_id}"
5555
evaluate_target_health = false
5656
}
5757
}
@@ -60,13 +60,13 @@ resource "aws_route53_record" "cert_validation" {
6060
zone_id = "${var.hosted_zone_id}"
6161
name = "${aws_acm_certificate.cert.domain_validation_options.0.resource_record_name}"
6262
type = "${aws_acm_certificate.cert.domain_validation_options.0.resource_record_type}"
63-
63+
6464
records = ["${aws_acm_certificate.cert.domain_validation_options.0.resource_record_value}"]
6565
ttl = 60
6666
}
6767

6868
resource "aws_acm_certificate_validation" "cert" {
69-
provider = "aws.nvirginia"
69+
provider = "aws.nvirginia"
7070
certificate_arn = "${aws_acm_certificate.cert.arn}"
7171
validation_record_fqdns = ["${aws_route53_record.cert_validation.fqdn}"]
7272
}
@@ -75,6 +75,12 @@ resource "aws_cloudfront_origin_access_identity" "origin_access_identity" {
7575
comment = "${var.domain} origin access identity"
7676
}
7777

78+
locals {
79+
s3_origin_id = "S3-${var.domain}"
80+
s3_www_origin_id = "S3-www-${var.domain}"
81+
www_domain = "www.${var.domain}"
82+
}
83+
7884
resource "aws_s3_bucket" "site" {
7985
bucket = "${var.domain}"
8086
acl = "private"
@@ -90,11 +96,7 @@ resource "aws_s3_bucket" "site" {
9096
"Resource": "arn:aws:s3:::${var.domain}/*"
9197
}]
9298
}
93-
EOF
94-
}
95-
96-
locals {
97-
s3_origin_id = "S3-${var.domain}"
99+
EOF
98100
}
99101

100102
resource "aws_cloudfront_distribution" "cdn" {
@@ -108,7 +110,7 @@ resource "aws_cloudfront_distribution" "cdn" {
108110
}
109111

110112
# If using route53 aliases for DNS we need to declare it here too, otherwise we'll get 403s.
111-
aliases = ["${var.domain}", "www.${var.domain}"]
113+
aliases = ["${var.domain}"]
112114

113115
enabled = true
114116
is_ipv6_enabled = true
@@ -121,6 +123,71 @@ resource "aws_cloudfront_distribution" "cdn" {
121123

122124
forwarded_values {
123125
query_string = true
126+
127+
cookies {
128+
forward = "none"
129+
}
130+
}
131+
132+
min_ttl = 0
133+
default_ttl = 86400
134+
max_ttl = 31536000
135+
compress = true
136+
viewer_protocol_policy = "redirect-to-https"
137+
}
138+
139+
price_class = "PriceClass_All"
140+
141+
restrictions {
142+
geo_restriction {
143+
restriction_type = "none"
144+
locations = []
145+
}
146+
}
147+
148+
viewer_certificate {
149+
acm_certificate_arn = "${aws_acm_certificate_validation.cert.certificate_arn}"
150+
minimum_protocol_version = "TLSv1.1_2016"
151+
ssl_support_method = "sni-only"
152+
}
153+
}
154+
155+
resource "aws_s3_bucket" "www_site" {
156+
bucket = "${local.www_domain}"
157+
acl = "public-read"
158+
159+
website {
160+
redirect_all_requests_to = "https://${var.domain}"
161+
}
162+
}
163+
164+
resource "aws_cloudfront_distribution" "www_cdn" {
165+
origin {
166+
origin_id = "${local.s3_www_origin_id}"
167+
domain_name = "${aws_s3_bucket.www_site.website_endpoint}"
168+
169+
custom_origin_config {
170+
http_port = 80
171+
https_port = 443
172+
origin_protocol_policy = "http-only"
173+
origin_ssl_protocols = ["TLSv1.1", "TLSv1.2"]
174+
}
175+
}
176+
177+
# If using route53 aliases for DNS we need to declare it here too, otherwise we'll get 403s.
178+
aliases = ["${local.www_domain}"]
179+
180+
enabled = true
181+
is_ipv6_enabled = true
182+
183+
default_cache_behavior {
184+
allowed_methods = ["GET", "HEAD", "OPTIONS"]
185+
cached_methods = ["GET", "HEAD"]
186+
target_origin_id = "${local.s3_www_origin_id}"
187+
188+
forwarded_values {
189+
query_string = true
190+
124191
cookies {
125192
forward = "none"
126193
}

0 commit comments

Comments
 (0)