generated from NHSDigital/nhs-notify-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 3
CCM-13768 Print analyser component #183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
20b3fde
CCM-13768: add basic print analyser function
Ian-Hodges c9da026
CCM-13768: add fetch from s3
Ian-Hodges 94ef43c
CCM-13768: add createAt to the file.safe event
Ian-Hodges 9d7b228
CCM-13768: add fetch from s3
Ian-Hodges d6f5cab
CCM-13768: general tidy up
Ian-Hodges 9d702e2
CCM-13768: add file-safe s3 bucket
Ian-Hodges 1dcbd0e
CCM-13768: fix event bus rule filter
Ian-Hodges 528ef46
CCM-13768: add component tests
Ian-Hodges d97cbad
CCM-13768: add fetch from s3
Ian-Hodges d240a41
CCM-13768: attempt to get messageReference for invalid events
Ian-Hodges ad9192f
CCM-13768: remove unnecessary console.log statements
Ian-Hodges b7dc210
CCM-13768: linting fixes
Ian-Hodges 8da6269
CCM-13768: update unit tests
Ian-Hodges b5bf9fc
CCM-13768: general tidy up
Ian-Hodges 30a7612
CCM-13768: restrict access to the queue
Ian-Hodges a9c3aa3
CCM-13768: restrict access to the queue
Ian-Hodges a78c49f
Merge branch 'main' into feature/CCM-13768_print-analyser
Ian-Hodges File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
infrastructure/terraform/components/dl/cloudwatch_event_rule_file_safe.tf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| resource "aws_cloudwatch_event_rule" "file_safe" { | ||
| name = "${local.csi}-file-safe" | ||
| description = "File safe event rule" | ||
| event_bus_name = aws_cloudwatch_event_bus.main.name | ||
|
|
||
| event_pattern = jsonencode({ | ||
| "detail" : { | ||
| "type" : [ | ||
| "uk.nhs.notify.digital.letters.print.file.safe.v1" | ||
| ] | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| resource "aws_cloudwatch_event_target" "file_safe_print_analyser" { | ||
| rule = aws_cloudwatch_event_rule.file_safe.name | ||
| arn = module.sqs_print_analyser.sqs_queue_arn | ||
| event_bus_name = aws_cloudwatch_event_bus.main.name | ||
| } |
10 changes: 10 additions & 0 deletions
10
infrastructure/terraform/components/dl/lambda_event_source_mapping_print_analyser.tf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| resource "aws_lambda_event_source_mapping" "print_analyser" { | ||
| event_source_arn = module.sqs_print_analyser.sqs_queue_arn | ||
| function_name = module.print_analyser.function_name | ||
| batch_size = var.queue_batch_size | ||
| maximum_batching_window_in_seconds = var.queue_batch_window_seconds | ||
|
|
||
| function_response_types = [ | ||
| "ReportBatchItemFailures" | ||
| ] | ||
| } |
99 changes: 99 additions & 0 deletions
99
infrastructure/terraform/components/dl/module_lambda_print_analyser.tf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| module "print_analyser" { | ||
| source = "https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.29/terraform-lambda.zip" | ||
|
|
||
| function_name = "print-analyser" | ||
| description = "A function for processing file safe events" | ||
|
|
||
| aws_account_id = var.aws_account_id | ||
| component = local.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.print_analyser.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 = "print-analyser/dist" | ||
| function_include_common = true | ||
| handler_function_name = "handler" | ||
| runtime = "nodejs22.x" | ||
| memory = 128 | ||
| timeout = 60 | ||
| log_level = var.log_level | ||
|
|
||
| force_lambda_code_deploy = var.force_lambda_code_deploy | ||
| enable_lambda_insights = false | ||
|
|
||
| log_destination_arn = local.log_destination_arn | ||
| log_subscription_role_arn = local.acct.log_subscription_role_arn | ||
|
|
||
| lambda_env_vars = { | ||
| "EVENT_PUBLISHER_EVENT_BUS_ARN" = aws_cloudwatch_event_bus.main.arn | ||
| "EVENT_PUBLISHER_DLQ_URL" = module.sqs_event_publisher_errors.sqs_queue_url | ||
| } | ||
| } | ||
|
|
||
| data "aws_iam_policy_document" "print_analyser" { | ||
| statement { | ||
| sid = "PutEvents" | ||
| effect = "Allow" | ||
|
|
||
| actions = [ | ||
| "events:PutEvents", | ||
| ] | ||
|
|
||
| resources = [ | ||
| aws_cloudwatch_event_bus.main.arn, | ||
| ] | ||
| } | ||
|
|
||
| statement { | ||
| sid = "SQSPermissionsDLQs" | ||
| effect = "Allow" | ||
|
|
||
| actions = [ | ||
| "sqs:SendMessage", | ||
| "sqs:SendMessageBatch", | ||
| ] | ||
|
|
||
| resources = [ | ||
| module.sqs_event_publisher_errors.sqs_queue_arn, | ||
| ] | ||
| } | ||
|
|
||
| statement { | ||
| sid = "SQSPermissionsPrintAnalyserQueue" | ||
| effect = "Allow" | ||
|
|
||
| actions = [ | ||
| "sqs:ReceiveMessage", | ||
| "sqs:DeleteMessage", | ||
| "sqs:GetQueueAttributes", | ||
| "sqs:GetQueueUrl", | ||
| ] | ||
|
|
||
| resources = [ | ||
| module.sqs_print_analyser.sqs_queue_arn, | ||
| ] | ||
| } | ||
|
|
||
| statement { | ||
| sid = "S3PermissionsPrintAnalyserQueue" | ||
| effect = "Allow" | ||
|
|
||
| actions = [ | ||
| "s3:GetObject", | ||
| ] | ||
|
|
||
| resources = [ | ||
| "${module.s3bucket_file_safe.arn}/*", | ||
| ] | ||
| } | ||
| } |
84 changes: 84 additions & 0 deletions
84
infrastructure/terraform/components/dl/module_s3bucket_file_safe.tf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| module "s3bucket_file_safe" { | ||
| source = "https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.24/terraform-s3bucket.zip" | ||
|
|
||
| name = "file-safe" | ||
|
|
||
| aws_account_id = var.aws_account_id | ||
| region = var.region | ||
| project = var.project | ||
| environment = var.environment | ||
| component = local.component | ||
|
|
||
| kms_key_arn = module.kms.key_arn | ||
|
|
||
| policy_documents = [data.aws_iam_policy_document.s3bucket_file_safe.json] | ||
|
|
||
| force_destroy = var.force_destroy | ||
|
|
||
| lifecycle_rules = [ | ||
| { | ||
| enabled = true | ||
|
|
||
| expiration = { | ||
| days = "90" | ||
| } | ||
|
|
||
| noncurrent_version_transition = [ | ||
| { | ||
| noncurrent_days = "30" | ||
| storage_class = "STANDARD_IA" | ||
| } | ||
| ] | ||
|
|
||
| noncurrent_version_expiration = { | ||
| noncurrent_days = "90" | ||
| } | ||
|
|
||
| abort_incomplete_multipart_upload = { | ||
| days = "1" | ||
| } | ||
| } | ||
| ] | ||
| } | ||
|
|
||
| data "aws_iam_policy_document" "s3bucket_file_safe" { | ||
| statement { | ||
| sid = "AllowManagedAccountsToList" | ||
| effect = "Allow" | ||
|
|
||
| actions = [ | ||
| "s3:ListBucket", | ||
| ] | ||
|
|
||
| resources = [ | ||
| module.s3bucket_file_safe.arn, | ||
| ] | ||
|
|
||
| principals { | ||
| type = "AWS" | ||
| identifiers = [ | ||
| "arn:aws:iam::${var.aws_account_id}:root" | ||
| ] | ||
| } | ||
| } | ||
|
|
||
| statement { | ||
| sid = "AllowManagedAccountsToGet" | ||
| effect = "Allow" | ||
|
|
||
| actions = [ | ||
| "s3:GetObject", | ||
| ] | ||
|
|
||
| resources = [ | ||
| "${module.s3bucket_file_safe.arn}/*", | ||
| ] | ||
|
|
||
| principals { | ||
| type = "AWS" | ||
| identifiers = [ | ||
| "arn:aws:iam::${var.aws_account_id}:root" | ||
| ] | ||
| } | ||
| } | ||
| } |
42 changes: 42 additions & 0 deletions
42
infrastructure/terraform/components/dl/module_sqs_print_analyser.tf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| module "sqs_print_analyser" { | ||
| source = "https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.30/terraform-sqs.zip" | ||
|
|
||
| aws_account_id = var.aws_account_id | ||
| component = local.component | ||
| environment = var.environment | ||
| project = var.project | ||
| region = var.region | ||
| name = "print-analyser" | ||
| sqs_kms_key_arn = module.kms.key_arn | ||
| visibility_timeout_seconds = 60 | ||
| delay_seconds = 5 | ||
| create_dlq = true | ||
| max_receive_count = 1 | ||
| sqs_policy_overload = data.aws_iam_policy_document.sqs_print_analyser.json | ||
| } | ||
|
|
||
| data "aws_iam_policy_document" "sqs_print_analyser" { | ||
| statement { | ||
| sid = "AllowEventBridgeToSendMessage" | ||
| effect = "Allow" | ||
|
|
||
| principals { | ||
| type = "Service" | ||
| identifiers = ["events.amazonaws.com"] | ||
| } | ||
|
|
||
| actions = [ | ||
| "sqs:SendMessage" | ||
| ] | ||
|
|
||
| resources = [ | ||
| "arn:aws:sqs:${var.region}:${var.aws_account_id}:${local.csi}-print-analyser-queue" | ||
| ] | ||
|
|
||
| condition { | ||
| test = "ArnEquals" | ||
| variable = "aws:SourceArn" | ||
| values = [aws_cloudwatch_event_rule.file_safe.arn] | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import { baseJestConfig } from '../../jest.config.base'; | ||
|
|
||
| const config = baseJestConfig; | ||
|
|
||
| export default config; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| { | ||
| "dependencies": { | ||
| "aws-lambda": "^1.0.7", | ||
| "digital-letters-events": "^0.0.1", | ||
| "pdf-lib": "^1.17.1", | ||
| "utils": "^0.0.1" | ||
| }, | ||
| "devDependencies": { | ||
| "@tsconfig/node22": "^22.0.2", | ||
| "@types/aws-lambda": "^8.10.155", | ||
| "@types/jest": "^29.5.14", | ||
| "jest": "^29.7.0", | ||
| "jest-mock-extended": "^3.0.7", | ||
| "typescript": "^5.9.3" | ||
| }, | ||
| "name": "nhs-notify-digital-letters-print-analyser", | ||
| "private": true, | ||
| "scripts": { | ||
| "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", | ||
| "lint": "eslint .", | ||
| "lint:fix": "eslint . --fix", | ||
| "test:unit": "jest", | ||
| "typecheck": "tsc --noEmit" | ||
| }, | ||
| "version": "0.0.1" | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.