Skip to content

Commit 0de0460

Browse files
authored
CCM-12614: Add PDM Poller component (#147)
1 parent 8d297c4 commit 0de0460

56 files changed

Lines changed: 2147 additions & 600 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

infrastructure/terraform/components/dl/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ No requirements.
3131
| <a name="input_pdm_use_non_mock_token"></a> [pdm\_use\_non\_mock\_token](#input\_pdm\_use\_non\_mock\_token) | Whether to use the shared APIM access token from SSM (/component/environment/apim/access\_token) instead of the mock token | `bool` | `false` | no |
3232
| <a name="input_project"></a> [project](#input\_project) | The name of the tfscaffold project | `string` | n/a | yes |
3333
| <a name="input_queue_batch_size"></a> [queue\_batch\_size](#input\_queue\_batch\_size) | maximum number of queue items to process | `number` | `10` | no |
34-
| <a name="input_queue_batch_window_seconds"></a> [queue\_batch\_window\_seconds](#input\_queue\_batch\_window\_seconds) | maximum time in seconds between processing events | `number` | `10` | no |
34+
| <a name="input_queue_batch_window_seconds"></a> [queue\_batch\_window\_seconds](#input\_queue\_batch\_window\_seconds) | maximum time in seconds between processing events | `number` | `1` | no |
3535
| <a name="input_region"></a> [region](#input\_region) | The AWS Region | `string` | n/a | yes |
3636
| <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 |
3737
| <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 |
@@ -44,11 +44,13 @@ No requirements.
4444
| <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 |
4545
| <a name="module_mesh_poll"></a> [mesh\_poll](#module\_mesh\_poll) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.29/terraform-lambda.zip | n/a |
4646
| <a name="module_pdm_mock"></a> [pdm\_mock](#module\_pdm\_mock) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.29/terraform-lambda.zip | n/a |
47+
| <a name="module_pdm_poll"></a> [pdm\_poll](#module\_pdm\_poll) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.29/terraform-lambda.zip | n/a |
4748
| <a name="module_pdm_uploader"></a> [pdm\_uploader](#module\_pdm\_uploader) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.29/terraform-lambda.zip | n/a |
4849
| <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 |
4950
| <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 |
5051
| <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 |
5152
| <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 |
53+
| <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 |
5254
| <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 |
5355
| <a name="module_sqs_ttl"></a> [sqs\_ttl](#module\_sqs\_ttl) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.24/terraform-sqs.zip | n/a |
5456
| <a name="module_sqs_ttl_handle_expiry_errors"></a> [sqs\_ttl\_handle\_expiry\_errors](#module\_sqs\_ttl\_handle\_expiry\_errors) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.24/terraform-sqs.zip | n/a |

infrastructure/terraform/components/dl/aws_api_gateway_deployment_pdm_mock.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ resource "aws_api_gateway_deployment" "pdm_mock" {
1616
aws_api_gateway_resource.document_reference[0].id,
1717
aws_api_gateway_resource.document_reference_id[0].id,
1818
aws_api_gateway_method.create_document_reference[0].id,
19+
aws_api_gateway_method.create_document_reference[0].authorization,
1920
aws_api_gateway_method.get_document_reference[0].id,
21+
aws_api_gateway_method.get_document_reference[0].authorization,
2022
aws_api_gateway_integration.create_document_reference[0].id,
2123
aws_api_gateway_integration.get_document_reference[0].id,
2224
]))

infrastructure/terraform/components/dl/aws_api_gateway_method_create_document_reference.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ resource "aws_api_gateway_method" "create_document_reference" {
44
rest_api_id = aws_api_gateway_rest_api.pdm_mock[0].id
55
resource_id = aws_api_gateway_resource.document_reference[0].id
66
http_method = "POST"
7-
authorization = "AWS_IAM"
7+
authorization = "NONE"
88
}

infrastructure/terraform/components/dl/aws_api_gateway_method_get_document_reference.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ resource "aws_api_gateway_method" "get_document_reference" {
44
rest_api_id = aws_api_gateway_rest_api.pdm_mock[0].id
55
resource_id = aws_api_gateway_resource.document_reference_id[0].id
66
http_method = "GET"
7-
authorization = "AWS_IAM"
7+
authorization = "NONE"
88
}
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_submitted" {
2+
name = "${local.csi}-pdm-resource-submitted"
3+
description = "PDM resource submitted event rule"
4+
event_bus_name = aws_cloudwatch_event_bus.main.name
5+
6+
event_pattern = jsonencode({
7+
"detail" : {
8+
"type" : [
9+
"uk.nhs.notify.digital.letters.pdm.resource.submitted.v1"
10+
]
11+
}
12+
})
13+
}
14+
15+
resource "aws_cloudwatch_event_target" "pdm_resource_submitted_pdm_poll" {
16+
rule = aws_cloudwatch_event_rule.pdm_resource_submitted.name
17+
arn = module.sqs_pdm_poll.sqs_queue_arn
18+
event_bus_name = aws_cloudwatch_event_bus.main.name
19+
}
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_unavailable" {
2+
name = "${local.csi}-pdm-resource-unavailable"
3+
description = "PDM resource unavailable event rule"
4+
event_bus_name = aws_cloudwatch_event_bus.main.name
5+
6+
event_pattern = jsonencode({
7+
"detail" : {
8+
"type" : [
9+
"uk.nhs.notify.digital.letters.pdm.resource.unavailable.v1"
10+
]
11+
}
12+
})
13+
}
14+
15+
resource "aws_cloudwatch_event_target" "pdm_resource_unavailable_pdm_poll" {
16+
rule = aws_cloudwatch_event_rule.pdm_resource_unavailable.name
17+
arn = module.sqs_pdm_poll.sqs_queue_arn
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" "pdm_poll_lambda" {
2+
event_source_arn = module.sqs_pdm_poll.sqs_queue_arn
3+
function_name = module.pdm_poll.function_name
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+
}

infrastructure/terraform/components/dl/module_lambda_pdm_mock.tf

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,6 @@ module "pdm_mock" {
3434

3535
log_destination_arn = local.log_destination_arn
3636
log_subscription_role_arn = local.acct.log_subscription_role_arn
37-
38-
lambda_env_vars = {
39-
MOCK_ACCESS_TOKEN = var.pdm_mock_access_token
40-
ACCESS_TOKEN_SSM_PATH = local.apim_access_token_ssm_parameter_name
41-
USE_NON_MOCK_TOKEN = var.pdm_use_non_mock_token
42-
}
4337
}
4438

4539
data "aws_iam_policy_document" "pdm_mock" {
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
module "pdm_poll" {
2+
source = "https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.29/terraform-lambda.zip"
3+
4+
function_name = "pdm-poll"
5+
description = "A function for polling PDM document status"
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.pdm_poll_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 = "pdm-poll-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+
log_destination_arn = local.log_destination_arn
35+
log_subscription_role_arn = local.acct.log_subscription_role_arn
36+
37+
lambda_env_vars = {
38+
"APIM_BASE_URL" = local.deploy_pdm_mock ? aws_api_gateway_stage.pdm_mock[0].invoke_url : var.apim_base_url
39+
"APIM_ACCESS_TOKEN_SSM_PARAMETER_NAME" = local.apim_access_token_ssm_parameter_name
40+
"EVENT_PUBLISHER_EVENT_BUS_ARN" = aws_cloudwatch_event_bus.main.arn
41+
"EVENT_PUBLISHER_DLQ_URL" = module.sqs_event_publisher_errors.sqs_queue_url
42+
"POLL_MAX_RETRIES" = 10
43+
}
44+
}
45+
46+
data "aws_iam_policy_document" "pdm_poll_lambda" {
47+
statement {
48+
sid = "AllowSSMParam"
49+
effect = "Allow"
50+
51+
actions = [
52+
"ssm:GetParameter",
53+
"ssm:GetParameters",
54+
"ssm:GetParametersByPath"
55+
]
56+
57+
resources = [
58+
"arn:aws:ssm:${var.region}:${var.aws_account_id}:parameter/${var.component}/${var.environment}/apim/*"
59+
]
60+
}
61+
statement {
62+
sid = "PutEvents"
63+
effect = "Allow"
64+
65+
actions = [
66+
"events:PutEvents",
67+
]
68+
69+
resources = [
70+
aws_cloudwatch_event_bus.main.arn,
71+
]
72+
}
73+
74+
statement {
75+
sid = "SQSPermissionsDLQs"
76+
effect = "Allow"
77+
78+
actions = [
79+
"sqs:SendMessage",
80+
"sqs:SendMessageBatch",
81+
]
82+
83+
resources = [
84+
module.sqs_event_publisher_errors.sqs_queue_arn,
85+
]
86+
}
87+
statement {
88+
sid = "SQSPermissionsPollPdmQueue"
89+
effect = "Allow"
90+
91+
actions = [
92+
"sqs:ReceiveMessage",
93+
"sqs:DeleteMessage",
94+
"sqs:GetQueueAttributes",
95+
"sqs:GetQueueUrl",
96+
]
97+
98+
resources = [
99+
module.sqs_pdm_poll.sqs_queue_arn,
100+
]
101+
}
102+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module "sqs_pdm_poll" {
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 = "pdm-poll"
10+
sqs_kms_key_arn = module.kms.key_arn
11+
visibility_timeout_seconds = 60
12+
delay_seconds = 5
13+
create_dlq = true
14+
sqs_policy_overload = data.aws_iam_policy_document.sqs_pdm_poll.json
15+
}
16+
17+
data "aws_iam_policy_document" "sqs_pdm_poll" {
18+
statement {
19+
sid = "AllowEventBridgeToSendMessage"
20+
effect = "Allow"
21+
22+
principals {
23+
type = "Service"
24+
identifiers = ["events.amazonaws.com"]
25+
}
26+
27+
actions = [
28+
"sqs:SendMessage"
29+
]
30+
31+
resources = [
32+
"arn:aws:sqs:${var.region}:${var.aws_account_id}:${local.csi}-pdm-poll-queue"
33+
]
34+
}
35+
}

0 commit comments

Comments
 (0)