Skip to content

Commit 8ffea30

Browse files
CCM-8861: SFTP poll lambda
1 parent d3af6a2 commit 8ffea30

File tree

19 files changed

+400
-17
lines changed

19 files changed

+400
-17
lines changed

infrastructure/terraform/components/app/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,6 @@ variable "letter_suppliers" {
198198
enable_polling = bool
199199
default_supplier = optional(bool)
200200
}))
201-
default = {}
201+
default = {}
202202
description = "Letter suppliers enabled in the environment"
203203
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ No requirements.
4343
| <a name="module_lambda_enrich_guardduty_scan_result"></a> [lambda\_enrich\_guardduty\_scan\_result](#module\_lambda\_enrich\_guardduty\_scan\_result) | ../lambda-function | n/a |
4444
| <a name="module_lambda_send_letter_proof"></a> [lambda\_send\_letter\_proof](#module\_lambda\_send\_letter\_proof) | ../lambda-function | n/a |
4545
| <a name="module_lambda_set_file_virus_scan_status"></a> [lambda\_set\_file\_virus\_scan\_status](#module\_lambda\_set\_file\_virus\_scan\_status) | ../lambda-function | n/a |
46+
| <a name="module_lambda_sftp_poll"></a> [lambda\_sftp\_poll](#module\_lambda\_sftp\_poll) | ../lambda-function | n/a |
4647
| <a name="module_list_template_lambda"></a> [list\_template\_lambda](#module\_list\_template\_lambda) | ../lambda-function | n/a |
4748
| <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 |
4849
| <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 |

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ resource "aws_api_gateway_rest_api" "main" {
44
description = "Templates API"
55
disable_execute_api_endpoint = false
66

7-
binary_media_types = [ "multipart/form-data" ]
7+
binary_media_types = ["multipart/form-data"]
88
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
resource "aws_cloudwatch_event_rule" "sftp_poll" {
2+
name = "${local.csi}-sftp-poll"
3+
schedule_expression = "rate(1 hour)" # Runs at the top of every hour
4+
}
5+
6+
resource "aws_cloudwatch_event_target" "lambda_target" {
7+
rule = aws_cloudwatch_event_rule.sftp_poll.name
8+
arn = module.lambda_sftp_poll.function_name
9+
}
10+
11+
resource "aws_lambda_permission" "allow_cloudwatch" {
12+
statement_id = "AllowExecutionFromCloudWatch"
13+
action = "lambda:InvokeFunction"
14+
function_name = module.lambda_sftp_poll.function_name
15+
principal = "events.amazonaws.com"
16+
source_arn = aws_cloudwatch_event_rule.sftp_poll.arn
17+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ module "build_sftp_letters_lambdas" {
55

66
entrypoints = [
77
"src/send-proof.ts",
8+
"src/sftp-poll.ts",
89
]
910
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module "lambda_send_letter_proof" {
1919
CSI = local.csi
2020
DEFAULT_LETTER_SUPPLIER = local.default_letter_supplier.name
2121
ENVIRONMENT = var.environment
22+
QUARANTINE_BUCKET_NAME = module.s3bucket_quarantine.id
2223
INTERNAL_BUCKET_NAME = module.s3bucket_internal.id
2324
NODE_OPTIONS = "--enable-source-maps",
2425
REGION = var.region
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
module "lambda_sftp_poll" {
2+
source = "../lambda-function"
3+
description = "Lambda to poll the SFTP suppliers and "
4+
5+
function_name = "${local.csi}-sftp-poll"
6+
filename = module.build_sftp_letters_lambdas.zips["src/sftp-poll.ts"].path
7+
source_code_hash = module.build_sftp_letters_lambdas.zips["src/sftp-poll.ts"].base64sha256
8+
handler = "sftp-poll.handler"
9+
10+
log_retention_in_days = var.log_retention_in_days
11+
12+
execution_role_policy_document = data.aws_iam_policy_document.sftp_poll.json
13+
14+
environment_variables = {
15+
CSI = local.csi
16+
ENVIRONMENT = var.environment
17+
QUARANTINE_BUCKET_NAME = module.s3bucket_quarantine.id
18+
INTERNAL_BUCKET_NAME = module.s3bucket_internal.id
19+
DEFAULT_LETTER_SUPPLIER = local.default_letter_supplier.name
20+
SFTP_ENVIRONMENT = local.sftp_environment
21+
REGION = var.region
22+
NODE_OPTIONS = "--enable-source-maps",
23+
}
24+
}
25+
26+
data "aws_iam_policy_document" "sftp_poll" {
27+
statement {
28+
sid = "AllowDynamoAccess"
29+
effect = "Allow"
30+
31+
actions = [
32+
"dynamodb:UpdateItem",
33+
]
34+
35+
resources = [
36+
aws_dynamodb_table.templates.arn,
37+
]
38+
}
39+
40+
statement {
41+
sid = "AllowS3"
42+
effect = "Allow"
43+
44+
actions = [
45+
"s3:PutObject",
46+
]
47+
48+
resources = ["${module.s3bucket_quarantine.arn}/*"]
49+
}
50+
51+
statement {
52+
sid = "AllowSSMParameterRead"
53+
effect = "Allow"
54+
actions = [
55+
"ssm:GetParameter",
56+
]
57+
resources = [
58+
"arn:aws:ssm:${var.region}:${var.aws_account_id}:parameter/${local.csi}/sftp-config/*"
59+
]
60+
}
61+
62+
statement {
63+
sid = "AllowKMSDynamoAccess"
64+
effect = "Allow"
65+
66+
actions = [
67+
"kms:Decrypt",
68+
"kms:DescribeKey",
69+
"kms:Encrypt",
70+
"kms:GenerateDataKey*",
71+
"kms:ReEncrypt*",
72+
]
73+
74+
resources = [
75+
var.kms_key_arn
76+
]
77+
}
78+
}

infrastructure/terraform/modules/lambda-function/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ No modules.
3232
| Name | Description |
3333
|------|-------------|
3434
| <a name="output_function_arn"></a> [function\_arn](#output\_function\_arn) | n/a |
35+
| <a name="output_function_name"></a> [function\_name](#output\_function\_name) | n/a |
3536
<!-- vale on -->
3637
<!-- markdownlint-enable -->
3738
<!-- END_TF_DOCS -->
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
output "function_arn" {
22
value = aws_lambda_function.main.arn
33
}
4+
output "function_name" {
5+
value = aws_lambda_function.main.function_name
6+
}

lambdas/sftp-letters/src/__tests__/infra/sftp-supplier-client-repository.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ describe('getClient', () => {
7070
sftpClient: mockSftpClient,
7171
baseUploadDir: 'upload/dir',
7272
baseDownloadDir: 'download/dir',
73+
name: 'SYNERTEC',
7374
});
7475

7576
expect(cache.get).toHaveBeenCalledWith(credKey);
@@ -121,6 +122,7 @@ describe('getClient', () => {
121122
sftpClient: mockSftpClient,
122123
baseUploadDir: 'upload/dir',
123124
baseDownloadDir: 'download/dir',
125+
name: 'SYNERTEC',
124126
});
125127

126128
expect(cache.get).toHaveBeenCalledWith(credKey);

0 commit comments

Comments
 (0)