You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: rust-demo/README.md
+41-7Lines changed: 41 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,32 @@
1
1
# rust graceful shutdown demo
2
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
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
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
4
28
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.
6
30
7
31
*It is recommended to use the latest [Lambda Insights extension](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Lambda-Insights-extension-versions.html)*
8
32
```yaml
@@ -14,8 +38,10 @@ extension will work. We use Lambda Insight extension simply because it is readil
14
38
- CloudWatchLambdaInsightsExecutionRolePolicy
15
39
```
16
40
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
+
19
45
20
46
```rust
21
47
// Handle SIGTERM signal:
@@ -28,19 +54,24 @@ tokio::spawn(async move {
28
54
_sigint = sigint.recv() => {
29
55
println!("[runtime] SIGINT received");
30
56
println!("[runtime] Graceful shutdown in progress ...");
0 commit comments