-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.tf
104 lines (81 loc) · 2.45 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
locals {
all_domains = concat([var.domain_name], var.domain_aliases)
origin_id = "redirect-${var.domain_name}"
}
module "cert" {
source = "commitdev/zero/aws//modules/certificate"
version = "0.1.0"
count = var.use_existing_cert ? 0 : 1
zone_name = var.zone_name
domain_name = var.domain_name
alternative_names = var.domain_aliases
}
resource "aws_s3_bucket" "redirect" {
// Our bucket's name is going to be the same as our site's domain name.
bucket = var.domain_name
acl = "private"
website {
redirect_all_requests_to = var.redirect_target_url
}
tags = var.tags
}
# Create the cloudfront distribution - this is just to add HTTPS which is not supported by S3
resource "aws_cloudfront_distribution" "cdn" {
origin {
domain_name = aws_s3_bucket.redirect.website_endpoint
origin_id = local.origin_id
custom_origin_config {
http_port = 80
https_port = 443
origin_protocol_policy = "http-only"
origin_ssl_protocols = ["TLSv1", "TLSv1.1", "TLSv1.2"]
}
}
enabled = true
is_ipv6_enabled = true
default_cache_behavior {
target_origin_id = local.origin_id
viewer_protocol_policy = "redirect-to-https"
compress = false
allowed_methods = ["GET", "HEAD"]
cached_methods = ["GET", "HEAD"]
min_ttl = 0
default_ttl = 86400
max_ttl = 31536000
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
}
aliases = local.all_domains
restrictions {
geo_restriction {
restriction_type = "none"
}
}
# Use our cert
viewer_certificate {
acm_certificate_arn = var.existing_cert_arn == "" ? module.cert[0].certificate_arn : var.existing_cert_arn
minimum_protocol_version = "TLSv1"
ssl_support_method = "sni-only"
}
tags = var.tags
# depends_on = [module.cert[0].certificate_validation]
}
data "aws_route53_zone" "zone" {
name = var.zone_name
}
# Domains to point at CF
resource "aws_route53_record" "domains" {
count = length(local.all_domains)
zone_id = data.aws_route53_zone.zone.zone_id
name = local.all_domains[count.index]
type = "A"
alias {
name = aws_cloudfront_distribution.cdn.domain_name
zone_id = aws_cloudfront_distribution.cdn.hosted_zone_id
evaluate_target_health = false
}
}