Skip to content

Commit 119df02

Browse files
committed
feat(rust): add second example using non-op internal extension, expand README
1 parent 216d625 commit 119df02

File tree

9 files changed

+1594
-8
lines changed

9 files changed

+1594
-8
lines changed

rust-demo/README.md

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
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 an internal extension.
7+
8+
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).
9+
10+
### External extension
11+
12+
An external extension runs as a separate process alongside your function's process. This can be easier to reason about and keep your code simpler,
13+
though it might add additional overhead if you don't actually need an external extension.
14+
15+
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
416
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.
17+
extension will work. We use Lambda Insight extension simply because it is readily available and useful. Note that this may incurs additional billing fees.
618

719
*It is recommended to use the latest [Lambda Insights extension](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Lambda-Insights-extension-versions.html)*
820
```yaml
@@ -14,6 +26,15 @@ extension will work. We use Lambda Insight extension simply because it is readil
1426
- CloudWatchLambdaInsightsExecutionRolePolicy
1527
```
1628
29+
### Internal extension
30+
31+
Alternately, you can register an extension directly from within your Rust process. This adds code complexity but is somewhat lighter weight.
32+
33+
In our case, we use a dummy no-op extension that simply ignores any event that it receives. You can also use an internal extension that has
34+
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).
35+
36+
## Signal handling in the function
37+
1738
In the function, a simple signal handler is added. It will be executed when the lambda runtime receives
1839
a `SIGTERM`、`SIGINT` signal. You can also add more signal types yourself.
1940

@@ -40,7 +61,10 @@ tokio::spawn(async move {
4061
}
4162
});
4263
```
43-
Use the following AWS SAM CLI commands to build and deploy this demo.
64+
65+
## Deploy and Test
66+
67+
Use the following AWS SAM CLI commands from within one of the two examples' subdirectories to build and deploy this demo.
4468

4569
```bash
4670
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/building-rust.html#building-rust-prerequisites
@@ -54,7 +78,7 @@ Take note of the output value of `RustHelloWorldApi`. Use curl to invoke the api
5478
curl "replace this with value of RustHelloWorldApi"
5579
```
5680

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

6084
for example:

rust-demo/template.yaml renamed to rust-demo/rust_app_external_extension/template.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
AWSTemplateFormatVersion: '2010-09-09'
22
Transform: AWS::Serverless-2016-10-31
33
Description: >
4-
rust-graceful-shutdown-demo
4+
rust-graceful-shutdown-demo-external-extension
55
6-
lambda graceful shutdown-demo(rust edition)
6+
lambda graceful shutdown-demo(rust edition with external extension)
77
88
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
99
Globals:
@@ -17,8 +17,8 @@ Resources:
1717
Metadata:
1818
BuildMethod: rust-cargolambda # More info about Cargo Lambda: https://github.com/cargo-lambda/cargo-lambda
1919
Properties:
20-
FunctionName: graceful-shutdown-rust
21-
CodeUri: ./rust_app # Points to dir of Cargo.toml
20+
FunctionName: graceful-shutdown-rust-external-extension
21+
CodeUri: . # Points to dir of Cargo.toml
2222
Handler: bootstrap
2323
Runtime: provided.al2023
2424
Architectures:

0 commit comments

Comments
 (0)