Skip to content

Commit 9d94d5c

Browse files
committed
update nodejs demo
1 parent 1ffc4d4 commit 9d94d5c

File tree

5 files changed

+98
-25
lines changed

5 files changed

+98
-25
lines changed

nodejs-demo/README.md

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,32 @@
22

33
This folder contains a simple node.js function with CloudWatch Lambda Insight enabled. CloudWatch Lambda Insight is monitoring and troubleshooting solution for serverless applicaiton. Its agent is an external extension. Any external extension will work. We use Lambda Insight extension simply because it is readily available.
44

5+
*It is recommended to use the
6+
latest [Lambda Insights extension](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Lambda-Insights-extension-versions.html)*
57
```yaml
68
Properties:
79
Layers:
8-
- !Sub "arn:aws:lambda:${AWS::Region}:580247275435:layer:LambdaInsightsExtension:14" # Add Lambda Insight Extension
10+
# Add Lambda Insight Extension: https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Lambda-Insights-extension-versions.html
11+
- !Sub "arn:aws:lambda:${AWS::Region}:580247275435:layer:LambdaInsightsExtension-Arm64:5"
912
Policies:
10-
- CloudWatchLambdaInsightsExecutionRolePolicy # Add IAM Permission for Lambda Insight Extension
13+
# Add IAM Permission for Lambda Insight Extension
14+
- CloudWatchLambdaInsightsExecutionRolePolicy
1115
```
1216
13-
In the function, a simple SIGTERM signal handler is added. It will be executed when the lambda runtime receives a SIGTERM signal.
17+
In the function, a simple `SIGTERM` signal handler is added. It will be executed when the lambda runtime receives a `SIGTERM` signal.
1418

1519
```javascript
20+
// Static initialization
21+
// SIGTERM Handler: https://docs.aws.amazon.com/lambda/latest/operatorguide/static-initialization.html
22+
// Listening for os signals that can be handled,reference: https://docs.aws.amazon.com/lambda/latest/dg/runtimes-extensions-api.html
23+
// Termination Signals: https://www.gnu.org/software/libc/manual/html_node/Termination-Signals.html
1624
process.on('SIGTERM', async () => {
1725
console.info('[runtime] SIGTERM received');
1826
1927
console.info('[runtime] cleaning up');
2028
// perform actual clean up work here.
2129
await new Promise(resolve => setTimeout(resolve, 200));
22-
30+
2331
console.info('[runtime] exiting');
2432
process.exit(0)
2533
});
@@ -32,21 +40,40 @@ sam build --use-container
3240
sam deploy --guided
3341
```
3442

35-
Take note of the output value of HelloWorldApi. Use curl to invoke the api and trigger the lambda function once.
43+
Take note of the output value of HelloWorldApi. Use curl to invoke the api and trigger the lambda function at least once.
3644

3745
```bash
3846
curl "replace this with value of HelloWorldApi"
3947
```
4048

4149
Waite for serveral minutes, check the function's log messages in CloudWatch. If you see a log line containing "SIGTERM received", it works!
4250

51+
52+
for example:
53+
![](./docs/images/nodejs-2024-01-08.png)
54+
```text
55+
2023-12-15T14:03:59.046+08:00 INIT_START Runtime Version: provided:al2023.v10 Runtime Version ARN: arn:aws:lambda:us-east-1::runtime:389fcaae1b213b40d38ed791dfb615af1a71a32d6996ff7c4afdde3d5af4b6f2
56+
2023-12-15T14:03:59.104+08:00 LOGS Name: cloudwatch_lambda_agent State: Subscribed Types: [Platform]
57+
2023-12-15T14:03:59.173+08:00 EXTENSION Name: cloudwatch_lambda_agent State: Ready Events: [INVOKE, SHUTDOWN]
58+
2023-12-15T14:03:59.175+08:00 START RequestId: 90d93089-6e10-45f2-81f2-3a640945976b Version: $LATEST
59+
2023-12-15T14:03:59.230+08:00 END RequestId: 90d93089-6e10-45f2-81f2-3a640945976b
60+
2023-12-15T14:03:59.230+08:00 REPORT RequestId: 90d93089-6e10-45f2-81f2-3a640945976b Duration: 55.01 ms Billed Duration: 183 ms Memory Size: 128 MB Max Memory Used: 31 MB Init Duration: 127.75 ms
61+
2023-12-15T14:04:07.275+08:00 START RequestId: 89827818-1fdf-4626-a0eb-4cb509171c29 Version: $LATEST
62+
2023-12-15T14:04:07.330+08:00 END RequestId: 89827818-1fdf-4626-a0eb-4cb509171c29
63+
2023-12-15T14:04:07.330+08:00 REPORT RequestId: 89827818-1fdf-4626-a0eb-4cb509171c29 Duration: 55.80 ms Billed Duration: 56 ms Memory Size: 128 MB Max Memory Used: 31 MB
64+
2023-12-15T14:09:35.620+08:00 [runtime] SIGTERM received
65+
2023-12-15T14:09:35.620+08:00 [runtime] Graceful shutdown in progress ...
66+
2023-12-15T14:09:35.620+08:00 [runtime] Graceful shutdown completed
4367
```
44-
2021/07/27/[$LATEST]0a35efaafbd24ecc9a5f4fad2dd94b49 2021-07-27T03:47:27.635000 START RequestId: 1aac889c-ccaf-4655-9ad1-018e464ab75d Version: $LATEST
45-
2021/07/27/[$LATEST]0a35efaafbd24ecc9a5f4fad2dd94b49 2021-07-27T03:47:27.789000 LOGS Name: cloudwatch_lambda_agent State: Subscribed Types: [platform]
46-
2021/07/27/[$LATEST]0a35efaafbd24ecc9a5f4fad2dd94b49 2021-07-27T03:47:27.789000 EXTENSION Name: cloudwatch_lambda_agent State: Ready Events: [INVOKE,SHUTDOWN]
47-
2021/07/27/[$LATEST]0a35efaafbd24ecc9a5f4fad2dd94b49 2021-07-27T03:47:27.880000 END RequestId: 1aac889c-ccaf-4655-9ad1-018e464ab75d
48-
2021/07/27/[$LATEST]0a35efaafbd24ecc9a5f4fad2dd94b49 2021-07-27T03:47:27.880000 REPORT RequestId: 1aac889c-ccaf-4655-9ad1-018e464ab75d Duration: 90.73 ms Billed Duration: 91 ms Memory Size: 128 MB Max Memory Used: 81 MB Init Duration: 232.27 ms
49-
2021/07/27/[$LATEST]0a35efaafbd24ecc9a5f4fad2dd94b49 2021-07-27T03:53:27.681000 2021-07-27T03:53:27.661Z 1aac889c-ccaf-4655-9ad1-018e464ab75d INFO [runtime] SIGTERM received
50-
2021/07/27/[$LATEST]0a35efaafbd24ecc9a5f4fad2dd94b49 2021-07-27T03:53:27.681000 2021-07-27T03:53:27.681Z 1aac889c-ccaf-4655-9ad1-018e464ab75d INFO [runtime] cleaning up
51-
2021/07/27/[$LATEST]0a35efaafbd24ecc9a5f4fad2dd94b49 2021-07-27T03:53:27.882000 2021-07-27T03:53:27.882Z 1aac889c-ccaf-4655-9ad1-018e464ab75d INFO [runtime] exiting
52-
```
68+
69+
## Tested Runtimes
70+
71+
| language version | Identifier | Operating system | Architectures | Support status |
72+
|------------------|------------|-------------------|------------------|----------------|
73+
| Node.js 20 | nodejs20.x | Amazon Linux 2023 | arm64<br/>x86_64 | ✅Support |
74+
| Node.js 18 | nodejs18.x | Amazon Linux 2 | arm64<br/>x86_64 | ✅Support |
75+
76+
## Reference:
77+
78+
- [Building Lambda functions with nodejs](https://docs.aws.amazon.com/lambda/latest/dg/lambda-nodejs.html)
79+
- [AWS SAM Documentation](https://docs.aws.amazon.com/serverless-application-model/)
403 KB
Loading

nodejs-demo/hello-world/app.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
1+
const os = require("os");
12

2-
// SIGTERM Handler
3+
// Static initialization
4+
// SIGTERM Handler: https://docs.aws.amazon.com/lambda/latest/operatorguide/static-initialization.html
5+
// Listening for os signals that can be handled,reference: https://docs.aws.amazon.com/lambda/latest/dg/runtimes-extensions-api.html
6+
// Termination Signals: https://www.gnu.org/software/libc/manual/html_node/Termination-Signals.html
37
process.on('SIGTERM', async () => {
48
console.info('[runtime] SIGTERM received');
59

610
console.info('[runtime] cleaning up');
711
// perform actual clean up work here.
812
await new Promise(resolve => setTimeout(resolve, 200));
9-
13+
1014
console.info('[runtime] exiting');
1115
process.exit(0)
1216
});
1317

1418
exports.lambdaHandler = async (event, context) => {
1519
let response = {
16-
'statusCode': 200,
17-
'body': JSON.stringify({
18-
message: 'hello world',
19-
})
20-
}
20+
'statusCode': 200,
21+
'body': JSON.stringify({
22+
"message": 'hello nodejs',
23+
"source ip": event['requestContext']['identity']['sourceIp'],
24+
"architecture": os.arch(),
25+
"operating system": os.type(),
26+
"node version ": process.version,
27+
})
28+
}
2129

2230
return response
2331
};

nodejs-demo/samconfig.toml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# More information about the configuration file can be found here:
2+
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html
3+
version = 0.1
4+
5+
[default]
6+
[default.global.parameters]
7+
stack_name = "nodejs-graceful-shutdown-demo"
8+
9+
[default.build.parameters]
10+
cached = true
11+
parallel = true
12+
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-build.html
13+
use_container = true
14+
15+
[default.validate.parameters]
16+
lint = true
17+
18+
[default.deploy.parameters]
19+
capabilities = "CAPABILITY_IAM"
20+
confirm_changeset = true
21+
resolve_s3 = true
22+
23+
[default.package.parameters]
24+
resolve_s3 = true
25+
26+
[default.sync.parameters]
27+
watch = true
28+
29+
[default.local_start_api.parameters]
30+
warm_containers = "EAGER"
31+
32+
[default.local_start_lambda.parameters]
33+
warm_containers = "EAGER"

nodejs-demo/template.yaml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
AWSTemplateFormatVersion: '2010-09-09'
22
Transform: AWS::Serverless-2016-10-31
33
Description: >
4-
lambda-graceful-shutdown-demo
4+
nodejs-graceful-shutdown-demo
55
66
Sample SAM Template for lambda-graceful-shutdown-demo
77
@@ -13,13 +13,18 @@ Resources:
1313
HelloWorldFunction:
1414
Type: AWS::Serverless::Function
1515
Properties:
16+
FunctionName: graceful-shutdown-nodejs
1617
CodeUri: hello-world/
1718
Handler: app.lambdaHandler
18-
Runtime: nodejs14.x
19+
Runtime: nodejs20.x
20+
Architectures:
21+
- arm64
1922
Layers:
20-
- !Sub "arn:aws:lambda:${AWS::Region}:580247275435:layer:LambdaInsightsExtension:14" # Add Lambda Insight Extension
23+
# Add Lambda Insight Extension: https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Lambda-Insights-extension-versions.html
24+
- !Sub "arn:aws:lambda:${AWS::Region}:580247275435:layer:LambdaInsightsExtension-Arm64:5"
2125
Policies:
22-
- CloudWatchLambdaInsightsExecutionRolePolicy # Add IAM Permission for Lambda Insight Extension
26+
# Add IAM Permission for Lambda Insight Extension
27+
- CloudWatchLambdaInsightsExecutionRolePolicy
2328
Events:
2429
HelloWorld:
2530
Type: Api

0 commit comments

Comments
 (0)