Skip to content

Commit d2eebf1

Browse files
committed
CCM-15212: add SQS into eventpub module
1 parent 6314903 commit d2eebf1

5 files changed

Lines changed: 57 additions & 9 deletions

File tree

infrastructure/terraform/modules/eventpub/lambda/eventpub/src/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,15 @@ async function sendToDLQ(events) {
9595
}
9696
}
9797

98-
exports.handler = async (snsEvent) => {
99-
console.debug(`Received SNS event with ${snsEvent.Records.length} records.`);
98+
exports.handler = async (sqsEvent) => {
99+
console.debug(`Received SQS event with ${sqsEvent.Records.length} records.`);
100100

101101
if (THROTTLE_DELAY_MS > 0) {
102102
console.info(`Throttling enabled. Delaying processing by ${THROTTLE_DELAY_MS}ms`);
103103
await new Promise(res => setTimeout(res, THROTTLE_DELAY_MS));
104104
}
105105

106-
const records = snsEvent.Records.map(record => JSON.parse(record.Sns.Message));
106+
const records = sqsEvent.Records.map(record => JSON.parse(record.body));
107107
const validEvents = records.filter(validateEvent);
108108
const invalidEvents = records.filter(event => !validateEvent(event));
109109

infrastructure/terraform/modules/eventpub/lambda_function.tf

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ resource "aws_lambda_function" "main" {
66
handler = "index.handler"
77
runtime = "nodejs22.x"
88
publish = true
9-
memory_size = 128
9+
memory_size = 512
1010
timeout = 20
1111

1212
filename = data.archive_file.lambda.output_path
@@ -28,3 +28,18 @@ resource "aws_lambda_function" "main" {
2828
}
2929
}
3030
}
31+
32+
resource "aws_lambda_event_source_mapping" "sqs_to_lambda" {
33+
event_source_arn = module.sqs_queue.sqs_queue_arn
34+
function_name = aws_lambda_function.main.function_name
35+
batch_size = 5000
36+
maximum_batching_window_in_seconds = 0
37+
function_response_types = [
38+
"ReportBatchItemFailures"
39+
]
40+
41+
scaling_config {
42+
maximum_concurrency = 20
43+
}
44+
}
45+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module "sqs_queue" {
2+
source = "../../sqs"
3+
4+
aws_account_id = var.aws_account_id
5+
component = var.component
6+
environment = var.environment
7+
project = var.project
8+
region = var.region
9+
name = local.csi
10+
create_dlq = true
11+
sqs_kms_key_arn = var.kms_key_arn
12+
sqs_policy_overload = data.aws_iam_policy_document.allow_sns_send.json
13+
}
14+
15+
data "aws_iam_policy_document" "allow_sns_send" {
16+
statement {
17+
sid = "AllowSNSSendMessage"
18+
effect = "Allow"
19+
20+
principals {
21+
type = "Service"
22+
identifiers = ["sns.amazonaws.com"]
23+
}
24+
25+
actions = [
26+
"sqs:SendMessage",
27+
]
28+
29+
resources = [
30+
module.sqs_queue.sqs_queue_arn,
31+
]
32+
}
33+
}

infrastructure/terraform/modules/eventpub/sns_topic_subscription_lambda.tf

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
resource "aws_sns_topic_subscription" "sqs" {
2+
topic_arn = aws_sns_topic.main.arn
3+
protocol = "sqs"
4+
endpoint = module.sqs_queue.sqs_queue_arn
5+
}

0 commit comments

Comments
 (0)