Skip to content

Commit 9bea46c

Browse files
committed
CCM-12649 Get status healthcheck endpoint
1 parent 54b0fc2 commit 9bea46c

File tree

17 files changed

+315
-9
lines changed

17 files changed

+315
-9
lines changed

infrastructure/terraform/components/api/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ No requirements.
3636
| <a name="module_domain_truststore"></a> [domain\_truststore](#module\_domain\_truststore) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.20/terraform-s3bucket.zip | n/a |
3737
| <a name="module_get_letter_data"></a> [get\_letter\_data](#module\_get\_letter\_data) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.24/terraform-lambda.zip | n/a |
3838
| <a name="module_get_letters"></a> [get\_letters](#module\_get\_letters) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.24/terraform-lambda.zip | n/a |
39+
| <a name="module_get_status"></a> [get\_status](#module\_get\_status) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.24/terraform-lambda.zip | n/a |
3940
| <a name="module_kms"></a> [kms](#module\_kms) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.20/terraform-kms.zip | n/a |
4041
| <a name="module_logging_bucket"></a> [logging\_bucket](#module\_logging\_bucket) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.20/terraform-s3bucket.zip | n/a |
4142
| <a name="module_patch_letter"></a> [patch\_letter](#module\_patch\_letter) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.24/terraform-lambda.zip | n/a |

infrastructure/terraform/components/api/iam_role_api_gateway_execution_role.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ data "aws_iam_policy_document" "api_gateway_execution_policy" {
5151
module.authorizer_lambda.function_arn,
5252
module.get_letters.function_arn,
5353
module.patch_letter.function_arn,
54-
module.get_letter_data.function_arn
54+
module.get_letter_data.function_arn,
55+
module.get_status.function_arn
5556
]
5657
}
5758
}

infrastructure/terraform/components/api/locals.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ locals {
1111
GET_LETTERS_LAMBDA_ARN = module.get_letters.function_arn
1212
GET_LETTER_DATA_LAMBDA_ARN = module.get_letter_data.function_arn
1313
PATCH_LETTER_LAMBDA_ARN = module.patch_letter.function_arn
14+
GET_STATUS_LAMBDA_ARN = module.get_status.function_arn
1415
})
1516

1617
destination_arn = "arn:aws:logs:${var.region}:${var.shared_infra_account_id}:destination:nhs-main-obs-firehose-logs"
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
module "get_status" {
2+
source = "https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.24/terraform-lambda.zip"
3+
4+
function_name = "get_status"
5+
description = "Healthcheck for service"
6+
7+
aws_account_id = var.aws_account_id
8+
component = var.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.get_status_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 = "api-handler/dist"
24+
function_include_common = true
25+
handler_function_name = "getStatus"
26+
runtime = "nodejs22.x"
27+
memory = 128
28+
timeout = 5
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.destination_arn
36+
log_subscription_role_arn = local.acct.log_subscription_role_arn
37+
38+
lambda_env_vars = merge(local.common_lambda_env_vars, {
39+
MAX_LIMIT = var.max_get_limit
40+
})
41+
}
42+
43+
data "aws_iam_policy_document" "get_status_lambda" {
44+
statement {
45+
sid = "KMSPermissions"
46+
effect = "Allow"
47+
48+
actions = [
49+
"kms:Decrypt",
50+
"kms:GenerateDataKey",
51+
]
52+
53+
resources = [
54+
module.kms.key_arn, ## Requires shared kms module
55+
]
56+
}
57+
58+
statement {
59+
sid = "AllowDynamoDBAccess"
60+
effect = "Allow"
61+
62+
actions = [
63+
"dynamodb:DescribeTable"
64+
]
65+
66+
resources = [
67+
aws_dynamodb_table.letters.arn,
68+
"${aws_dynamodb_table.letters.arn}/index/supplierStatus-index"
69+
]
70+
}
71+
72+
73+
statement {
74+
sid = "S3ListBuckets"
75+
actions = ["s3:ListBuckets"]
76+
resources = ["${module.s3bucket_test_letters.arn}/*"]
77+
}
78+
}

infrastructure/terraform/components/api/resources/spec.tmpl.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,39 @@
2323
},
2424
"openapi": "3.0.1",
2525
"paths": {
26+
"/_status": {
27+
"get": {
28+
"operationId": "getStatusId",
29+
"responses": {
30+
"200": {
31+
"description": "Empty body"
32+
},
33+
"500": {
34+
"description": "Server error"
35+
}
36+
},
37+
"security": [
38+
{
39+
"LambdaAuthorizer": []
40+
}
41+
],
42+
"summary": "Healthcheck endpoint",
43+
"x-amazon-apigateway-integration": {
44+
"contentHandling": "CONVERT_TO_TEXT",
45+
"credentials": "${APIG_EXECUTION_ROLE_ARN}",
46+
"httpMethod": "POST",
47+
"passthroughBehavior": "WHEN_NO_TEMPLATES",
48+
"responses": {
49+
".*": {
50+
"statusCode": "200"
51+
}
52+
},
53+
"timeoutInMillis": 29000,
54+
"type": "AWS_PROXY",
55+
"uri": "arn:aws:apigateway:${AWS_REGION}:lambda:path/2015-03-31/functions/${GET_STATUS_LAMBDA_ARN}/invocations"
56+
}
57+
}
58+
},
2659
"/letters": {
2760
"get": {
2861
"description": "Returns 200 OK with paginated letter ids.",
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";
2+
import { DBHealthcheck } from "../healthcheck";
3+
import { createTables, DBContext, deleteTables, setupDynamoDBContainer } from "./db";
4+
5+
// Database tests can take longer, especially with setup and teardown
6+
jest.setTimeout(30000);
7+
8+
describe('DBHealthcheck', () => {
9+
10+
let db: DBContext;
11+
12+
beforeAll(async () => {
13+
db = await setupDynamoDBContainer();
14+
});
15+
16+
beforeEach(async () => {
17+
await createTables(db);
18+
});
19+
20+
afterEach(async () => {
21+
await deleteTables(db);
22+
});
23+
24+
it('passes when the database is available', async () => {
25+
const dbHealthCheck = new DBHealthcheck(db.docClient, db.config);
26+
await dbHealthCheck.check();
27+
});
28+
29+
it('fails when the database is unavailable', async () => {
30+
const realFunction = db.docClient.send;
31+
db.docClient.send = jest.fn().mockImplementation(() => { throw new Error('Failed to send')});
32+
33+
const dbHealthCheck = new DBHealthcheck(db.docClient, db.config);
34+
await expect(dbHealthCheck.check()).rejects.toThrow();
35+
36+
db.docClient.send = realFunction;
37+
});
38+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { DescribeTableCommand } from "@aws-sdk/client-dynamodb";
2+
import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";
3+
import { LetterRepositoryConfig } from "./letter-repository";
4+
5+
export class DBHealthcheck {
6+
constructor(readonly ddbClient: DynamoDBDocumentClient,
7+
readonly config: LetterRepositoryConfig) {}
8+
9+
async check(): Promise<void> {
10+
await this.ddbClient.send(new DescribeTableCommand({
11+
TableName: this.config.lettersTableName}));
12+
}
13+
}

internal/datastore/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './types';
22
export * from './letter-repository';
3+
export * from './healthcheck';
34
export * from './types';

lambdas/api-handler/src/config/__tests__/deps.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import type { Deps } from '../deps';
32

43
describe('createDependenciesContainer', () => {
@@ -24,6 +23,7 @@ describe('createDependenciesContainer', () => {
2423
// Repo client
2524
jest.mock('../../../../../internal/datastore', () => ({
2625
LetterRepository: jest.fn(),
26+
DBHealthcheck: jest.fn()
2727
}));
2828

2929
// Env

lambdas/api-handler/src/config/deps.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import { S3Client } from "@aws-sdk/client-s3";
22
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
33
import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb';
44
import pino from 'pino';
5-
import { LetterRepository } from '../../../../internal/datastore';
5+
import { LetterRepository, DBHealthcheck } from '../../../../internal/datastore';
66
import { envVars, EnvVars } from "../config/env";
77

88
export type Deps = {
99
s3Client: S3Client;
1010
letterRepo: LetterRepository;
11-
logger: pino.Logger,
11+
dbHealthcheck: DBHealthcheck;
12+
logger: pino.Logger;
1213
env: EnvVars
1314
};
1415

@@ -23,13 +24,25 @@ function createLetterRepository(log: pino.Logger, envVars: EnvVars): LetterRepos
2324
return new LetterRepository(docClient, log, config);
2425
}
2526

27+
function createDBHealthcheck(envVars: EnvVars): DBHealthcheck {
28+
const ddbClient = new DynamoDBClient({});
29+
const docClient = DynamoDBDocumentClient.from(ddbClient);
30+
const config = {
31+
lettersTableName: envVars.LETTERS_TABLE_NAME,
32+
ttlHours: envVars.LETTER_TTL_HOURS
33+
};
34+
35+
return new DBHealthcheck(docClient, config);
36+
}
37+
2638
export function createDependenciesContainer(): Deps {
2739

2840
const log = pino();
2941

3042
return {
3143
s3Client: new S3Client(),
3244
letterRepo: createLetterRepository(log, envVars),
45+
dbHealthcheck: createDBHealthcheck(envVars),
3346
logger: log,
3447
env: envVars
3548
};

0 commit comments

Comments
 (0)