Skip to content

Commit 17bee0e

Browse files
authored
Merge pull request #31 from NHSDigital/AMB-1708-status-endpoint
AMB-1708 Adding status endpoint to back end lambda
2 parents 6af6b37 + e2bf018 commit 17bee0e

File tree

5 files changed

+32
-20
lines changed

5 files changed

+32
-20
lines changed

lambda_typescript/src/index.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
import { DynamoDB } from 'aws-sdk';
22

3+
34
const dynamoDB = new DynamoDB.DocumentClient();
45

56
export const handler = async (event: any): Promise<any> => {
7+
68
const operation = event.httpMethod;
79
const dynamoDBTableName = process.env.DYNAMODB_TABLE_NAME || 'DefaultTableName';
8-
9-
if (operation === 'POST' && event.path === '/event') {
10+
11+
if (event.path === '/_status' && operation === 'GET') {
12+
const statusResponse = {
13+
status: 'healthy',
14+
message: 'Backend is up and running.',
15+
};
16+
return {
17+
statusCode: 200,
18+
body: JSON.stringify(statusResponse),
19+
};
20+
} else if (operation === 'POST' && event.path === '/event') {
1021
// Handle the POST request to create a new item
1122
const params = {
1223
TableName: dynamoDBTableName,
1324
Item: JSON.parse(event.body),
1425
};
15-
1626
try {
1727
await dynamoDB.put(params).promise();
1828
return {
@@ -34,7 +44,6 @@ export const handler = async (event: any): Promise<any> => {
3444
id: event.queryStringParameters.id,
3545
},
3646
};
37-
3847
try {
3948
const data = await dynamoDB.get(params).promise();
4049
if (!data.Item) {
@@ -63,7 +72,6 @@ export const handler = async (event: any): Promise<any> => {
6372
id: event.queryStringParameters.id,
6473
},
6574
};
66-
6775
try {
6876
const data = await dynamoDB.get(params).promise();
6977
if (!data.Item) {
@@ -87,7 +95,6 @@ export const handler = async (event: any): Promise<any> => {
8795
};
8896
}
8997
}
90-
9198
return {
9299
statusCode: 400,
93100
body: JSON.stringify('Invalid operation.'),

proxies/live/apiproxy/policies/ServiceCallout.CallHealthcheckEndpoint.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<!-- <Path>/_ping</Path>-->
1717
<!-- </HTTPTargetConnection>-->
1818
<HTTPTargetConnection>
19-
<URL>http://mocktarget.apigee.net</URL>
19+
<URL>{{ DOMAIN_ENDPOINT }}/_status</URL>
2020
<Properties>
2121
<Property name="supports.http10">true</Property>
2222
<Property name="request.retain.headers">User-Agent,Referer,Accept-Language</Property>

proxies/live/apiproxy/proxies/default.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</Step>
77
</Request>
88
</PreFlow>
9-
9+
1010
<Flows>
1111
<Flow name="OptionsPreFlight">
1212
<Request/>

terraform/api_gateway/api.tf

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,15 @@ resource "aws_apigatewayv2_domain_name" "service_api_domain_name" {
3939
endpoint_type = "REGIONAL"
4040
security_policy = "TLS_1_2"
4141
}
42-
4342
tags = {
4443
Name = "${var.prefix}-api-domain-name"
4544
}
4645
}
47-
4846
resource "aws_apigatewayv2_api_mapping" "api_mapping" {
4947
api_id = aws_apigatewayv2_api.service_api.id
5048
domain_name = aws_apigatewayv2_domain_name.service_api_domain_name.id
5149
stage = aws_apigatewayv2_stage.default.id
5250
}
53-
5451
resource "aws_lambda_permission" "api_gw" {
5552
statement_id = "AllowExecutionFromAPIGateway"
5653
action = "lambda:InvokeFunction"
@@ -86,6 +83,15 @@ resource "aws_route53_record" "api_domain" {
8683
zone_id = aws_apigatewayv2_domain_name.service_api_domain_name.domain_name_configuration[0].hosted_zone_id
8784
}
8885
}
86+
data "aws_lambda_function" "status_lambda" {
87+
function_name = var.lambda_name
88+
}
89+
resource "aws_apigatewayv2_integration" "status_integration" {
90+
api_id = aws_apigatewayv2_api.service_api.id
91+
integration_uri = data.aws_lambda_function.status_lambda.invoke_arn
92+
integration_type = "AWS_PROXY"
93+
integration_method = "POST"
94+
}
8995

9096
output "service_domain_name" {
9197
value = aws_apigatewayv2_api_mapping.api_mapping.domain_name

tests/test_lambda.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
"""
2-
See
3-
https://github.com/NHSDigital/pytest-nhsd-apim/blob/main/tests/test_examples.py
4-
for more ideas on how to test the authorization of your API.
5-
"""
61
import random
72
import time
8-
93
import pytest
104
import requests
115
from assertpy import assert_that
@@ -18,7 +12,9 @@ def lambda_crud(url, headers):
1812

1913
request_payload = {
2014
"id": event_id,
21-
"message": "Hello World"}
15+
"message": "Hello World"
16+
}
17+
2218
post_response = requests.post(
2319
url=f"{url}/event",
2420
headers=headers,
@@ -61,8 +57,11 @@ def test_lambda_crud_nhs_login(nhsd_apim_proxy_url, nhsd_apim_auth_headers):
6157

6258

6359
@pytest.mark.smoketest
64-
@pytest.mark.debug
65-
@pytest.mark.nhsd_apim_authorization({"access": "application", "level": "level3"})
60+
@pytest.mark.nhsd_apim_authorization(
61+
{
62+
"access": "application",
63+
"level": "level3"
64+
})
6665
def test_lambda_crud_app_restricted(nhsd_apim_proxy_url, nhsd_apim_auth_headers):
6766
"""
6867
Test for the POST,GET and Delete for Lambda endpoints. Using app-restricted

0 commit comments

Comments
 (0)