Skip to content

Commit ff5c9a0

Browse files
committed
feat(rust): add second example using non-op internal extension with new lambda-runtime helper, expand README, clean up cargo imports
1 parent 216d625 commit ff5c9a0

File tree

10 files changed

+1757
-163
lines changed

10 files changed

+1757
-163
lines changed

rust-demo/README.md

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,32 @@
11
# rust graceful shutdown demo
22

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
3+
## Generating graceful shutdown signals
4+
5+
In order for Lambda to support graceful shutdown, at least one extension must be registered for your function.
6+
This folder contains two examples demonstrating this. One uses an external extension, and one uses Rust `lambda-runtime` crate's
7+
[`spawn_graceful_shutdown_handler() helper`] function,
8+
which is backed by an internal extension.
9+
10+
For more information on the difference between the two, see [these Lambda Extensions API docs](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-extensions-api.html).
11+
12+
### Internal extension
13+
14+
The simplest way to enable shutdown signals in a `Rust` lambda is via [`spawn_graceful_shutdown_handler() helper`] function in the `lambda-runtime`. Under the hood, this registers an internal extension from wtihin your handler process.
15+
16+
The registered extension is a dummy no-op extension that doesn't subscribe to any events. This is very lightweight since it spawns a `tokio` task that keeps open a long-running connection with the Lambda execution environment, that never receives data. It therefore is essentially never woken and just occupies a small amount of heap memory.
17+
18+
The helper also accepts a callback that includes the logic to fire on `SIGTERM` or `SIGINT`, and generates the boilerplate to react to those signals for us.
19+
20+
You can also manually implement your own internal extension registration, if you want an internal extension that has
21+
useful functionality. For instance, see this example of an internal extension that flushes telemetry: [ref](https://github.com/awslabs/aws-lambda-rust-runtime/blob/main/examples/extension-internal-flush). In that case, you could still use the helper, or you could also directly spawn signal handlers as demonstrated in the [Signal handling in the function](#signal-handling-in-the-function).
22+
23+
### External extension
24+
25+
Alternately, you can receive shutdown signals if an external extension is registered with the runtime. An external extension runs as a separate process alongside your function's process. This does not require code changes in your function handler (besides signal handling logic), but it might add additional overhead if you don't actually need an external extension.
26+
27+
The external extension example assumes that the [CloudWatch Lambda Insight](https://docs.aws.amazon.com/lambda/latest/dg/monitoring-insights.html) is enabled. CloudWatch Lambda Insight is a
428
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.
29+
extension will work. We use Lambda Insight extension simply because it is readily available and useful. Note that this may incurs additional billing fees.
630

731
*It is recommended to use the latest [Lambda Insights extension](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Lambda-Insights-extension-versions.html)*
832
```yaml
@@ -14,8 +38,10 @@ extension will work. We use Lambda Insight extension simply because it is readil
1438
- CloudWatchLambdaInsightsExecutionRolePolicy
1539
```
1640
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.
41+
## Signal handling in the function
42+
43+
Inside our external extension example, or inside the [`spawn_graceful_shutdown_handler() helper`], a simple signal handler is added. It will be executed when the lambda runtime receives a `SIGTERM`、`SIGINT` signal. You can customize the logic that will fire when one of the signals is received.
44+
1945

2046
```rust
2147
// Handle SIGTERM signal:
@@ -28,19 +54,24 @@ tokio::spawn(async move {
2854
_sigint = sigint.recv() => {
2955
println!("[runtime] SIGINT received");
3056
println!("[runtime] Graceful shutdown in progress ...");
57+
// additional logic
3158
println!("[runtime] Graceful shutdown completed");
3259
std::process::exit(0);
3360
},
3461
_sigterm = sigterm.recv()=> {
3562
println!("[runtime] SIGTERM received");
3663
println!("[runtime] Graceful shutdown in progress ...");
64+
// additional logic
3765
println!("[runtime] Graceful shutdown completed");
3866
std::process::exit(0);
3967
},
4068
}
4169
});
4270
```
43-
Use the following AWS SAM CLI commands to build and deploy this demo.
71+
72+
## Deploy and Test
73+
74+
Use the following AWS SAM CLI commands from within one of the two examples' subdirectories to build and deploy this demo.
4475

4576
```bash
4677
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/building-rust.html#building-rust-prerequisites
@@ -54,7 +85,7 @@ Take note of the output value of `RustHelloWorldApi`. Use curl to invoke the api
5485
curl "replace this with value of RustHelloWorldApi"
5586
```
5687

57-
Waite for several minutes, check the function's log messages in CloudWatch. If you see a log line containing "SIGTERM
88+
Wait for several minutes, check the function's log messages in CloudWatch. If you see a log line containing "SIGTERM
5889
received", it works!
5990

6091
for example:
@@ -92,4 +123,7 @@ is an experimental package. It is subject to change and intended only for evalua
92123
- [Building Lambda functions with Rust](https://docs.aws.amazon.com/lambda/latest/dg/lambda-rust.html)
93124
- [AWS SAM Documentation](https://docs.aws.amazon.com/serverless-application-model/)
94125
- [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/)
126+
- [cargo-lambda](https://www.cargo-lambda.info/)
127+
128+
129+
[`spawn_graceful_shutdown_handler() helper`]: https://docs.rs/lambda_runtime/latest/lambda_runtime/fn.spawn_graceful_shutdown_handler.html

rust-demo/rust_app/Cargo.toml

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)