-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
68 lines (61 loc) · 2.41 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
locals {
prefix = var.prefix != "" ? (endswith(var.prefix, "-") ? var.prefix : "${var.prefix}-") : ""
suffix_main = var.fifo ? "${var.suffix_main}.fifo" : var.suffix_main
suffix_dlq = var.fifo ? "${var.suffix_dlq}.fifo" : var.suffix_dlq
main_sqs_name = var.name_main != "" ? var.name_main : "${local.prefix}${var.name}${local.suffix_main}"
main_sqs_policy = var.main_policy
dlq_sqs_name = var.name_dlq != "" ? var.name_dlq : "${local.prefix}${var.name}${local.suffix_dlq}"
dlq_sqs_policy = var.create_dlq ? (var.dlq_policy != "" ? var.dlq_policy : data.aws_iam_policy_document.dlq[0].json) : null
dlq_arn = var.create_dlq ? aws_sqs_queue.dlq[0].arn : (length(data.aws_sqs_queue.custom_dlq) > 0 ? data.aws_sqs_queue.custom_dlq[0].arn : null)
}
data "aws_caller_identity" "current" {}
# DLQ
data "aws_iam_policy_document" "dlq" {
count = var.create_dlq ? 1 : 0
statement {
effect = "Allow"
resources = [aws_sqs_queue.dlq[0].arn]
actions = [
"sqs:ChangeMessageVisibility",
"sqs:DeleteMessage",
"sqs:GetQueueAttributes",
"sqs:GetQueueUrl",
"sqs:ListQueueTags",
"sqs:ReceiveMessage",
"sqs:SendMessage",
]
principals {
type = "AWS"
identifiers = [data.aws_caller_identity.current.account_id]
}
}
}
resource "aws_sqs_queue" "dlq" {
count = var.create_dlq ? 1 : 0
name = local.dlq_sqs_name
fifo_queue = var.fifo
content_based_deduplication = var.dlq_content_based_deduplication
message_retention_seconds = var.dlq_retention_time
visibility_timeout_seconds = var.dlq_visibility_timeout
tags = var.tags
}
resource "aws_sqs_queue_policy" "dlq" {
count = var.create_dlq ? 1 : 0
queue_url = aws_sqs_queue.dlq[0].id
policy = local.dlq_sqs_policy
}
# Main
resource "aws_sqs_queue" "main" {
name = local.main_sqs_name
fifo_queue = var.fifo
content_based_deduplication = var.main_content_based_deduplication
message_retention_seconds = var.main_retention_time
visibility_timeout_seconds = var.main_visibility_timeout
delay_seconds = var.main_delay_time
policy = local.main_sqs_policy
redrive_policy = local.dlq_arn != null ? jsonencode({
deadLetterTargetArn = local.dlq_arn
maxReceiveCount = var.max_redrive
}) : null
tags = var.tags
}