Skip to content

Commit 1906abd

Browse files
authored
Merge pull request #181 from rtfpessoa/terraform
Initial terraform setup
2 parents 1cdfda0 + 7d41bd6 commit 1906abd

File tree

5 files changed

+180
-1
lines changed

5 files changed

+180
-1
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ jobs:
8888
- run:
8989
name: Deploy
9090
working_directory: ~/diff2html/docs
91-
command: aws s3 sync . s3://diff2html.rtfpessoa.xyz --region eu-west-1
91+
command: aws s3 sync . s3://diff2html.xyz --region eu-west-1
9292

9393
workflows:
9494
version: 2

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,6 @@ coverage/
2525

2626
# Bower
2727
bower_components/
28+
29+
# Terraform
30+
/terraform/.terraform

terraform/main.tf

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
# Inspired by https://gist.github.com/danihodovic/a51eb0d9d4b29649c2d094f4251827dd
2+
3+
provider "aws" {
4+
profile = "${var.aws_profile}"
5+
region = "${var.aws_region}"
6+
}
7+
8+
provider "aws" {
9+
alias = "nvirginia"
10+
profile = "${var.aws_profile}"
11+
region = "us-east-1"
12+
}
13+
14+
terraform {
15+
backend "s3" {
16+
region = "us-east-1"
17+
encrypt = true
18+
bucket = "terraform-state-bucket.rtfpessoa.xyz"
19+
dynamodb_table = "terraform-state-table"
20+
key = "diff2html.xyz"
21+
}
22+
}
23+
24+
resource "aws_acm_certificate" "cert" {
25+
provider = "aws.nvirginia"
26+
domain_name = "${var.domain}"
27+
subject_alternative_names = ["*.${var.domain}"]
28+
validation_method = "DNS"
29+
30+
lifecycle {
31+
create_before_destroy = true
32+
}
33+
}
34+
35+
resource "aws_route53_record" "root_domain" {
36+
zone_id = "${var.hosted_zone_id}"
37+
name = "${var.domain}"
38+
type = "A"
39+
40+
alias {
41+
name = "${aws_cloudfront_distribution.cdn.domain_name}"
42+
zone_id = "${aws_cloudfront_distribution.cdn.hosted_zone_id}"
43+
evaluate_target_health = false
44+
}
45+
}
46+
47+
resource "aws_route53_record" "www_domain" {
48+
zone_id = "${var.hosted_zone_id}"
49+
name = "www.${var.domain}"
50+
type = "A"
51+
52+
alias {
53+
name = "${aws_cloudfront_distribution.cdn.domain_name}"
54+
zone_id = "${aws_cloudfront_distribution.cdn.hosted_zone_id}"
55+
evaluate_target_health = false
56+
}
57+
}
58+
59+
resource "aws_route53_record" "cert_validation" {
60+
zone_id = "${var.hosted_zone_id}"
61+
name = "${aws_acm_certificate.cert.domain_validation_options.0.resource_record_name}"
62+
type = "${aws_acm_certificate.cert.domain_validation_options.0.resource_record_type}"
63+
64+
records = ["${aws_acm_certificate.cert.domain_validation_options.0.resource_record_value}"]
65+
ttl = 60
66+
}
67+
68+
resource "aws_acm_certificate_validation" "cert" {
69+
provider = "aws.nvirginia"
70+
certificate_arn = "${aws_acm_certificate.cert.arn}"
71+
validation_record_fqdns = ["${aws_route53_record.cert_validation.fqdn}"]
72+
}
73+
74+
resource "aws_cloudfront_origin_access_identity" "origin_access_identity" {
75+
comment = "${var.domain} origin access identity"
76+
}
77+
78+
resource "aws_s3_bucket" "site" {
79+
bucket = "${var.domain}"
80+
acl = "private"
81+
82+
policy = <<EOF
83+
{
84+
"Version": "2008-10-17",
85+
"Statement": [{
86+
"Sid": "AllowCloudFrontRead",
87+
"Effect": "Allow",
88+
"Principal": { "AWS": "${aws_cloudfront_origin_access_identity.origin_access_identity.iam_arn}" },
89+
"Action": "s3:GetObject",
90+
"Resource": "arn:aws:s3:::${var.domain}/*"
91+
}]
92+
}
93+
EOF
94+
}
95+
96+
locals {
97+
s3_origin_id = "S3-${var.domain}"
98+
}
99+
100+
resource "aws_cloudfront_distribution" "cdn" {
101+
origin {
102+
domain_name = "${aws_s3_bucket.site.bucket_regional_domain_name}"
103+
origin_id = "${local.s3_origin_id}"
104+
105+
s3_origin_config {
106+
origin_access_identity = "${aws_cloudfront_origin_access_identity.origin_access_identity.cloudfront_access_identity_path}"
107+
}
108+
}
109+
110+
# 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}"]
112+
113+
enabled = true
114+
is_ipv6_enabled = true
115+
default_root_object = "index.html"
116+
117+
default_cache_behavior {
118+
allowed_methods = ["GET", "HEAD", "OPTIONS"]
119+
cached_methods = ["GET", "HEAD"]
120+
target_origin_id = "${local.s3_origin_id}"
121+
122+
forwarded_values {
123+
query_string = true
124+
cookies {
125+
forward = "none"
126+
}
127+
}
128+
129+
min_ttl = 0
130+
default_ttl = 86400
131+
max_ttl = 31536000
132+
compress = true
133+
viewer_protocol_policy = "redirect-to-https"
134+
}
135+
136+
price_class = "PriceClass_All"
137+
138+
restrictions {
139+
geo_restriction {
140+
restriction_type = "none"
141+
locations = []
142+
}
143+
}
144+
145+
viewer_certificate {
146+
acm_certificate_arn = "${aws_acm_certificate_validation.cert.certificate_arn}"
147+
minimum_protocol_version = "TLSv1.1_2016"
148+
ssl_support_method = "sni-only"
149+
}
150+
}

terraform/outputs.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
output "route53_domain" {
2+
value = "${aws_route53_record.root_domain.fqdn}"
3+
}
4+
5+
output "cdn_domain" {
6+
value = "${aws_cloudfront_distribution.cdn.domain_name}"
7+
}

terraform/variables.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
variable "aws_region" {
2+
description = "The aws region to deploy"
3+
default = "eu-west-1"
4+
}
5+
6+
variable "aws_profile" {
7+
description = "The aws profile to use"
8+
default = "personal"
9+
}
10+
11+
variable "domain" {
12+
description = "The domain to deploy this page"
13+
default = "diff2html.xyz"
14+
}
15+
16+
variable "hosted_zone_id" {
17+
description = "The hosted zone id where the domain will be created"
18+
default = "Z2T76N7UKY0XQI"
19+
}

0 commit comments

Comments
 (0)