-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.tf
158 lines (130 loc) · 4.02 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
data "aws_caller_identity" "current" {}
locals {
policydomain = "mta-sts.${var.domain}"
policyhash = md5(format("%s%s%s", join("", var.mx), var.mode, var.max_age))
bucketname = "${data.aws_caller_identity.current.account_id}.${var.domain}"
}
provider "aws" {
alias = "useast1"
region = "us-east-1"
}
resource "aws_acm_certificate" "cert" {
domain_name = local.policydomain
validation_method = "DNS"
tags = var.tags
provider = aws.useast1
}
data "aws_route53_zone" "zone" {
zone_id = var.zone_id
}
resource "aws_route53_record" "cert_validation" {
name = tolist(aws_acm_certificate.cert.domain_validation_options)[0].resource_record_name
type = tolist(aws_acm_certificate.cert.domain_validation_options)[0].resource_record_type
zone_id = data.aws_route53_zone.zone.id
records = [tolist(aws_acm_certificate.cert.domain_validation_options)[0].resource_record_value]
ttl = 60
}
resource "aws_acm_certificate_validation" "cert" {
certificate_arn = aws_acm_certificate.cert.arn
validation_record_fqdns = [aws_route53_record.cert_validation.fqdn]
provider = aws.useast1
}
resource "aws_s3_bucket" "policybucket" {
bucket = local.bucketname
acl = "private"
}
resource "aws_s3_bucket_object" "mtastspolicyfile" {
key = ".well-known/mta-sts.txt"
bucket = aws_s3_bucket.policybucket.id
content = <<EOF
version: STSv1
mode: ${var.mode}
${join("", formatlist("mx: %s\n", var.mx))}max_age: ${var.max_age}
EOF
content_type = "text/plain"
}
locals {
s3_origin_id = "myS3Origin"
}
resource "aws_cloudfront_distribution" "s3_distribution" {
origin {
domain_name = aws_s3_bucket.policybucket.bucket_regional_domain_name
origin_id = local.s3_origin_id
s3_origin_config {
origin_access_identity = aws_cloudfront_origin_access_identity.policybucketoai.cloudfront_access_identity_path
}
}
enabled = true
aliases = [local.policydomain]
default_cache_behavior {
allowed_methods = ["GET", "HEAD"]
cached_methods = ["GET", "HEAD"]
target_origin_id = local.s3_origin_id
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
viewer_protocol_policy = "https-only"
min_ttl = 0
default_ttl = 300
max_ttl = 300
}
restrictions {
geo_restriction {
restriction_type = "none"
}
}
viewer_certificate {
acm_certificate_arn = aws_acm_certificate.cert.arn
ssl_support_method = "sni-only"
minimum_protocol_version = "TLSv1.2_2018"
}
}
resource "aws_cloudfront_origin_access_identity" "policybucketoai" {
comment = "OAI for MTA-STS policy bucket (${var.domain})"
}
data "aws_iam_policy_document" "s3_policy" {
statement {
actions = ["s3:GetObject"]
resources = ["${aws_s3_bucket.policybucket.arn}/*"]
principals {
type = "AWS"
identifiers = [aws_cloudfront_origin_access_identity.policybucketoai.iam_arn]
}
}
}
resource "aws_s3_bucket_policy" "policybucketpolicy" {
bucket = aws_s3_bucket.policybucket.id
policy = data.aws_iam_policy_document.s3_policy.json
}
resource "aws_route53_record" "cloudfrontalias" {
name = local.policydomain
type = "A"
zone_id = data.aws_route53_zone.zone.id
alias {
evaluate_target_health = true
name = aws_cloudfront_distribution.s3_distribution.domain_name
zone_id = aws_cloudfront_distribution.s3_distribution.hosted_zone_id
}
}
resource "aws_route53_record" "smtptlsreporting" {
zone_id = data.aws_route53_zone.zone.id
name = "_smtp._tls.${var.domain}"
type = "TXT"
ttl = "300"
count = length(var.reporting_email) > 0 ? 1 : 0
records = [
"v=TLSRPTv1;rua=mailto:${var.reporting_email}",
]
}
resource "aws_route53_record" "mtastspolicydns" {
zone_id = data.aws_route53_zone.zone.id
name = "_mta-sts.${var.domain}"
type = "TXT"
ttl = "300"
records = [
"v=STSv1; id=${local.policyhash}",
]
}