Skip to content

Commit 132dbe6

Browse files
CCM-12858: Core Notifier code migrated from sms nudge
1 parent a474d08 commit 132dbe6

36 files changed

Lines changed: 2870 additions & 2 deletions

infrastructure/terraform/components/dl/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ No requirements.
1515
| <a name="input_apim_keygen_schedule"></a> [apim\_keygen\_schedule](#input\_apim\_keygen\_schedule) | Schedule to refresh key pairs if necessary | `string` | `"cron(0 14 * * ? *)"` | no |
1616
| <a name="input_aws_account_id"></a> [aws\_account\_id](#input\_aws\_account\_id) | The AWS Account ID (numeric) | `string` | n/a | yes |
1717
| <a name="input_component"></a> [component](#input\_component) | The variable encapsulating the name of this component | `string` | `"dl"` | no |
18+
| <a name="input_core_notify_sandbox_base_url"></a> [core\_notify\_sandbox\_base\_url](#input\_core\_notify\_sandbox\_base\_url) | The URL used to send requests to Notify sandbox environment (should not be used in production environment) | `string` | `"https://sandbox.api.service.nhs.uk"` | no |
1819
| <a name="input_default_tags"></a> [default\_tags](#input\_default\_tags) | A map of default tags to apply to all taggable resources within the component | `map(string)` | `{}` | no |
1920
| <a name="input_enable_dynamodb_delete_protection"></a> [enable\_dynamodb\_delete\_protection](#input\_enable\_dynamodb\_delete\_protection) | Enable DynamoDB Delete Protection on all Tables | `bool` | `true` | no |
2021
| <a name="input_enable_pdm_mock"></a> [enable\_pdm\_mock](#input\_enable\_pdm\_mock) | Flag indicating whether to deploy PDM mock API (should be false in production environments) | `bool` | `true` | no |
@@ -35,10 +36,12 @@ No requirements.
3536
| <a name="input_region"></a> [region](#input\_region) | The AWS Region | `string` | n/a | yes |
3637
| <a name="input_shared_infra_account_id"></a> [shared\_infra\_account\_id](#input\_shared\_infra\_account\_id) | The AWS Shared Infra Account ID (numeric) | `string` | n/a | yes |
3738
| <a name="input_ttl_poll_schedule"></a> [ttl\_poll\_schedule](#input\_ttl\_poll\_schedule) | Schedule to poll for any overdue TTL records | `string` | `"rate(10 minutes)"` | no |
39+
| <a name="input_use_core_nofiy_sandbox"></a> [use\_core\_nofiy\_sandbox](#input\_use\_core\_nofiy\_sandbox) | Flag indicating whether to use Core Notify sandbox or not (should be false in production environments) | `bool` | `true` | no |
3840
## Modules
3941

4042
| Name | Source | Version |
4143
|------|--------|---------|
44+
| <a name="module_core_notifier"></a> [core\_notifier](#module\_core\_notifier) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.24/terraform-lambda.zip | n/a |
4245
| <a name="module_kms"></a> [kms](#module\_kms) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.24/terraform-kms.zip | n/a |
4346
| <a name="module_lambda_apim_key_generation"></a> [lambda\_apim\_key\_generation](#module\_lambda\_apim\_key\_generation) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.29/terraform-lambda.zip | n/a |
4447
| <a name="module_lambda_lambda_apim_refresh_token"></a> [lambda\_lambda\_apim\_refresh\_token](#module\_lambda\_lambda\_apim\_refresh\_token) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.29/terraform-lambda.zip | n/a |
@@ -49,6 +52,8 @@ No requirements.
4952
| <a name="module_s3bucket_cf_logs"></a> [s3bucket\_cf\_logs](#module\_s3bucket\_cf\_logs) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.24/terraform-s3bucket.zip | n/a |
5053
| <a name="module_s3bucket_letters"></a> [s3bucket\_letters](#module\_s3bucket\_letters) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.24/terraform-s3bucket.zip | n/a |
5154
| <a name="module_s3bucket_static_assets"></a> [s3bucket\_static\_assets](#module\_s3bucket\_static\_assets) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.24/terraform-s3bucket.zip | n/a |
55+
| <a name="module_sqs_core_notifier"></a> [sqs\_core\_notifier](#module\_sqs\_core\_notifier) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.24/terraform-sqs.zip | n/a |
56+
| <a name="module_sqs_core_notifier_errors"></a> [sqs\_core\_notifier\_errors](#module\_sqs\_core\_notifier\_errors) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.24/terraform-sqs.zip | n/a |
5257
| <a name="module_sqs_event_publisher_errors"></a> [sqs\_event\_publisher\_errors](#module\_sqs\_event\_publisher\_errors) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.24/terraform-sqs.zip | n/a |
5358
| <a name="module_sqs_pdm_poll"></a> [sqs\_pdm\_poll](#module\_sqs\_pdm\_poll) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.24/terraform-sqs.zip | n/a |
5459
| <a name="module_sqs_pdm_uploader"></a> [sqs\_pdm\_uploader](#module\_sqs\_pdm\_uploader) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.24/terraform-sqs.zip | n/a |
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
resource "aws_cloudwatch_event_rule" "pdm_resource_available" {
2+
name = "${local.csi}-pdm-resource-available"
3+
description = "PDM resource available event rule"
4+
event_bus_name = aws_cloudwatch_event_bus.main.name
5+
event_pattern = jsonencode({
6+
"detail" : {
7+
"type" : [
8+
"uk.nhs.notify.digital.letters.pdm.resource.available.v1"
9+
],
10+
}
11+
})
12+
}
13+
14+
resource "aws_cloudwatch_event_target" "core-notifier-target" {
15+
rule = aws_cloudwatch_event_rule.pdm_resource_available.name
16+
arn = module.sqs_core_notifier.sqs_queue_arn
17+
target_id = "core-notifier-target"
18+
event_bus_name = aws_cloudwatch_event_bus.main.name
19+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
resource "aws_lambda_event_source_mapping" "core_notifier_lambda" {
2+
event_source_arn = module.sqs_core_notifier.sqs_queue_arn
3+
function_name = module.core_notifier.function_arn
4+
batch_size = var.queue_batch_size
5+
maximum_batching_window_in_seconds = var.queue_batch_window_seconds
6+
7+
function_response_types = [
8+
"ReportBatchItemFailures"
9+
]
10+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
module "core_notifier" {
2+
source = "https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.24/terraform-lambda.zip"
3+
4+
function_name = "core-notifier"
5+
description = "A function to send messages to core Notify when a PDM resource is available"
6+
7+
aws_account_id = var.aws_account_id
8+
component = local.component
9+
environment = var.environment
10+
project = var.project
11+
region = var.region
12+
group = var.group
13+
14+
log_retention_in_days = var.log_retention_in_days
15+
kms_key_arn = module.kms.key_arn
16+
17+
iam_policy_document = {
18+
body = data.aws_iam_policy_document.core_notifier_lambda.json
19+
}
20+
21+
function_s3_bucket = local.acct.s3_buckets["lambda_function_artefacts"]["id"]
22+
function_code_base_path = local.aws_lambda_functions_dir_path
23+
function_code_dir = "core-notifier-lambda/dist"
24+
function_include_common = true
25+
handler_function_name = "handler"
26+
runtime = "nodejs22.x"
27+
memory = 128
28+
timeout = 60
29+
log_level = var.log_level
30+
31+
force_lambda_code_deploy = var.force_lambda_code_deploy
32+
enable_lambda_insights = false
33+
34+
send_to_firehose = true
35+
log_destination_arn = local.log_destination_arn
36+
log_subscription_role_arn = local.acct.log_subscription_role_arn
37+
38+
lambda_env_vars = {
39+
"APIM_BASE_URL" = var.use_core_nofiy_sandbox ? var.core_notify_sandbox_base_url : var.apim_base_url
40+
"APIM_ACCESS_TOKEN_SSM_PARAMETER_NAME" = local.apim_access_token_ssm_parameter_name
41+
"EVENT_PUBLISHER_EVENT_BUS_ARN" = aws_cloudwatch_event_bus.main.arn
42+
"EVENT_PUBLISHER_DLQ_URL" = module.sqs_event_publisher_errors.sqs_queue_url
43+
"ENVIRONMENT" = var.environment
44+
}
45+
}
46+
47+
data "aws_iam_policy_document" "core_notifier_lambda" {
48+
statement {
49+
sid = "AllowSSMParam"
50+
effect = "Allow"
51+
52+
actions = [
53+
"ssm:GetParameter",
54+
"ssm:GetParameters",
55+
"ssm:GetParametersByPath"
56+
]
57+
58+
resources = [
59+
"arn:aws:ssm:${var.region}:${var.aws_account_id}:parameter/${var.component}/${var.environment}/apim/*",
60+
"arn:aws:ssm:${var.region}:${var.aws_account_id}:parameter/${var.component}/${var.environment}/senders/*"
61+
]
62+
}
63+
64+
statement {
65+
sid = "KMSPermissions"
66+
effect = "Allow"
67+
68+
actions = [
69+
"kms:Decrypt",
70+
"kms:GenerateDataKey",
71+
]
72+
73+
resources = [
74+
module.kms.key_arn,
75+
]
76+
}
77+
78+
statement {
79+
sid = "SQSPermissionsUploadToCoreNotifierQueue"
80+
effect = "Allow"
81+
82+
actions = [
83+
"sqs:ReceiveMessage",
84+
"sqs:DeleteMessage",
85+
"sqs:GetQueueAttributes",
86+
"sqs:GetQueueUrl",
87+
]
88+
89+
resources = [
90+
module.sqs_core_notifier.sqs_queue_arn,
91+
]
92+
}
93+
94+
statement {
95+
sid = "PutEvents"
96+
effect = "Allow"
97+
98+
actions = [
99+
"events:PutEvents",
100+
]
101+
102+
resources = [
103+
aws_cloudwatch_event_bus.main.arn,
104+
]
105+
}
106+
107+
statement {
108+
sid = "SQSPermissionsDLQ"
109+
effect = "Allow"
110+
111+
actions = [
112+
"sqs:SendMessage",
113+
"sqs:SendMessageBatch",
114+
]
115+
116+
resources = [
117+
module.sqs_event_publisher_errors.sqs_queue_arn,
118+
]
119+
}
120+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module "sqs_core_notifier" {
2+
source = "https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.24/terraform-sqs.zip"
3+
4+
aws_account_id = var.aws_account_id
5+
component = local.component
6+
environment = var.environment
7+
project = var.project
8+
region = var.region
9+
name = "core-notifier"
10+
11+
sqs_kms_key_arn = module.kms.key_arn
12+
13+
visibility_timeout_seconds = 60
14+
15+
create_dlq = true
16+
17+
sqs_policy_overload = data.aws_iam_policy_document.sqs_core_notifier.json
18+
}
19+
20+
data "aws_iam_policy_document" "sqs_core_notifier" {
21+
statement {
22+
sid = "AllowEventBridgeToSendMessage"
23+
effect = "Allow"
24+
25+
principals {
26+
type = "Service"
27+
identifiers = ["events.amazonaws.com"]
28+
}
29+
30+
actions = [
31+
"sqs:SendMessage"
32+
]
33+
34+
resources = [
35+
"arn:aws:sqs:${var.region}:${var.aws_account_id}:${local.csi}-core-notifier-queue"
36+
]
37+
}
38+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module "sqs_core_notifier_errors" {
2+
source = "https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.24/terraform-sqs.zip"
3+
4+
aws_account_id = var.aws_account_id
5+
component = local.component
6+
environment = var.environment
7+
project = var.project
8+
region = var.region
9+
name = "core-notifier-errors"
10+
11+
sqs_kms_key_arn = module.kms.key_arn
12+
13+
visibility_timeout_seconds = 60
14+
}

infrastructure/terraform/components/dl/variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,15 @@ variable "enable_pdm_mock" {
163163
description = "Flag indicating whether to deploy PDM mock API (should be false in production environments)"
164164
default = true
165165
}
166+
167+
variable "use_core_nofiy_sandbox" {
168+
type = bool
169+
description = "Flag indicating whether to use Core Notify sandbox or not (should be false in production environments)"
170+
default = true
171+
}
172+
173+
variable "core_notify_sandbox_base_url" {
174+
type = string
175+
description = "The URL used to send requests to Notify sandbox environment (should not be used in production environment)"
176+
default = "https://sandbox.api.service.nhs.uk"
177+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { baseJestConfig } from '../../jest.config.base';
2+
3+
const config = baseJestConfig;
4+
5+
export default config;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"dependencies": {
3+
"aws-lambda": "^1.0.7",
4+
"axios": "^1.13.2",
5+
"digital-letters-events": "^0.0.1",
6+
"sender-management": "^0.0.1",
7+
"utils": "^0.0.1"
8+
},
9+
"devDependencies": {
10+
"@tsconfig/node22": "^22.0.2",
11+
"@types/aws-lambda": "^8.10.155",
12+
"@types/jest": "^29.5.14",
13+
"aws-sdk-client-mock": "^4.1.0",
14+
"aws-sdk-client-mock-jest": "^4.1.0",
15+
"jest": "^29.7.0",
16+
"jest-mock-extended": "^3.0.7",
17+
"typescript": "^5.9.3"
18+
},
19+
"name": "nhs-notify-digital-core-notifier-lambda",
20+
"private": true,
21+
"scripts": {
22+
"lambda-build": "rm -rf dist && npx esbuild --bundle --minify --sourcemap --target=es2020 --platform=node --loader:.node=file --entry-names=[name] --outdir=dist src/index.ts",
23+
"lint": "eslint .",
24+
"lint:fix": "eslint . --fix",
25+
"test:unit": "jest",
26+
"typecheck": "tsc --noEmit"
27+
},
28+
"version": "0.0.1"
29+
}

0 commit comments

Comments
 (0)