forked from terraform-aws-modules/terraform-aws-sqs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
231 lines (185 loc) · 8.51 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
################################################################################
# Queue
################################################################################
locals {
name = try(trimsuffix(var.name, ".fifo"), "")
}
resource "aws_sqs_queue" "this" {
count = var.create ? 1 : 0
content_based_deduplication = var.content_based_deduplication
deduplication_scope = var.deduplication_scope
delay_seconds = var.delay_seconds
fifo_queue = var.fifo_queue
fifo_throughput_limit = var.fifo_throughput_limit
kms_data_key_reuse_period_seconds = var.kms_data_key_reuse_period_seconds
kms_master_key_id = var.kms_master_key_id
max_message_size = var.max_message_size
message_retention_seconds = var.message_retention_seconds
name = var.use_name_prefix ? null : (var.fifo_queue ? "${local.name}.fifo" : local.name)
name_prefix = var.use_name_prefix ? "${local.name}-" : null
receive_wait_time_seconds = var.receive_wait_time_seconds
sqs_managed_sse_enabled = var.kms_master_key_id != null ? null : var.sqs_managed_sse_enabled
visibility_timeout_seconds = var.visibility_timeout_seconds
tags = var.tags
}
################################################################################
# Queue Policy
################################################################################
data "aws_iam_policy_document" "this" {
count = var.create && var.create_queue_policy ? 1 : 0
source_policy_documents = var.source_queue_policy_documents
override_policy_documents = var.override_queue_policy_documents
dynamic "statement" {
for_each = var.queue_policy_statements
content {
sid = try(statement.value.sid, null)
actions = try(statement.value.actions, null)
not_actions = try(statement.value.not_actions, null)
effect = try(statement.value.effect, null)
resources = try(statement.value.resources, [aws_sqs_queue.this[0].arn])
not_resources = try(statement.value.not_resources, null)
dynamic "principals" {
for_each = try(statement.value.principals, [])
content {
type = principals.value.type
identifiers = principals.value.identifiers
}
}
dynamic "not_principals" {
for_each = try(statement.value.not_principals, [])
content {
type = not_principals.value.type
identifiers = not_principals.value.identifiers
}
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
values = condition.value.values
variable = condition.value.variable
}
}
}
}
}
resource "aws_sqs_queue_policy" "this" {
count = var.create && var.create_queue_policy ? 1 : 0
queue_url = aws_sqs_queue.this[0].url
policy = data.aws_iam_policy_document.this[0].json
}
################################################################################
# Re-drive Policy
################################################################################
resource "aws_sqs_queue_redrive_policy" "this" {
count = var.create && !var.create_dlq && length(var.redrive_policy) > 0 ? 1 : 0
queue_url = aws_sqs_queue.this[0].url
redrive_policy = jsonencode(var.redrive_policy)
}
resource "aws_sqs_queue_redrive_policy" "dlq" {
count = var.create && var.create_dlq ? 1 : 0
queue_url = aws_sqs_queue.this[0].url
redrive_policy = jsonencode(
merge(
{
deadLetterTargetArn = aws_sqs_queue.dlq[0].arn
maxReceiveCount = 5
},
var.redrive_policy
)
)
}
################################################################################
# Dead Letter Queue
################################################################################
locals {
stripped_dlq_name = try(trimsuffix(var.dlq_name, ".fifo"), "")
inter_dlq_name = try(coalesce(local.stripped_dlq_name, "${local.name}-dlq"), "")
dlq_name = var.fifo_queue && !var.use_name_prefix ? "${local.inter_dlq_name}.fifo" : local.inter_dlq_name
dlq_kms_master_key_id = try(coalesce(var.dlq_kms_master_key_id, var.kms_master_key_id), null)
dlq_sqs_managed_sse_enabled = coalesce(var.dlq_sqs_managed_sse_enabled, var.sqs_managed_sse_enabled)
}
resource "aws_sqs_queue" "dlq" {
count = var.create && var.create_dlq ? 1 : 0
content_based_deduplication = try(coalesce(var.dlq_content_based_deduplication, var.content_based_deduplication), null)
deduplication_scope = try(coalesce(var.dlq_deduplication_scope, var.deduplication_scope), null)
delay_seconds = try(coalesce(var.dlq_delay_seconds, var.delay_seconds), null)
# If source queue is FIFO, DLQ must also be FIFO and vice versa
fifo_queue = var.fifo_queue
fifo_throughput_limit = var.fifo_throughput_limit
kms_data_key_reuse_period_seconds = try(coalesce(var.dlq_kms_data_key_reuse_period_seconds, var.kms_data_key_reuse_period_seconds), null)
kms_master_key_id = local.dlq_kms_master_key_id
max_message_size = var.max_message_size
message_retention_seconds = try(coalesce(var.dlq_message_retention_seconds, var.message_retention_seconds), null)
name = var.use_name_prefix ? null : local.dlq_name
name_prefix = var.use_name_prefix ? "${local.dlq_name}-" : null
receive_wait_time_seconds = try(coalesce(var.dlq_receive_wait_time_seconds, var.receive_wait_time_seconds), null)
sqs_managed_sse_enabled = local.dlq_kms_master_key_id != null ? null : local.dlq_sqs_managed_sse_enabled
visibility_timeout_seconds = try(coalesce(var.dlq_visibility_timeout_seconds, var.visibility_timeout_seconds), null)
tags = merge(var.tags, var.dlq_tags)
}
################################################################################
# Queue Policy
################################################################################
data "aws_iam_policy_document" "dlq" {
count = var.create && var.create_dlq && var.create_dlq_queue_policy ? 1 : 0
source_policy_documents = var.source_dlq_queue_policy_documents
override_policy_documents = var.override_dlq_queue_policy_documents
dynamic "statement" {
for_each = var.dlq_queue_policy_statements
content {
sid = try(statement.value.sid, null)
actions = try(statement.value.actions, null)
not_actions = try(statement.value.not_actions, null)
effect = try(statement.value.effect, null)
resources = try(statement.value.resources, [aws_sqs_queue.dlq[0].arn])
not_resources = try(statement.value.not_resources, null)
dynamic "principals" {
for_each = try(statement.value.principals, [])
content {
type = principals.value.type
identifiers = principals.value.identifiers
}
}
dynamic "not_principals" {
for_each = try(statement.value.not_principals, [])
content {
type = not_principals.value.type
identifiers = not_principals.value.identifiers
}
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
values = condition.value.values
variable = condition.value.variable
}
}
}
}
}
resource "aws_sqs_queue_policy" "dlq" {
count = var.create && var.create_dlq && var.create_dlq_queue_policy ? 1 : 0
queue_url = aws_sqs_queue.dlq[0].url
policy = data.aws_iam_policy_document.dlq[0].json
}
################################################################################
# Re-drive Allow Policy
################################################################################
resource "aws_sqs_queue_redrive_allow_policy" "this" {
count = var.create && !var.create_dlq && length(var.redrive_allow_policy) > 0 ? 1 : 0
queue_url = aws_sqs_queue.this[0].url
redrive_allow_policy = jsonencode(var.redrive_allow_policy)
}
resource "aws_sqs_queue_redrive_allow_policy" "dlq" {
count = var.create && var.create_dlq && var.create_dlq_redrive_allow_policy ? 1 : 0
queue_url = aws_sqs_queue.dlq[0].url
redrive_allow_policy = jsonencode(merge(
{
redrivePermission = "byQueue",
sourceQueueArns = [aws_sqs_queue.this[0].arn]
},
var.dlq_redrive_allow_policy)
)
}