Skip to content

Commit bd1ca95

Browse files
CCM-10442: Event publisher lambda
1 parent 2c318eb commit bd1ca95

27 files changed

+875
-4
lines changed

infrastructure/terraform/components/app/module_backend_api.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ module "backend_api" {
3131
email_domain = module.ses.domain
3232
template_submitted_sender_email_address = "template-submitted@${module.ses.domain}"
3333
proof_requested_sender_email_address = "proof-requested@${module.ses.domain}"
34+
35+
sns_topic_arn = module.eventpub.sns_topic.arn
3436
}

infrastructure/terraform/modules/backend-api/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ No requirements.
3232
| <a name="input_proof_requested_sender_email_address"></a> [proof\_requested\_sender\_email\_address](#input\_proof\_requested\_sender\_email\_address) | Proof requested sender email address | `string` | n/a | yes |
3333
| <a name="input_region"></a> [region](#input\_region) | The AWS Region | `string` | n/a | yes |
3434
| <a name="input_send_to_firehose"></a> [send\_to\_firehose](#input\_send\_to\_firehose) | Flag indicating whether logs should be sent to firehose | `bool` | n/a | yes |
35+
| <a name="input_sns_topic_arn"></a> [sns\_topic\_arn](#input\_sns\_topic\_arn) | SNS topic ARN | `string` | `null` | no |
3536
| <a name="input_template_submitted_sender_email_address"></a> [template\_submitted\_sender\_email\_address](#input\_template\_submitted\_sender\_email\_address) | Template submitted sender email address | `string` | n/a | yes |
3637
## Modules
3738

@@ -45,6 +46,7 @@ No requirements.
4546
| <a name="module_get_template_lambda"></a> [get\_template\_lambda](#module\_get\_template\_lambda) | git::https://github.com/NHSDigital/nhs-notify-shared-modules.git//infrastructure/modules/lambda | v2.0.4 |
4647
| <a name="module_lambda_copy_scanned_object_to_internal"></a> [lambda\_copy\_scanned\_object\_to\_internal](#module\_lambda\_copy\_scanned\_object\_to\_internal) | git::https://github.com/NHSDigital/nhs-notify-shared-modules.git//infrastructure/modules/lambda | v2.0.4 |
4748
| <a name="module_lambda_delete_failed_scanned_object"></a> [lambda\_delete\_failed\_scanned\_object](#module\_lambda\_delete\_failed\_scanned\_object) | git::https://github.com/NHSDigital/nhs-notify-shared-modules.git//infrastructure/modules/lambda | v2.0.4 |
49+
| <a name="module_lambda_event_publisher"></a> [lambda\_event\_publisher](#module\_lambda\_event\_publisher) | git::https://github.com/NHSDigital/nhs-notify-shared-modules.git//infrastructure/modules/lambda | v2.0.4 |
4850
| <a name="module_lambda_process_proof"></a> [lambda\_process\_proof](#module\_lambda\_process\_proof) | git::https://github.com/NHSDigital/nhs-notify-shared-modules.git//infrastructure/modules/lambda | v2.0.4 |
4951
| <a name="module_lambda_set_file_virus_scan_status_for_upload"></a> [lambda\_set\_file\_virus\_scan\_status\_for\_upload](#module\_lambda\_set\_file\_virus\_scan\_status\_for\_upload) | git::https://github.com/NHSDigital/nhs-notify-shared-modules.git//infrastructure/modules/lambda | v2.0.4 |
5052
| <a name="module_lambda_sftp_poll"></a> [lambda\_sftp\_poll](#module\_lambda\_sftp\_poll) | git::https://github.com/NHSDigital/nhs-notify-shared-modules.git//infrastructure/modules/lambda | v2.0.4 |
@@ -56,7 +58,7 @@ No requirements.
5658
| <a name="module_s3bucket_internal"></a> [s3bucket\_internal](#module\_s3bucket\_internal) | git::https://github.com/NHSDigital/nhs-notify-shared-modules.git//infrastructure/modules/s3bucket | v1.0.8 |
5759
| <a name="module_s3bucket_quarantine"></a> [s3bucket\_quarantine](#module\_s3bucket\_quarantine) | git::https://github.com/NHSDigital/nhs-notify-shared-modules.git//infrastructure/modules/s3bucket | v1.0.8 |
5860
| <a name="module_sqs_sftp_upload"></a> [sqs\_sftp\_upload](#module\_sqs\_sftp\_upload) | git::https://github.com/NHSDigital/nhs-notify-shared-modules.git//infrastructure/modules/sqs | v2.0.1 |
59-
| <a name="module_sqs_template_mgmt_events"></a> [sqs\_template\_mgmt\_events](#module\_sqs\_template\_mgmt\_events) | git::https://github.com/NHSDigital/nhs-notify-shared-modules.git//infrastructure/modules/sqs | v2.0.8 |
61+
| <a name="module_sqs_template_mgmt_events"></a> [sqs\_template\_mgmt\_events](#module\_sqs\_template\_mgmt\_events) | git::https://github.com/NHSDigital/nhs-notify-shared-modules.git//infrastructure/modules/sqs | v2.0.15 |
6062
| <a name="module_sqs_template_table_events_pipe_dlq"></a> [sqs\_template\_table\_events\_pipe\_dlq](#module\_sqs\_template\_table\_events\_pipe\_dlq) | git::https://github.com/NHSDigital/nhs-notify-shared-modules.git//infrastructure/modules/sqs | v2.0.8 |
6163
| <a name="module_sqs_validate_letter_template_files"></a> [sqs\_validate\_letter\_template\_files](#module\_sqs\_validate\_letter\_template\_files) | git::https://github.com/NHSDigital/nhs-notify-shared-modules.git//infrastructure/modules/sqs | v2.0.1 |
6264
| <a name="module_submit_template_lambda"></a> [submit\_template\_lambda](#module\_submit\_template\_lambda) | git::https://github.com/NHSDigital/nhs-notify-shared-modules.git//infrastructure/modules/lambda | v2.0.4 |
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
module "lambda_event_publisher" {
2+
source = "git::https://github.com/NHSDigital/nhs-notify-shared-modules.git//infrastructure/modules/lambda?ref=v2.0.4"
3+
4+
project = var.project
5+
environment = var.environment
6+
component = var.component
7+
aws_account_id = var.aws_account_id
8+
region = var.region
9+
10+
kms_key_arn = var.kms_key_arn
11+
12+
function_name = "event-publisher"
13+
14+
function_module_name = "event-publisher"
15+
handler_function_name = "handler"
16+
description = "Lambda that accepts events from the dynamodb stream and publishes them to SNS"
17+
18+
memory = 512
19+
timeout = 20
20+
runtime = "nodejs20.x"
21+
22+
log_retention_in_days = var.log_retention_in_days
23+
iam_policy_document = {
24+
body = data.aws_iam_policy_document.event_publisher.json
25+
}
26+
27+
lambda_env_vars = {
28+
SNS_TOPIC_ARN = coalesce(var.sns_topic_arn, aws_sns_topic.main.arn)
29+
TEMPLATES_TABLE_NAME = aws_dynamodb_table.templates.name
30+
EVENT_SOURCE = "//notify.nhs.uk/${var.component}/${var.group}/${var.environment}"
31+
}
32+
33+
function_s3_bucket = var.function_s3_bucket
34+
function_code_base_path = local.lambdas_dir
35+
function_code_dir = "event-publisher/dist"
36+
37+
send_to_firehose = var.send_to_firehose
38+
log_destination_arn = var.log_destination_arn
39+
log_subscription_role_arn = var.log_subscription_role_arn
40+
}
41+
42+
resource "aws_lambda_event_source_mapping" "event_publisher" {
43+
event_source_arn = module.sqs_template_mgmt_events.sqs_queue_arn
44+
function_name = module.lambda_event_publisher.function_name
45+
batch_size = 5
46+
maximum_batching_window_in_seconds = 0
47+
function_response_types = [
48+
"ReportBatchItemFailures"
49+
]
50+
51+
scaling_config {
52+
maximum_concurrency = 5
53+
}
54+
}
55+
56+
data "aws_iam_policy_document" "event_publisher" {
57+
statement {
58+
sid = "AllowSNS"
59+
effect = "Allow"
60+
61+
actions = [
62+
"sns:Publish",
63+
]
64+
65+
resources = [
66+
coalesce(var.sns_topic_arn, aws_sns_topic.main.arn)
67+
]
68+
}
69+
70+
statement {
71+
sid = "AllowSQSDLQ"
72+
effect = "Allow"
73+
74+
actions = [
75+
"sqs:SendMessage",
76+
]
77+
78+
resources = [
79+
module.sqs_template_mgmt_events.sqs_dlq_arn,
80+
]
81+
}
82+
83+
statement {
84+
sid = "AllowSQS"
85+
effect = "Allow"
86+
87+
actions = [
88+
"sqs:ReceiveMessage",
89+
"sqs:DeleteMessage",
90+
"sqs:GetQueueAttributes",
91+
"sqs:ChangeMessageVisibility",
92+
]
93+
94+
resources = [
95+
module.sqs_template_mgmt_events.sqs_queue_arn,
96+
]
97+
}
98+
99+
statement {
100+
sid = "AllowKMS"
101+
effect = "Allow"
102+
103+
actions = [
104+
"kms:Decrypt",
105+
"kms:DescribeKey",
106+
"kms:Encrypt",
107+
"kms:GenerateDataKey*",
108+
"kms:ReEncrypt*",
109+
]
110+
111+
resources = [
112+
var.kms_key_arn,
113+
]
114+
}
115+
}

infrastructure/terraform/modules/backend-api/module_sqs_template_mgmt_events.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module "sqs_template_mgmt_events" {
2-
source = "git::https://github.com/NHSDigital/nhs-notify-shared-modules.git//infrastructure/modules/sqs?ref=v2.0.8"
2+
source = "git::https://github.com/NHSDigital/nhs-notify-shared-modules.git//infrastructure/modules/sqs?ref=v2.0.15"
33

44
aws_account_id = var.aws_account_id
55
component = var.component
@@ -9,4 +9,5 @@ module "sqs_template_mgmt_events" {
99
name = "template-mgmt-events"
1010
fifo_queue = true
1111
sqs_kms_key_arn = var.kms_key_arn
12+
create_dlq = true
1213
}

infrastructure/terraform/modules/backend-api/pipes_pipe_template_table_events.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ resource "aws_pipes_pipe" "template_table_events" {
2222
}
2323

2424
target_parameters {
25+
input_template = "{\"dynamodb\": <$.dynamodb>,\"eventID\": <$.eventID>,\"eventName\": <$.eventName>,\"eventSource\": <$.eventSource>,\"tableName\": \"${aws_dynamodb_table.templates.name}\"}"
26+
2527
sqs_queue_parameters {
2628
message_group_id = "$.dynamodb.Keys.id.S"
2729
message_deduplication_id = "$.eventID"

infrastructure/terraform/modules/backend-api/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,9 @@ variable "proof_requested_sender_email_address" {
143143
type = string
144144
description = "Proof requested sender email address"
145145
}
146+
147+
variable "sns_topic_arn" {
148+
type = string
149+
description = "SNS topic ARN"
150+
default = null
151+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist

lambdas/event-publisher/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.build
2+
coverage
3+
node_modules
4+
dist
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { Config } from 'jest';
2+
import { baseJestConfig } from 'nhs-notify-web-template-management-utils';
3+
4+
const config: Config = {
5+
...baseJestConfig,
6+
testEnvironment: 'node',
7+
};
8+
9+
export default config;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"dependencies": {
3+
"@aws-sdk/client-dynamodb": "3.775.0",
4+
"@aws-sdk/client-sns": "3.775.0",
5+
"@aws-sdk/util-dynamodb": "3.775.0",
6+
"nhs-notify-backend-client": "^0.0.1",
7+
"nhs-notify-web-template-management-utils": "^0.0.1",
8+
"zod": "^3.24.2"
9+
},
10+
"devDependencies": {
11+
"@swc/core": "^1.11.13",
12+
"@swc/jest": "^0.2.37",
13+
"@tsconfig/node20": "^20.1.5",
14+
"@types/aws-lambda": "^8.10.148",
15+
"@types/jest": "^29.5.14",
16+
"esbuild": "^0.24.0",
17+
"jest": "^29.7.0",
18+
"jest-mock-extended": "^3.0.7",
19+
"typescript": "^5.8.2"
20+
},
21+
"name": "nhs-notify-templates-event-publisher",
22+
"private": true,
23+
"scripts": {
24+
"lambda-build": "rm -rf dist && npx esbuild --bundle --minify --sourcemap --target=es2020 --platform=node --loader:.node=file --entry-names=[name] --outdir=dist src/event-publisher.ts",
25+
"lint": "eslint .",
26+
"lint:fix": "eslint . --fix",
27+
"test:unit": "jest",
28+
"typecheck": "tsc --noEmit"
29+
},
30+
"version": "0.0.1"
31+
}

0 commit comments

Comments
 (0)