|
| 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 | + |
| 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/) |
0 commit comments