-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
253 lines (199 loc) · 9.43 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# ---------------------------------------------------------------------------------------------------------------------
# Data Sources
# ---------------------------------------------------------------------------------------------------------------------
data "aws_caller_identity" "current" {}
# ---------------------------------------------------------------------------------------------------------------------
# Identity
# ---------------------------------------------------------------------------------------------------------------------
resource "aws_sesv2_email_identity" "this" {
count = var.create_identity ? 1 : 0
email_identity = var.email_identity
configuration_set_name = try(aws_sesv2_configuration_set.this[0].id, null)
dynamic "dkim_signing_attributes" {
for_each = length(keys(var.dkim_signing_attributes)) == 0 ? [] : [var.dkim_signing_attributes]
content {
domain_signing_private_key = lookup(dkim_signing_attributes.value, "domain_signing_private_key", null)
domain_signing_selector = lookup(dkim_signing_attributes.value, "domain_signing_selector", null)
next_signing_key_length = lookup(dkim_signing_attributes.value, "next_signing_key_length", null)
}
}
tags = var.email_identity_tags
}
# ---------------------------------------------------------------------------------------------------------------------
# Identity Feedback Attributes
# ---------------------------------------------------------------------------------------------------------------------
resource "aws_sesv2_email_identity_feedback_attributes" "this" {
count = var.create_email_identity_feedback_attributes ? 1 : 0
email_identity = aws_sesv2_email_identity.this[0].email_identity
email_forwarding_enabled = var.email_forwarding_enabled
}
# -------------------------------------------------------------------------------------------------------------------------------------------------
# Verify domain indentity in Route53 (only works for the 'DOMAIN' identity type & Easy DKIM). For other DNS providers perform verification manually
# -------------------------------------------------------------------------------------------------------------------------------------------------
resource "aws_route53_record" "this" {
count = var.verify_easy_dkim_in_route53 ? 3 : 0
zone_id = var.zone_id
name = format("%s._domainkey.%s", element(aws_sesv2_email_identity.this[0].dkim_signing_attributes[0].tokens, count.index), var.email_identity)
type = "CNAME"
ttl = 1800
records = [format("%s.dkim.amazonses.com", element(aws_sesv2_email_identity.this[0].dkim_signing_attributes[0].tokens, count.index))]
}
# ---------------------------------------------------------------------------------------------------------------------
# Configuration Set
# ---------------------------------------------------------------------------------------------------------------------
resource "aws_sesv2_configuration_set" "this" {
count = var.create_configuration_set ? 1 : 0
configuration_set_name = var.configuration_set_name
dynamic "delivery_options" {
for_each = length(keys(var.delivery_options)) == 0 ? [] : [var.delivery_options]
content {
sending_pool_name = lookup(delivery_options.value, "sending_pool_name", null)
tls_policy = lookup(delivery_options.value, "tls_policy", null)
}
}
dynamic "tracking_options" {
for_each = length(keys(var.tracking_options)) == 0 ? [] : [var.tracking_options]
content {
custom_redirect_domain = tracking_options.value["custom_redirect_domain"]
}
}
dynamic "reputation_options" {
for_each = length(keys(var.reputation_options)) == 0 ? [] : [var.reputation_options]
content {
reputation_metrics_enabled = lookup(reputation_options.value, "reputation_metrics_enabled", false)
}
}
dynamic "sending_options" {
for_each = length(keys(var.sending_options)) == 0 ? [] : [var.sending_options]
content {
sending_enabled = lookup(sending_options.value, "sending_enabled", true)
}
}
dynamic "suppression_options" {
for_each = length(keys(var.suppression_options)) == 0 ? [] : [var.suppression_options]
content {
suppressed_reasons = lookup(suppression_options.value, "suppressed_reasons", null)
}
}
dynamic "vdm_options" {
for_each = length(keys(var.vdm_options)) == 0 ? [] : [var.vdm_options]
content {
dynamic "dashboard_options" {
for_each = length(keys(lookup(vdm_options.value, "dashboard_options", {}))) == 0 ? [] : [lookup(vdm_options.value, "dashboard_options", {})]
content {
engagement_metrics = lookup(dashboard_options.value, "engagement_metrics", null)
}
}
dynamic "guardian_options" {
for_each = length(keys(lookup(vdm_options.value, "guardian_options", {}))) == 0 ? [] : [lookup(vdm_options.value, "guardian_options", {})]
content {
optimized_shared_delivery = lookup(guardian_options.value, "optimized_shared_delivery", null)
}
}
}
}
tags = var.configuration_set_tags
}
# ---------------------------------------------------------------------------------------------------------------------
# Configuration Set Event Destination IAM Role (currently, the module supports only KDF destination)
# ---------------------------------------------------------------------------------------------------------------------
resource "aws_iam_role" "this" {
count = var.create_role ? 1 : 0
name = var.role_name
description = var.role_description
force_detach_policies = var.force_detach_policies
max_session_duration = var.max_session_duration
assume_role_policy = data.aws_iam_policy_document.this_ses_assume_kinesis_data_firehose[0].json
path = var.role_path
permissions_boundary = var.role_permissions_boundary
tags = var.iam_role_tags
}
# ---------------------------------------------------------------------------------------------------------------------
# IAM policy to allow message publishing into Kinesis Data Firehose
# ---------------------------------------------------------------------------------------------------------------------
data "aws_iam_policy_document" "this_kinesis_data_firehose" {
count = var.create_kinesis_data_firehose_policy ? 1 : 0
version = "2012-10-17"
statement {
sid = "PublishToKinesisDataFirehose"
effect = "Allow"
actions = [
"firehose:PutRecordBatch",
]
resources = [
var.kinesis_data_firehose_delivery_stream_arn,
]
}
}
data "aws_iam_policy_document" "this_ses_assume_kinesis_data_firehose" {
count = var.create_kinesis_data_firehose_policy ? 1 : 0
version = "2012-10-17"
statement {
sid = "AllowAmazonSESToAssumeTheRole"
effect = "Allow"
actions = [
"sts:AssumeRole",
]
principals {
type = "Service"
identifiers = [
"ses.amazonaws.com",
]
}
condition {
test = "StringEquals"
variable = "AWS:SourceAccount"
values = [
data.aws_caller_identity.current.account_id,
]
}
condition {
test = "StringEquals"
variable = "AWS:SourceArn"
values = [
aws_sesv2_configuration_set.this[0].arn,
]
}
}
}
resource "aws_iam_policy" "this_kinesis_data_firehose" {
count = var.create_kinesis_data_firehose_policy ? 1 : 0
name = var.kinesis_data_firehose_policy_name
description = var.kinesis_data_firehose_policy_description
path = var.kinesis_data_firehose_policy_path
policy = data.aws_iam_policy_document.this_kinesis_data_firehose[0].json
tags = var.kinesis_data_firehose_policy_tags
}
resource "aws_iam_role_policy_attachment" "this_kinesis_data_firehose" {
count = var.create_kinesis_data_firehose_policy ? 1 : 0
role = aws_iam_role.this[0].name
policy_arn = aws_iam_policy.this_kinesis_data_firehose[0].arn
}
# ---------------------------------------------------------------------------------------------------------------------
# Configuration Set Event Destination (currently, the module supports only KDF destination)
# ---------------------------------------------------------------------------------------------------------------------
# `time_sleep` is used due to unresolved bug - https://github.com/hashicorp/terraform-provider-aws/issues/33367
resource "time_sleep" "wait_120_seconds" {
depends_on = [aws_iam_role_policy_attachment.this_kinesis_data_firehose]
create_duration = "120s"
}
resource "aws_sesv2_configuration_set_event_destination" "this" {
count = var.create_event_destination ? 1 : 0
configuration_set_name = aws_sesv2_configuration_set.this[0].id
event_destination_name = var.event_destination_name
dynamic "event_destination" {
for_each = [var.event_destination]
content {
enabled = lookup(event_destination.value, "enabled", false)
matching_event_types = event_destination.value["matching_event_types"]
dynamic "kinesis_firehose_destination" {
for_each = length(keys(lookup(event_destination.value, "kinesis_firehose_destination", {}))) == 0 ? [] : [lookup(event_destination.value, "kinesis_firehose_destination", {})]
content {
delivery_stream_arn = kinesis_firehose_destination.value["delivery_stream_arn"]
iam_role_arn = lookup(kinesis_firehose_destination.value, "iam_role_arn", aws_iam_role.this[0].arn)
}
}
}
}
depends_on = [time_sleep.wait_120_seconds]
}