@@ -7,24 +7,32 @@ This folder contains a simple python function with CloudWatch Lambda Insight ena
7
7
``` yaml
8
8
Properties :
9
9
Layers :
10
- - !Sub " arn:aws:lambda:${AWS::Region}:580247275435:layer:LambdaInsightsExtension:14" # Add Lambda Insight Extension
10
+ # Add Lambda Insight Extension: 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"
11
12
Policies :
12
- - CloudWatchLambdaInsightsExecutionRolePolicy # Add IAM Permission for Lambda Insight Extension
13
+ # Add IAM Permission for Lambda Insight Extension
14
+ - CloudWatchLambdaInsightsExecutionRolePolicy
13
15
` ` `
14
16
15
- In the function, a simple SIGTERM signal handler is added. It will be executed when the lambda runtime receives a SIGTERM signal.
17
+ In the function, a simple ` SIGTERM` signal handler is added. It will be executed when the lambda runtime receives a ` SIGTERM` signal.
16
18
17
19
` ` ` python
18
- def exit_gracefully(signum, frame) :
20
+ def exit_gracefully(signum, frame):
21
+ r"""
22
+ SIGTERM Handler: https://docs.aws.amazon.com/lambda/latest/operatorguide/static-initialization.html
23
+ Listening for os signals that can be handled,reference: https://docs.aws.amazon.com/lambda/latest/dg/runtimes-extensions-api.html
24
+ Termination Signals: https://www.gnu.org/software/libc/manual/html_node/Termination-Signals.html
25
+ """
19
26
print("[runtime] SIGTERM received")
20
27
21
28
print("[runtime] cleaning up")
22
- # perform actual clean up work here.
29
+ # perform actual clean up work here.
23
30
time.sleep(0.2)
24
31
25
32
print("[runtime] exiting")
26
33
sys.exit(0)
27
34
35
+
28
36
signal.signal(signal.SIGTERM, exit_gracefully)
29
37
30
38
` ` `
@@ -36,21 +44,42 @@ sam build --use-container
36
44
sam deploy --guided
37
45
` ` `
38
46
39
- Take note of the output value of HelloWorldApi. Use curl to invoke the api and trigger the lambda function once.
47
+ Take note of the output value of HelloWorldApi. Use curl to invoke the api and trigger the lambda function at least once.
40
48
41
49
` ` ` bash
42
50
curl "replace this with value of HelloWorldApi"
43
51
` ` `
44
52
45
- Waite for serveral minutes, check the function's log messages in CloudWatch. If you see a log line containing "SIGTERM received", it works!
53
+ Waite for serveral minutes, check the function's log messages in CloudWatch. If you see a log line containing "SIGTERM
54
+ received", it works!
46
55
56
+ for example :
57
+ 
47
58
```
48
- 2021/07/28/[$LATEST]7b4ab412d2494617934d9cd408d8f8a8 2021-07-28T06:15:05.879000 START RequestId: abdd9973-487b-4293-93e5-ed230703cab0 Version: $LATEST
49
- 2021/07/28/[$LATEST]7b4ab412d2494617934d9cd408d8f8a8 2021-07-28T06:15:06.004000 LOGS Name: cloudwatch_lambda_agent State: Subscribed Types: [platform]
50
- 2021/07/28/[$LATEST]7b4ab412d2494617934d9cd408d8f8a8 2021-07-28T06:15:06.004000 EXTENSION Name: cloudwatch_lambda_agent State: Ready Events: [INVOKE,SHUTDOWN]
51
- 2021/07/28/[$LATEST]7b4ab412d2494617934d9cd408d8f8a8 2021-07-28T06:15:06.073000 END RequestId: abdd9973-487b-4293-93e5-ed230703cab0
52
- 2021/07/28/[$LATEST]7b4ab412d2494617934d9cd408d8f8a8 2021-07-28T06:15:06.073000 REPORT RequestId: abdd9973-487b-4293-93e5-ed230703cab0 Duration: 67.61 ms Billed Duration: 68 ms Memory Size: 128 MB Max Memory Used: 64 MB Init Duration: 201.85 ms
53
- 2021/07/28/[$LATEST]7b4ab412d2494617934d9cd408d8f8a8 2021-07-28T06:21:05.739000 [runtime] SIGTERM received
54
- 2021/07/28/[$LATEST]7b4ab412d2494617934d9cd408d8f8a8 2021-07-28T06:21:05.739000 [runtime] cleaning up
55
- 2021/07/28/[$LATEST]7b4ab412d2494617934d9cd408d8f8a8 2021-07-28T06:21:05.939000 [runtime] exiting
59
+ 2023-12-15T14:48:34.955+08:00 INIT_START Runtime Version: python:3.12.v16 Runtime Version ARN: arn:aws:lambda:us-east-1::runtime:5eaca0ecada617668d4d59f66bf32f963e95d17ca326aad52b85465d04c429f5
60
+ 2023-12-15T14:48:35.021+08:00 LOGS Name: cloudwatch_lambda_agent State: Subscribed Types: [ Platform]
61
+ 2023-12-15T14:48:35.130+08:00 EXTENSION Name: cloudwatch_lambda_agent State: Ready Events: [ INVOKE, SHUTDOWN]
62
+ 2023-12-15T14:48:35.131+08:00 START RequestId: 19234f5f-b2f8-4a9e-b7da-641ef9b4a181 Version: $LATEST
63
+ 2023-12-15T14:48:35.171+08:00 END RequestId: 19234f5f-b2f8-4a9e-b7da-641ef9b4a181
64
+ 2023-12-15T14:48:35.171+08:00 REPORT RequestId: 19234f5f-b2f8-4a9e-b7da-641ef9b4a181 Duration: 39.75 ms Billed Duration: 40 ms Memory Size: 128 MB Max Memory Used: 45 MB Init Duration: 175.18 ms
65
+ 2023-12-15T14:48:37.515+08:00 START RequestId: a20821af-2040-4aa5-9908-09ec9aec0279 Version: $LATEST
66
+ 2023-12-15T14:48:37.531+08:00 END RequestId: a20821af-2040-4aa5-9908-09ec9aec0279
67
+ 2023-12-15T14:48:37.531+08:00 REPORT RequestId: a20821af-2040-4aa5-9908-09ec9aec0279 Duration: 16.06 ms Billed Duration: 17 ms Memory Size: 128 MB Max Memory Used: 45 MB
68
+ 2023-12-15T14:54:28.850+08:00 [ runtime] SIGTERM received
69
+ 2023-12-15T14:54:28.850+08:00 [ runtime] cleaning up
70
+ 2023-12-15T14:54:29.051+08:00 [ runtime] exiting
56
71
```
72
+
73
+
74
+ ## Tested Runtimes
75
+
76
+ | language version | Identifier | Operating system | Architectures | Support status |
77
+ |------------------------|-------------------------------------------------------|-------------------|------------------|----------------|
78
+ | Python 3.12 | python3.12 | Amazon Linux 2023 | arm64<br/>x86_64 | ✅ Support |
79
+ | Python 3.11 or earlier | python3.11<br/>python3.10<br/>python3.9<br/>python3.8 | Amazon Linux 2 | arm64<br/>x86_64 | ❌ NOT Support |
80
+
81
+ ## Reference:
82
+
83
+ - [Building Lambda functions with Python](https://docs.aws.amazon.com/lambda/latest/dg/lambda-python.html)
84
+ - [Python 3.12 runtime now available in AWS Lambda](https://aws.amazon.com/cn/blogs/compute/python-3-12-runtime-now-available-in-aws-lambda/)
85
+ - [AWS SAM Documentation](https://docs.aws.amazon.com/serverless-application-model/)
0 commit comments