Skip to content

Commit c064789

Browse files
CCM-8237 adding SQS module
1 parent cc4d494 commit c064789

11 files changed

Lines changed: 307 additions & 0 deletions
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
data "aws_iam_policy_document" "deadletter_queue" {
2+
count = var.create_dlq ? 1 : 0
3+
4+
statement {
5+
effect = "Allow"
6+
7+
resources = [aws_sqs_queue.deadletter_queue[0].arn]
8+
9+
actions = [
10+
"sqs:ChangeMessageVisibility",
11+
"sqs:DeleteMessage",
12+
"sqs:GetQueueAttributes",
13+
"sqs:GetQueueUrl",
14+
"sqs:ListQueueTags",
15+
"sqs:ReceiveMessage",
16+
"sqs:SendMessage",
17+
]
18+
19+
principals {
20+
type = "AWS"
21+
identifiers = [var.aws_account_id]
22+
}
23+
}
24+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
data "aws_iam_policy_document" "sqs_queue" {
2+
statement {
3+
effect = "Allow"
4+
5+
resources = [aws_sqs_queue.sqs_queue.arn]
6+
7+
actions = [
8+
"sqs:ChangeMessageVisibility",
9+
"sqs:DeleteMessage",
10+
"sqs:GetQueueAttributes",
11+
"sqs:GetQueueUrl",
12+
"sqs:ListQueueTags",
13+
"sqs:ReceiveMessage",
14+
"sqs:SendMessage",
15+
]
16+
17+
principals {
18+
type = "AWS"
19+
identifiers = [var.aws_account_id]
20+
}
21+
}
22+
23+
dynamic "statement" {
24+
for_each = var.sns_source_arn != null ? [1] : []
25+
26+
content {
27+
effect = "Allow"
28+
29+
principals {
30+
type = "Service"
31+
identifiers = [
32+
"sns.amazonaws.com"
33+
]
34+
}
35+
36+
actions = [
37+
"sqs:SendMessage",
38+
"sqs:SendMessageBatch",
39+
]
40+
41+
condition {
42+
test = "ArnEquals"
43+
variable = "aws:SourceArn"
44+
values = [
45+
var.sns_source_arn
46+
]
47+
}
48+
49+
resources = [
50+
aws_sqs_queue.sqs_queue.arn,
51+
]
52+
}
53+
}
54+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
locals {
2+
# Compound Scope Identifier
3+
csi = replace(
4+
format(
5+
"%s-%s-%s-%s",
6+
var.project,
7+
var.environment,
8+
var.component,
9+
var.name
10+
),
11+
"_",
12+
"",
13+
)
14+
15+
# CSI for use in resources with a global namespace, i.e. S3 Buckets
16+
csi_global = replace(
17+
format(
18+
"%s-%s-%s-%s-%s",
19+
var.project,
20+
var.aws_account_id,
21+
var.region,
22+
var.environment,
23+
var.component,
24+
),
25+
"_",
26+
"",
27+
)
28+
29+
default_tags = merge(
30+
var.default_tags,
31+
{
32+
Module = var.module
33+
Name = local.csi
34+
},
35+
)
36+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
output "sqs_queue_url" {
2+
value = aws_sqs_queue.sqs_queue.id
3+
}
4+
5+
output "sqs_queue_arn" {
6+
value = aws_sqs_queue.sqs_queue.arn
7+
}
8+
9+
output "sqs_dlq_url" {
10+
value = var.create_dlq ? aws_sqs_queue.deadletter_queue[0].id : null
11+
}
12+
13+
output "sqs_dlq_arn" {
14+
value = var.create_dlq ? aws_sqs_queue.deadletter_queue[0].arn : null
15+
}
16+
17+
output "sqs_queue_name" {
18+
value = "${local.csi}-queue"
19+
}
20+
21+
output "sqs_dlq_name" {
22+
value = var.create_dlq ? aws_sqs_queue.deadletter_queue[0].name : null
23+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
resource "aws_sqs_queue" "sqs_queue" {
2+
name = "${local.csi}-queue"
3+
4+
message_retention_seconds = var.message_retention_seconds
5+
visibility_timeout_seconds = var.visibility_timeout_seconds
6+
fifo_queue = var.fifo_queue
7+
content_based_deduplication = var.content_based_deduplication
8+
max_message_size = var.max_message_size
9+
10+
kms_master_key_id = var.sqs_kms_key_arn
11+
kms_data_key_reuse_period_seconds = var.kms_data_key_reuse_period_seconds
12+
13+
tags = local.default_tags
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
resource "aws_sqs_queue" "deadletter_queue" {
2+
count = var.create_dlq ? 1 : 0
3+
4+
name = "${local.csi}-dlq"
5+
6+
message_retention_seconds = var.message_retention_seconds
7+
visibility_timeout_seconds = var.visibility_timeout_seconds
8+
fifo_queue = var.fifo_queue
9+
content_based_deduplication = var.content_based_deduplication
10+
max_message_size = var.max_message_size
11+
12+
kms_master_key_id = var.sqs_kms_key_arn
13+
kms_data_key_reuse_period_seconds = var.kms_data_key_reuse_period_seconds
14+
15+
tags = local.default_tags
16+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
resource "aws_sqs_queue_policy" "sqs_queue_policy" {
2+
queue_url = aws_sqs_queue.sqs_queue.id
3+
policy = data.aws_iam_policy_document.sqs_queue.json
4+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
resource "aws_sqs_queue_policy" "deadletter_queue" {
2+
count = var.create_dlq ? 1 : 0
3+
4+
queue_url = aws_sqs_queue.deadletter_queue[0].id
5+
policy = data.aws_iam_policy_document.deadletter_queue[0].json
6+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
resource "aws_sqs_queue_redrive_policy" "redrive_policy" {
2+
count = var.create_dlq ? 1 : 0
3+
4+
queue_url = aws_sqs_queue.sqs_queue.url
5+
redrive_policy = jsonencode({
6+
deadLetterTargetArn = aws_sqs_queue.deadletter_queue[0].arn
7+
maxReceiveCount = 3
8+
})
9+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
##
2+
# Basic Required Variables for tfscaffold Modules
3+
##
4+
5+
variable "project" {
6+
type = string
7+
description = "The name of the tfscaffold project"
8+
}
9+
10+
variable "environment" {
11+
type = string
12+
description = "The name of the tfscaffold environment"
13+
}
14+
15+
variable "component" {
16+
type = string
17+
description = "The name of the tfscaffold component"
18+
}
19+
20+
variable "aws_account_id" {
21+
type = string
22+
description = "The AWS Account ID (numeric)"
23+
}
24+
25+
variable "region" {
26+
type = string
27+
description = "The AWS Region"
28+
}
29+
30+
##
31+
# tfscaffold variables specific to this module
32+
##
33+
34+
variable "module" {
35+
type = string
36+
description = "The variable encapsulating the name of this module"
37+
default = "sqs"
38+
}
39+
40+
variable "default_tags" {
41+
type = map(string)
42+
description = "A map of default tags to apply to all taggable resources within the component"
43+
default = {}
44+
}
45+
46+
##
47+
# Variables specific to this module
48+
##
49+
50+
variable "name" {
51+
type = string
52+
description = "Name of the SQS Queue"
53+
}
54+
55+
variable "sqs_kms_key_arn" {
56+
type = string
57+
description = "ARN of the KMS key to encrypt SQS queue messages"
58+
}
59+
60+
variable "sns_source_arn" {
61+
type = string
62+
description = "ARN of an sns resource allowed to send to this resource"
63+
default = null
64+
}
65+
66+
variable "allowed_arns" {
67+
description = "A list of AWS account IDs allowed to access this resource"
68+
type = list(any)
69+
default = null
70+
}
71+
72+
variable "message_retention_seconds" {
73+
description = "The number of seconds Amazon SQS retains a message. Integer representing seconds, from 60 (1 minute) to 1209600 (14 days)"
74+
type = number
75+
default = null
76+
}
77+
78+
variable "visibility_timeout_seconds" {
79+
description = "The visibility timeout for the queue. An integer from 0 to 43200 (12 hours)"
80+
type = number
81+
default = 300
82+
}
83+
84+
variable "fifo_queue" {
85+
description = "Boolean designating a FIFO queue"
86+
type = bool
87+
default = false
88+
}
89+
90+
variable "content_based_deduplication" {
91+
description = "Enables content-based deduplication for FIFO queues"
92+
type = bool
93+
default = false
94+
}
95+
96+
variable "kms_data_key_reuse_period_seconds" {
97+
description = "The length of time, in seconds, for which Amazon SQS can reuse a data key to encrypt or decrypt messages before calling AWS KMS again. An integer representing seconds, between 60 seconds (1 minute) and 86,400 seconds (24 hours)"
98+
type = number
99+
default = 300
100+
}
101+
102+
variable "max_message_size" {
103+
description = "The limit of how many bytes a message can contain before Amazon SQS rejects it. An integer from 1024 bytes (1 KiB) up to 262144 bytes (256 KiB)"
104+
type = number
105+
default = 262144
106+
}
107+
108+
variable "create_dlq" {
109+
description = "Create a DLQ"
110+
type = bool
111+
default = false
112+
}

0 commit comments

Comments
 (0)