Skip to content

Commit bf2d6e2

Browse files
committed
feat(health-check): add health check route to index handler
Calling __health in path will return version and name of the function deployed.
1 parent 0f7a28c commit bf2d6e2

File tree

6 files changed

+104
-66
lines changed

6 files changed

+104
-66
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "lambda-container-service",
33
"description": "Template to create a aws lambda compatibel function containers",
44
"author": "Balwant",
5-
"version": "0.0.0-development",
5+
"version": "1.0.0",
66
"license": "MIT",
77
"keywords": [
88
"serverless",
@@ -24,8 +24,8 @@
2424
"scripts": {
2525
"start": "tsc --watch --preserveWatchOutput & nodemon dev-server.js -e js -w ./app",
2626
"build": "tsc",
27-
"test": "jest",
28-
"test:watch": "jest --watch",
27+
"test": "jest --coverage",
28+
"test:watch": "jest --watch --coverage",
2929
"test:newman": "node dev-server.js --test",
3030
"lint": "tslint --fix -p ./tsconfig.json",
3131
"prettier": "prettier --write './src/**/*.ts?(x)'",

publish.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/bin/bash
2+
3+
npm version $1 --no-git-tag-version
24
docker build -t lambda-container-service .
35
docker tag lambda-container-service:latest public.ecr.aws/m0q0z2r6/lambda-container-service:$1
46
docker push public.ecr.aws/m0q0z2r6/lambda-container-service:$1

src/index.test.ts

Lines changed: 82 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,11 @@ import {
22
APIGatewayEventRequestContext,
33
APIGatewayProxyEventV2,
44
} from 'aws-lambda';
5+
import { version, name } from '../package.json';
56
import { handler } from '.';
6-
const mockContext: APIGatewayEventRequestContext = {
7-
accountId: 'string',
8-
apiId: 'string',
9-
authorizer: {
10-
jwt: {
11-
claims: {
12-
foo: 'string',
13-
},
14-
scopes: [],
15-
},
16-
},
17-
protocol: '',
18-
domainName: '',
19-
domainPrefix: 'string',
20-
requestId: '',
21-
routeKey: '',
22-
stage: '',
23-
httpMethod: '',
24-
identity: {
25-
accessKey: '',
26-
accountId: '',
27-
apiKey: '',
28-
apiKeyId: '',
29-
caller: '',
30-
clientCert: null,
31-
cognitoAuthenticationProvider: '',
32-
cognitoAuthenticationType: '',
33-
cognitoIdentityId: '',
34-
cognitoIdentityPoolId: '',
35-
principalOrgId: '',
36-
sourceIp: '0.0.0.0',
37-
user: '',
38-
userAgent: '',
39-
userArn: '',
40-
},
41-
path: '',
42-
requestTimeEpoch: 0,
43-
resourceId: '',
44-
resourcePath: '',
45-
};
46-
const mockRequest: APIGatewayProxyEventV2 = {
47-
version: '',
48-
routeKey: '',
49-
rawPath: '',
50-
rawQueryString: '',
51-
headers: {},
52-
requestContext: {
7+
8+
function mockContext(): APIGatewayEventRequestContext {
9+
return {
5310
accountId: 'string',
5411
apiId: 'string',
5512
authorizer: {
@@ -60,27 +17,77 @@ const mockRequest: APIGatewayProxyEventV2 = {
6017
scopes: [],
6118
},
6219
},
20+
protocol: '',
6321
domainName: '',
6422
domainPrefix: 'string',
65-
timeEpoch: 0,
66-
http: {
67-
method: 'string',
68-
path: 'string',
69-
protocol: 'string',
70-
sourceIp: 'string',
71-
userAgent: 'string',
72-
},
7323
requestId: '',
7424
routeKey: '',
7525
stage: '',
76-
time: '',
77-
},
78-
isBase64Encoded: false,
79-
};
26+
httpMethod: '',
27+
identity: {
28+
accessKey: '',
29+
accountId: '',
30+
apiKey: '',
31+
apiKeyId: '',
32+
caller: '',
33+
clientCert: null,
34+
cognitoAuthenticationProvider: '',
35+
cognitoAuthenticationType: '',
36+
cognitoIdentityId: '',
37+
cognitoIdentityPoolId: '',
38+
principalOrgId: '',
39+
sourceIp: '0.0.0.0',
40+
user: '',
41+
userAgent: '',
42+
userArn: '',
43+
},
44+
path: '',
45+
requestTimeEpoch: 0,
46+
resourceId: '',
47+
resourcePath: '',
48+
};
49+
}
50+
51+
function mockRequest(): APIGatewayProxyEventV2 {
52+
return {
53+
version: '',
54+
routeKey: '',
55+
rawPath: '',
56+
rawQueryString: '',
57+
headers: {},
58+
requestContext: {
59+
accountId: 'string',
60+
apiId: 'string',
61+
authorizer: {
62+
jwt: {
63+
claims: {
64+
foo: 'string',
65+
},
66+
scopes: [],
67+
},
68+
},
69+
domainName: '',
70+
domainPrefix: 'string',
71+
timeEpoch: 0,
72+
http: {
73+
method: 'string',
74+
path: 'string',
75+
protocol: 'string',
76+
sourceIp: 'string',
77+
userAgent: 'string',
78+
},
79+
requestId: '',
80+
routeKey: '',
81+
stage: '',
82+
time: '',
83+
},
84+
isBase64Encoded: false,
85+
};
86+
}
8087

8188
describe('Index Handler', () => {
8289
it('Should return success response', async () => {
83-
const response = await handler(mockRequest, mockContext);
90+
const response = await handler(mockRequest(), mockContext());
8491
expect(response).toStrictEqual({
8592
body: JSON.stringify({
8693
hello: 'world',
@@ -92,4 +99,19 @@ describe('Index Handler', () => {
9299
statusCode: 200,
93100
});
94101
});
102+
it('Should return help response when path is __health', async () => {
103+
const req = mockRequest();
104+
req.rawPath = '__health';
105+
const response = await handler(req, mockContext());
106+
expect(response).toStrictEqual({
107+
body: JSON.stringify({
108+
version,
109+
name,
110+
}),
111+
headers: {
112+
'content-type': 'text/json',
113+
},
114+
statusCode: 200,
115+
});
116+
});
95117
});

src/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,25 @@ import {
44
APIGatewayProxyResultV2,
55
} from 'aws-lambda';
66
import Logger from './lib/logger';
7+
import pkg from '../package.json';
78

89
export const handler = async (
910
event: APIGatewayProxyEventV2,
1011
_context: APIGatewayEventRequestContext
1112
): Promise<APIGatewayProxyResultV2> => {
13+
if (event.rawPath === '__health') {
14+
return {
15+
statusCode: 200,
16+
headers: {
17+
'content-type': 'text/json',
18+
},
19+
body: JSON.stringify({
20+
version: pkg.version,
21+
name: pkg.name,
22+
}),
23+
};
24+
}
25+
1226
Logger.debug('This is a log message from handler', event);
1327
return {
1428
statusCode: 200,

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// "sourceMap": true, /* Generates corresponding '.map' file. */
1313
// "outFile": "./", /* Concatenate and emit output to single file. */
1414
"outDir": "app", /* Redirect output structure to the directory. */
15-
"rootDir": "src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
15+
// "rootDir": "src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
1616
// "composite": true, /* Enable project compilation */
1717
// "removeComments": true, /* Do not emit comments to output. */
1818
// "noEmit": true, /* Do not emit outputs. */

0 commit comments

Comments
 (0)