|
| 1 | +# golang graceful shutdown demo |
| 2 | + |
| 3 | +This folder contains a simple golang function |
| 4 | +with [CloudWatch Lambda Insight](https://docs.aws.amazon.com/lambda/latest/dg/monitoring-insights.html) enabled. |
| 5 | +CloudWatch Lambda Insight is |
| 6 | +monitoring and troubleshooting solution for serverless application. Its agent is an external extension. Any external |
| 7 | +extension will work. We use Lambda Insight extension simply because it is readily available. |
| 8 | + |
| 9 | +*It is recommended to use the |
| 10 | +latest [Lambda Insights extension](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Lambda-Insights-extension-versions.html)* |
| 11 | + |
| 12 | +```yaml |
| 13 | + Layers: |
| 14 | + # Add Lambda Insight Extension: https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Lambda-Insights-extension-versions.html |
| 15 | + - !Sub "arn:aws:lambda:${AWS::Region}:580247275435:layer:LambdaInsightsExtension-Arm64:5" |
| 16 | + Policies: |
| 17 | + # Add IAM Permission for Lambda Insight Extension |
| 18 | + - CloudWatchLambdaInsightsExecutionRolePolicy |
| 19 | +``` |
| 20 | +
|
| 21 | +In the function, a simple signal handler is added. It will be executed when the lambda runtime receives a |
| 22 | +`SIGTERM` signal or other signal. You can also add more signal types yourself. |
| 23 | + |
| 24 | +```go |
| 25 | +// Static initialization |
| 26 | +// SIGTERM Handler: https://docs.aws.amazon.com/lambda/latest/operatorguide/static-initialization.html |
| 27 | +func init() { |
| 28 | + // Create a chan to receive os signal |
| 29 | + var c = make(chan os.Signal) |
| 30 | + // Listening for os signals that can be handled,reference: https://docs.aws.amazon.com/lambda/latest/dg/runtimes-extensions-api.html |
| 31 | + // Termination Signals: https://www.gnu.org/software/libc/manual/html_node/Termination-Signals.html |
| 32 | + signal.Notify(c, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGHUP) |
| 33 | + // do something when os signal received |
| 34 | + go func() { |
| 35 | + for s := range c { |
| 36 | + switch s { |
| 37 | + // if lambda runtime received SIGTERM signal,perform actual clean up work here. |
| 38 | + case syscall.SIGTERM: |
| 39 | + fmt.Println("[runtime] SIGTERM received") |
| 40 | + fmt.Println("[runtime] Graceful shutdown in progress ...") |
| 41 | + fmt.Println("[runtime] Graceful shutdown completed") |
| 42 | + os.Exit(0) |
| 43 | + // else if lambda runtime received other signal |
| 44 | + default: |
| 45 | + fmt.Println("[runtime] Other signal received") |
| 46 | + fmt.Println("[runtime] Graceful shutdown in progress ...") |
| 47 | + fmt.Println("[runtime] Graceful shutdown completed") |
| 48 | + os.Exit(0) |
| 49 | + } |
| 50 | + } |
| 51 | + }() |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +Use the following AWS SAM CLI commands to build and deploy this demo. |
| 56 | + |
| 57 | +```bash |
| 58 | +sam build |
| 59 | +sam deploy |
| 60 | +``` |
| 61 | + |
| 62 | +Take note of the output value of `GoHelloWorldApi`. Use curl to invoke the api and trigger the lambda function at least once. |
| 63 | + |
| 64 | +```bash |
| 65 | +curl "replace this with value of GoHelloWorldApi" |
| 66 | +``` |
| 67 | + |
| 68 | +Waite for several minutes, check the function's log messages in CloudWatch. If you see a log line containing "SIGTERM |
| 69 | +received", it works! |
| 70 | + |
| 71 | +for example: |
| 72 | + |
| 73 | +```text |
| 74 | +2023-12-15T14:03:59.046+08:00 INIT_START Runtime Version: provided:al2023.v10 Runtime Version ARN: arn:aws:lambda:us-east-1::runtime:389fcaae1b213b40d38ed791dfb615af1a71a32d6996ff7c4afdde3d5af4b6f2 |
| 75 | +2023-12-15T14:03:59.104+08:00 LOGS Name: cloudwatch_lambda_agent State: Subscribed Types: [Platform] |
| 76 | +2023-12-15T14:03:59.173+08:00 EXTENSION Name: cloudwatch_lambda_agent State: Ready Events: [INVOKE, SHUTDOWN] |
| 77 | +2023-12-15T14:03:59.175+08:00 START RequestId: 90d93089-6e10-45f2-81f2-3a640945976b Version: $LATEST |
| 78 | +2023-12-15T14:03:59.230+08:00 END RequestId: 90d93089-6e10-45f2-81f2-3a640945976b |
| 79 | +2023-12-15T14:03:59.230+08:00 REPORT RequestId: 90d93089-6e10-45f2-81f2-3a640945976b Duration: 55.01 ms Billed Duration: 183 ms Memory Size: 128 MB Max Memory Used: 31 MB Init Duration: 127.75 ms |
| 80 | +2023-12-15T14:04:07.275+08:00 START RequestId: 89827818-1fdf-4626-a0eb-4cb509171c29 Version: $LATEST |
| 81 | +2023-12-15T14:04:07.330+08:00 END RequestId: 89827818-1fdf-4626-a0eb-4cb509171c29 |
| 82 | +2023-12-15T14:04:07.330+08:00 REPORT RequestId: 89827818-1fdf-4626-a0eb-4cb509171c29 Duration: 55.80 ms Billed Duration: 56 ms Memory Size: 128 MB Max Memory Used: 31 MB |
| 83 | +2023-12-15T14:09:35.620+08:00 [runtime] SIGTERM received |
| 84 | +2023-12-15T14:09:35.620+08:00 [runtime] Graceful shutdown in progress ... |
| 85 | +2023-12-15T14:09:35.620+08:00 [runtime] Graceful shutdown completed |
| 86 | +``` |
| 87 | + |
| 88 | +## Tested Runtimes |
| 89 | + |
| 90 | +| language version | Identifier | Operating system | Architectures | Support status | |
| 91 | +|-----------------------------|-----------------|-------------------|------------------|----------------| |
| 92 | +| go 1.x<br/>(1.19,1.20,1.21) | provided.al2023 | Amazon Linux 2023 | arm64<br/>x86_64 | ✅Support | |
| 93 | +| go 1.x<br/>(1.19,1.20,1.21) | provided.al2 | Amazon Linux 2 | arm64<br/>x86_64 | ✅Support | |
| 94 | + |
| 95 | +## Reference: |
| 96 | + |
| 97 | +- [Building Lambda functions with Go](https://docs.aws.amazon.com/lambda/latest/dg/lambda-golang.html) |
| 98 | +- [AWS SAM Documentation](https://docs.aws.amazon.com/serverless-application-model/) |
0 commit comments