Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitleaksignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ e12407e09151898bfd8d049d57eee9db9977d56b:.github/copilot-instructions.md:generic
82cf3b2e89ea24b97c4ffc09e618700fb1b0aff3:pact-contracts/pacts/letter-rendering/supplier-api-letter-request-prepared.json:generic-api-key:10
82f6be3e657b46d8447e77cdc1894fba0b855c26:tests/component-tests/testCases/create-letter-request.spec.ts:generic-api-key:10
debc75a97cfe551a69fd1e8694be483213322a9d:pact-contracts/pacts/letter-rendering/supplier-api-letter-request-prepared.json:generic-api-key:10
777eb4047ad06b9e939a292ee18664a0ffee4f29:tests/resources/prepared-letter.json:generic-api-key:4
4fa1923947bbff2387218d698d766cbb7c121a0f:pact-contracts/pacts/letter-rendering/supplier-api-letter-request-prepared.json:generic-api-key:10
3 changes: 3 additions & 0 deletions infrastructure/terraform/components/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ No requirements.

| Name | Source | Version |
|------|--------|---------|
| <a name="module_allocation_lambda"></a> [allocation\_lambda](#module\_allocation\_lambda) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.26/terraform-lambda.zip | n/a |
| <a name="module_authorizer_lambda"></a> [authorizer\_lambda](#module\_authorizer\_lambda) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.29/terraform-lambda.zip | n/a |
| <a name="module_domain_truststore"></a> [domain\_truststore](#module\_domain\_truststore) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.26/terraform-s3bucket.zip | n/a |
| <a name="module_eventpub"></a> [eventpub](#module\_eventpub) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.26/terraform-eventpub.zip | n/a |
Expand All @@ -56,6 +57,8 @@ No requirements.
| <a name="module_post_mi"></a> [post\_mi](#module\_post\_mi) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.29/terraform-lambda.zip | n/a |
| <a name="module_s3bucket_test_letters"></a> [s3bucket\_test\_letters](#module\_s3bucket\_test\_letters) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.26/terraform-s3bucket.zip | n/a |
| <a name="module_sqs_letter_updates"></a> [sqs\_letter\_updates](#module\_sqs\_letter\_updates) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.26/terraform-sqs.zip | n/a |
| <a name="module_supplier_events_forwarder_lambda"></a> [supplier\_events\_forwarder\_lambda](#module\_supplier\_events\_forwarder\_lambda) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.26/terraform-lambda.zip | n/a |
| <a name="module_supplier_events_queue"></a> [supplier\_events\_queue](#module\_supplier\_events\_queue) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.26/terraform-sqs.zip | n/a |
| <a name="module_supplier_ssl"></a> [supplier\_ssl](#module\_supplier\_ssl) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.26/terraform-ssl.zip | n/a |
| <a name="module_upsert_letter"></a> [upsert\_letter](#module\_upsert\_letter) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.29/terraform-lambda.zip | n/a |
## Outputs
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
resource "aws_lambda_event_source_mapping" "supplier_events_forwarder" {
event_source_arn = module.supplier_events_queue.sqs_queue_arn
function_name = module.supplier_events_forwarder_lambda.function_arn
batch_size = 10
function_response_types = [
"ReportBatchItemFailures"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
module "allocation_lambda" {
source = "https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.26/terraform-lambda.zip"

function_name = "allocate_supplier"
description = "Lambda function for allocating supplier"

aws_account_id = var.aws_account_id
component = var.component
environment = var.environment
project = var.project
region = var.region
group = var.group

log_retention_in_days = var.log_retention_in_days
kms_key_arn = module.kms.key_arn

iam_policy_document = {
body = data.aws_iam_policy_document.allocation_lambda.json
}

function_s3_bucket = local.acct.s3_buckets["lambda_function_artefacts"]["id"]
function_code_base_path = local.aws_lambda_functions_dir_path
function_code_dir = "allocation/dist"
function_include_common = true
handler_function_name = "handler"
runtime = "nodejs22.x"
memory = 128
timeout = 29
log_level = var.log_level

force_lambda_code_deploy = var.force_lambda_code_deploy
enable_lambda_insights = false

send_to_firehose = true
log_destination_arn = local.destination_arn
log_subscription_role_arn = local.acct.log_subscription_role_arn

lambda_env_vars = {
QUEUE_URL = module.sqs_letter_updates.sqs_queue_url
}
}


data "aws_iam_policy_document" "allocation_lambda" {
statement {
sid = "KMSPermissions"
effect = "Allow"

actions = [
"kms:Decrypt",
"kms:GenerateDataKey",
]

resources = [
module.kms.key_arn,
]
}

statement {
sid = "AllowQueueAccess"
effect = "Allow"

actions = [
"sqs:SendMessage",
"sqs:GetQueueAttributes",
]

resources = [
module.sqs_letter_updates.sqs_queue_arn
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
module "supplier_events_forwarder_lambda" {
source = "https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.26/terraform-lambda.zip"

function_name = "supplier_events_forwarder"
description = "Lambda function for forwarding supplier events to Firehose"

aws_account_id = var.aws_account_id
component = var.component
environment = var.environment
project = var.project
region = var.region
group = var.group

log_retention_in_days = var.log_retention_in_days
kms_key_arn = module.kms.key_arn

iam_policy_document = {
body = data.aws_iam_policy_document.supplier_events_forwarder_lambda.json
}

function_s3_bucket = local.acct.s3_buckets["lambda_function_artefacts"]["id"]
function_code_base_path = local.aws_lambda_functions_dir_path
function_code_dir = "supplier-events-forwarder/dist"
function_include_common = true
handler_function_name = "handler"
runtime = "nodejs22.x"
memory = 128
timeout = 29
log_level = var.log_level

force_lambda_code_deploy = var.force_lambda_code_deploy
enable_lambda_insights = false

send_to_firehose = true
log_destination_arn = local.destination_arn
log_subscription_role_arn = local.acct.log_subscription_role_arn

lambda_env_vars = {
FIREHOSE_DELIVERY_STREAM_NAME = module.eventsub.firehose_delivery_stream.name
}
}

data "aws_iam_policy_document" "supplier_events_forwarder_lambda" {
statement {
sid = "KMSPermissions"
effect = "Allow"

actions = [
"kms:Decrypt",
"kms:GenerateDataKey",
]

resources = [
module.kms.key_arn,
]
}

statement {
sid = "FirehosePermissions"
effect = "Allow"

actions = [
"firehose:PutRecord",
"firehose:PutRecordBatch",
]

resources = [
module.eventsub.firehose_delivery_stream.arn,
]
}

statement {
sid = "SQSPermissions"
effect = "Allow"

actions = [
"sqs:ReceiveMessage",
"sqs:DeleteMessage",
"sqs:GetQueueAttributes",
]

resources = [
module.supplier_events_queue.sqs_queue_arn,
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,6 @@ module "sqs_letter_updates" {

data "aws_iam_policy_document" "letter_updates_queue_policy" {
version = "2012-10-17"
statement {
sid = "AllowSNSToSendMessage"
effect = "Allow"

principals {
type = "Service"
identifiers = ["sns.amazonaws.com"]
}

actions = [
"sqs:SendMessage"
]

resources = [
"arn:aws:sqs:${var.region}:${var.aws_account_id}:${var.project}-${var.environment}-${var.component}-letter-updates-queue"
]

condition {
test = "ArnEquals"
variable = "aws:SourceArn"
values = [module.eventsub.sns_topic.arn]
}
}

statement {
sid = "AllowSNSPermissions"
effect = "Allow"
Expand All @@ -65,7 +41,9 @@ data "aws_iam_policy_document" "letter_updates_queue_policy" {
condition {
test = "ArnEquals"
variable = "aws:SourceArn"
values = [module.eventsub.sns_topic.arn]
values = [
module.eventsub.sns_topic_clone.arn
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module "supplier_events_queue" {
source = "https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.26/terraform-sqs.zip"

aws_account_id = var.aws_account_id
component = var.component
environment = var.environment
project = var.project
region = var.region
name = "supplier-events"

sqs_kms_key_arn = module.kms.key_arn

visibility_timeout_seconds = 60

create_dlq = true
sqs_policy_overload = data.aws_iam_policy_document.supplier_events_queue_policy.json
}

data "aws_iam_policy_document" "supplier_events_queue_policy" {
version = "2012-10-17"
statement {
sid = "AllowSNSPermissions"
effect = "Allow"

principals {
type = "Service"
identifiers = ["sns.amazonaws.com"]
}

actions = [
"sqs:SendMessage",
"sqs:ListQueueTags",
"sqs:GetQueueUrl",
"sqs:GetQueueAttributes",
]

resources = [
"arn:aws:sqs:${var.region}:${var.aws_account_id}:${var.project}-${var.environment}-${var.component}-supplier-events-queue"
]

condition {
test = "ArnEquals"
variable = "aws:SourceArn"
values = [
module.eventsub.sns_topic_clone.arn
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
resource "aws_sns_topic_subscription" "allocation_lambda" {
topic_arn = module.eventsub.sns_topic.arn
protocol = "lambda"
endpoint = module.allocation_lambda.function_arn
}

resource "aws_lambda_permission" "allocation_lambda_sns" {
statement_id = "AllowExecutionFromSNS"
action = "lambda:InvokeFunction"
function_name = module.allocation_lambda.function_name
principal = "sns.amazonaws.com"
source_arn = module.eventsub.sns_topic.arn
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
resource "aws_sns_topic_subscription" "eventsub_sqs_letter_updates" {
topic_arn = module.eventsub.sns_topic.arn
resource "aws_sns_topic_subscription" "eventsub_sqs_letter_updates_clone" {
topic_arn = module.eventsub.sns_topic_clone.arn
protocol = "sqs"
endpoint = module.sqs_letter_updates.sqs_queue_arn
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
resource "aws_sns_topic_subscription" "supplier_events_queue" {
topic_arn = module.eventsub.sns_topic_clone.arn
protocol = "sqs"
endpoint = module.supplier_events_queue.sqs_queue_arn
raw_message_delivery = false
}
2 changes: 2 additions & 0 deletions infrastructure/terraform/modules/eventsub/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@

| Name | Description |
|------|-------------|
| <a name="output_firehose_delivery_stream"></a> [firehose\_delivery\_stream](#output\_firehose\_delivery\_stream) | Kinesis Firehose Delivery Stream ARN and Name |
| <a name="output_s3_bucket_event_cache"></a> [s3\_bucket\_event\_cache](#output\_s3\_bucket\_event\_cache) | S3 Bucket ARN and Name for event cache |
| <a name="output_sns_topic"></a> [sns\_topic](#output\_sns\_topic) | SNS Topic ARN and Name |
| <a name="output_sns_topic_clone"></a> [sns\_topic\_clone](#output\_sns\_topic\_clone) | SNS Topic ARN and Name |
<!-- vale on -->
<!-- markdownlint-enable -->
<!-- END_TF_DOCS -->
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@ resource "aws_cloudwatch_metric_alarm" "sns_delivery_failures" {
treat_missing_data = "notBreaching"

dimensions = {
TopicName = aws_sns_topic.main.name
TopicName = aws_sns_topic.main_orig.name
}
}

resource "aws_cloudwatch_metric_alarm" "sns_delivery_failures_clone" {
alarm_name = "${local.csi}-sns-delivery-failures"
alarm_description = "RELIABILITY: Alarm for SNS topic delivery failures"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 1
metric_name = "NumberOfNotificationsFailed"
namespace = "AWS/SNS"
period = 300
statistic = "Sum"
threshold = 0
treat_missing_data = "notBreaching"

dimensions = {
TopicName = aws_sns_topic.main_clone.name
}
}
20 changes: 18 additions & 2 deletions infrastructure/terraform/modules/eventsub/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
output "sns_topic" {
description = "SNS Topic ARN and Name"
value = {
arn = aws_sns_topic.main.arn
name = aws_sns_topic.main.name
arn = aws_sns_topic.main_orig.arn
name = aws_sns_topic.main_orig.name
}
}

output "sns_topic_clone" {
description = "SNS Topic ARN and Name"
value = {
arn = aws_sns_topic.main_clone.arn
name = aws_sns_topic.main_clone.name
}
}

Expand All @@ -13,3 +21,11 @@ output "s3_bucket_event_cache" {
bucket = module.s3bucket_event_cache[0].bucket
} : {}
}

output "firehose_delivery_stream" {
description = "Kinesis Firehose Delivery Stream ARN and Name"
value = {
arn = aws_kinesis_firehose_delivery_stream.main[0].arn
name = aws_kinesis_firehose_delivery_stream.main[0].name
}
}
2 changes: 1 addition & 1 deletion infrastructure/terraform/modules/eventsub/sns_topic.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
resource "aws_sns_topic" "main" {
resource "aws_sns_topic" "main_orig" {
name = local.csi
kms_master_key_id = var.kms_key_arn

Expand Down
Loading
Loading