Skip to content

Commit 93fe8da

Browse files
committed
add rust demo for lambda graceful shutdown
1 parent cc0b4d3 commit 93fe8da

File tree

8 files changed

+1208
-0
lines changed

8 files changed

+1208
-0
lines changed

rust-demo/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
*/target

rust-demo/README.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# rust graceful shutdown demo
2+
3+
This folder contains a simple rust function with [CloudWatch Lambda Insight](https://docs.aws.amazon.com/lambda/latest/dg/monitoring-insights.html) enabled. CloudWatch Lambda Insight is
4+
monitoring and troubleshooting solution for serverless application. Its agent is an external extension. Any external
5+
extension will work. We use Lambda Insight extension simply because it is readily available.
6+
7+
*It is recommended to use the latest [Lambda Insights extension](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Lambda-Insights-extension-versions.html)*
8+
```yaml
9+
Layers:
10+
# 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" # Add Lambda Insight Extension
12+
Policies:
13+
# Add IAM Permission for Lambda Insight Extension
14+
- CloudWatchLambdaInsightsExecutionRolePolicy
15+
```
16+
17+
In the function, a simple signal handler is added. It will be executed when the lambda runtime receives
18+
a `SIGTERM`、`SIGINT` signal. You can also add more signal types yourself.
19+
20+
```rust
21+
// Handle SIGTERM signal:
22+
// https://tokio.rs/tokio/topics/shutdown
23+
// https://rust-cli.github.io/book/in-depth/signals.html
24+
tokio::spawn(async move {
25+
let mut sigint = signal(SignalKind::interrupt()).unwrap();
26+
let mut sigterm = signal(SignalKind::terminate()).unwrap();
27+
tokio::select! {
28+
_sigint = sigint.recv() => {
29+
println!("[runtime] SIGINT received");
30+
println!("[runtime] Graceful shutdown in progress ...");
31+
println!("[runtime] Graceful shutdown completed");
32+
std::process::exit(0);
33+
},
34+
_sigterm = sigterm.recv()=> {
35+
println!("[runtime] SIGTERM received");
36+
println!("[runtime] Graceful shutdown in progress ...");
37+
println!("[runtime] Graceful shutdown completed");
38+
std::process::exit(0);
39+
},
40+
}
41+
});
42+
```
43+
Use the following AWS SAM CLI commands to build and deploy this demo.
44+
45+
```bash
46+
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/building-rust.html#building-rust-prerequisites
47+
sam build --beta-features
48+
sam deploy
49+
```
50+
51+
Take note of the output value of `RustHelloWorldApi`. Use curl to invoke the api and trigger the lambda function at least once.
52+
53+
```bash
54+
curl "replace this with value of RustHelloWorldApi"
55+
```
56+
57+
Waite for several minutes, check the function's log messages in CloudWatch. If you see a log line containing "SIGTERM
58+
received", it works!
59+
60+
for example:
61+
![](./docs/images/rust-2023-12-15.png)
62+
```text
63+
2023-12-15T13:28:12.634+08:00 INIT_START Runtime Version: provided:al2023.v10 Runtime Version ARN: arn:aws:lambda:us-east-1::runtime:389fcaae1b213b40d38ed791dfb615af1a71a32d6996ff7c4afdde3d5af4b6f2
64+
2023-12-15T13:28:12.690+08:00 LOGS Name: cloudwatch_lambda_agent State: Subscribed Types: [Platform]
65+
2023-12-15T13:28:12.715+08:00 EXTENSION Name: cloudwatch_lambda_agent State: Ready Events: [INVOKE, SHUTDOWN]
66+
2023-12-15T13:28:12.717+08:00 START RequestId: ca7dfb1a-9b58-4a07-ba4d-feeea6d61f80 Version: $LATEST
67+
2023-12-15T13:28:12.850+08:00 END RequestId: ca7dfb1a-9b58-4a07-ba4d-feeea6d61f80
68+
2023-12-15T13:28:12.850+08:00 REPORT RequestId: ca7dfb1a-9b58-4a07-ba4d-feeea6d61f80 Duration: 133.17 ms Billed Duration: 215 ms Memory Size: 128 MB Max Memory Used: 26 MB Init Duration: 81.74 ms
69+
2023-12-15T13:28:46.863+08:00 START RequestId: 7e16e208-7b23-44ec-8ce0-793210971ed6 Version: $LATEST
70+
2023-12-15T13:28:46.869+08:00 END RequestId: 7e16e208-7b23-44ec-8ce0-793210971ed6
71+
2023-12-15T13:28:46.869+08:00 REPORT RequestId: 7e16e208-7b23-44ec-8ce0-793210971ed6 Duration: 6.54 ms Billed Duration: 7 ms Memory Size: 128 MB Max Memory Used: 26 MB
72+
2023-12-15T13:34:54.526+08:00 [runtime] SIGTERM received
73+
2023-12-15T13:34:54.526+08:00 [runtime] Graceful shutdown in progress ...
74+
2023-12-15T13:34:54.526+08:00 [runtime] Graceful shutdown completed
75+
```
76+
77+
78+
## Tested Runtimes
79+
80+
| language version | Identifier | Operating system | Architectures | Support status |
81+
|------------------|-----------------|-------------------|------------------|----------------|
82+
| rust | provided.al2023 | Amazon Linux 2023 | arm64<br/>x86_64 | ✅Support |
83+
| rust | provided.al2 | Amazon Linux 2 | arm64<br/>x86_64 | ✅Support |
84+
85+
> **Note**: The [Rust runtime client](https://github.com/awslabs/aws-lambda-rust-runtime)
86+
is an experimental package. It is subject to change and intended only for evaluation purposes.
87+
88+
> **Note**: Make sure your [SAM CLI version](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html) is the latest version,SAM CLI version 1.103.0 or newer is recommended.
89+
90+
91+
## Reference:
92+
- [Building Lambda functions with Rust](https://docs.aws.amazon.com/lambda/latest/dg/lambda-rust.html)
93+
- [AWS SAM Documentation](https://docs.aws.amazon.com/serverless-application-model/)
94+
- [Building Rust Lambda functions with Cargo Lambda](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/building-rust.html)
95+
- [cargo-lambda](https://www.cargo-lambda.info/)
420 KB
Loading

rust-demo/events/event.json

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"body": "hello world",
3+
"resource": "/{proxy+}",
4+
"path": "/path/to/resource",
5+
"httpMethod": "POST",
6+
"isBase64Encoded": false,
7+
"queryStringParameters": {
8+
"foo": "bar"
9+
},
10+
"pathParameters": {
11+
"proxy": "/path/to/resource"
12+
},
13+
"stageVariables": {
14+
"baz": "qux"
15+
},
16+
"headers": {
17+
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
18+
"Accept-Encoding": "gzip, deflate, sdch",
19+
"Accept-Language": "en-US,en;q=0.8",
20+
"Cache-Control": "max-age=0",
21+
"CloudFront-Forwarded-Proto": "https",
22+
"CloudFront-Is-Desktop-Viewer": "true",
23+
"CloudFront-Is-Mobile-Viewer": "false",
24+
"CloudFront-Is-SmartTV-Viewer": "false",
25+
"CloudFront-Is-Tablet-Viewer": "false",
26+
"CloudFront-Viewer-Country": "US",
27+
"Host": "1234567890.execute-api.us-east-1.amazonaws.com",
28+
"Upgrade-Insecure-Requests": "1",
29+
"User-Agent": "Custom User Agent String",
30+
"Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)",
31+
"X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==",
32+
"X-Forwarded-For": "127.0.0.1, 127.0.0.2",
33+
"X-Forwarded-Port": "443",
34+
"X-Forwarded-Proto": "https"
35+
},
36+
"requestContext": {
37+
"accountId": "123456789012",
38+
"resourceId": "123456",
39+
"stage": "prod",
40+
"requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
41+
"requestTime": "09/Apr/2015:12:34:56 +0000",
42+
"requestTimeEpoch": 1428582896000,
43+
"identity": {
44+
"cognitoIdentityPoolId": null,
45+
"accountId": null,
46+
"cognitoIdentityId": null,
47+
"caller": null,
48+
"accessKey": null,
49+
"sourceIp": "127.0.0.1",
50+
"cognitoAuthenticationType": null,
51+
"cognitoAuthenticationProvider": null,
52+
"userArn": null,
53+
"userAgent": "Custom User Agent String",
54+
"user": null
55+
},
56+
"path": "/prod/path/to/resource",
57+
"resourcePath": "/{proxy+}",
58+
"httpMethod": "POST",
59+
"apiId": "1234567890",
60+
"protocol": "HTTP/1.1"
61+
}
62+
}

0 commit comments

Comments
 (0)