generated from silinternational/template-public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
191 lines (162 loc) · 4.47 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
locals {
aws_account = data.aws_caller_identity.this.account_id
aws_region = data.aws_region.current.name
email_domain = split("@", var.email_from_address)[1]
mail_from_domain = "${var.mail_from_subdomain}.${local.email_domain}"
}
data "aws_caller_identity" "this" {}
data "aws_region" "current" {}
data "cloudflare_zone" "this" {
name = var.cloudflare_domain == "" ? local.email_domain : var.cloudflare_domain
}
/*
* Domain Identity
*/
resource "aws_ses_domain_identity" "this" {
domain = local.email_domain
}
# DKIM records
resource "aws_ses_domain_dkim" "this" {
domain = one(aws_ses_domain_identity.this[*].domain)
}
resource "cloudflare_record" "ses_dkim" {
count = 3
name = "${element(one(aws_ses_domain_dkim.this[*].dkim_tokens), count.index)}._domainkey.${local.email_domain}"
type = "CNAME"
zone_id = data.cloudflare_zone.this.id
value = "${element(one(aws_ses_domain_dkim.this[*].dkim_tokens), count.index)}.dkim.amazonses.com"
tags = var.cloudflare_tags
comment = "DKIM record for email authentication"
}
# TXT record for SPF
resource "cloudflare_record" "spf" {
count = var.create_spf_record ? 1 : 0
name = local.email_domain
type = "TXT"
zone_id = data.cloudflare_zone.this.id
value = var.spf_record_text
tags = var.cloudflare_tags
comment = "SPF record for email authentication"
}
# DMARC record
resource "cloudflare_record" "dmarc" {
count = var.create_dmarc_record ? 1 : 0
name = "_dmarc.${local.email_domain}"
type = "TXT"
zone_id = data.cloudflare_zone.this.id
value = var.dmarc_record_text
comment = "DMARC record for ${local.email_domain}"
tags = var.cloudflare_tags
}
/*
* Custom Mail From domain and DNS records
*/
resource "aws_ses_domain_mail_from" "this" {
domain = local.email_domain
mail_from_domain = local.mail_from_domain
depends_on = [
aws_ses_domain_identity.this
]
}
resource "cloudflare_record" "from_domain_mx" {
name = local.mail_from_domain
type = "MX"
zone_id = data.cloudflare_zone.this.id
priority = 10
value = "feedback-smtp.${local.aws_region}.amazonses.com"
tags = var.cloudflare_tags
comment = "MX record for ${local.email_domain} bounce messages"
}
resource "cloudflare_record" "from_domain_spf" {
name = local.mail_from_domain
type = "TXT"
zone_id = data.cloudflare_zone.this.id
value = "v=spf1 include:amazonses.com ~all"
tags = var.cloudflare_tags
comment = "SPF record for ${local.email_domain} bounce messages"
}
/*
* SMTP user and access key
*/
resource "aws_iam_user" "smtp" {
count = var.create_smtp_user ? 1 : 0
name = var.smtp_user_name == "" ? "${local.email_domain}-smtp-user" : var.smtp_user_name
}
resource "aws_iam_access_key" "smtp" {
count = var.create_smtp_user ? 1 : 0
user = one(aws_iam_user.smtp[*].name)
}
resource "aws_iam_user_policy" "smtp" {
count = var.create_smtp_user ? 1 : 0
name = "SMTP-User"
user = one(aws_iam_user.smtp[*].name)
policy = jsonencode(
{
Version = "2012-10-17"
Statement = [
{
Effect = "Allow",
Action = [
"ses:sendEmail",
"ses:sendRawEmail",
]
Resource = aws_ses_domain_identity.this.arn,
Condition = {
StringEquals = {
"ses:FromAddress" = var.email_from_address
}
}
}
]
}
)
}
/*
* Create ECS role
*/
resource "aws_iam_role" "ecs" {
count = var.create_ecs_role ? 1 : 0
name = var.ecs_role_name
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Sid = "ECSAssumeRole"
Effect = "Allow"
Principal = {
Service = [
"ecs-tasks.amazonaws.com",
]
}
Action = "sts:AssumeRole"
Condition = {
ArnLike = {
"aws:SourceArn" = "arn:aws:ecs:${local.aws_region}:${local.aws_account}:*"
}
StringEquals = {
"aws:SourceAccount" = local.aws_account
}
}
}
]
})
}
resource "aws_iam_role_policy" "ses" {
count = var.create_ecs_role ? 1 : 0
name = "ses"
role = one(aws_iam_role.ecs[*].name)
policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Sid = "SendEmail"
Effect = "Allow"
Action = "ses:SendEmail"
Resource = "*"
Condition = {
StringEquals = {
"ses:FromAddress" = var.email_from_address
}
}
}]
})
}