Skip to content

Commit b765ccd

Browse files
authored
Merge pull request #13 from thinktik/python3-dev
update python3-demo
2 parents 8a12878 + 2f166f0 commit b765ccd

File tree

5 files changed

+102
-24
lines changed

5 files changed

+102
-24
lines changed

python-demo/README.md

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,32 @@ This folder contains a simple python function with CloudWatch Lambda Insight ena
77
```yaml
88
Properties:
99
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"
1112
Policies:
12-
- CloudWatchLambdaInsightsExecutionRolePolicy # Add IAM Permission for Lambda Insight Extension
13+
# Add IAM Permission for Lambda Insight Extension
14+
- CloudWatchLambdaInsightsExecutionRolePolicy
1315
```
1416
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.
1618

1719
```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+
"""
1926
print("[runtime] SIGTERM received")
2027
2128
print("[runtime] cleaning up")
22-
# perform actual clean up work here.
29+
# perform actual clean up work here.
2330
time.sleep(0.2)
2431
2532
print("[runtime] exiting")
2633
sys.exit(0)
2734
35+
2836
signal.signal(signal.SIGTERM, exit_gracefully)
2937
3038
```
@@ -36,21 +44,42 @@ sam build --use-container
3644
sam deploy --guided
3745
```
3846

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.
4048

4149
```bash
4250
curl "replace this with value of HelloWorldApi"
4351
```
4452

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!
4655

56+
for example:
57+
![](./docs/images/python3-2023-12-15.png)
4758
```
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
5671
```
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/)
390 KB
Loading

python-demo/hello_world/app.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,37 @@
11
import json
2+
import platform
23
import signal
34
import sys
45
import time
56

6-
def exit_gracefully(signum, frame):
7+
8+
def exit_gracefully(signum, frame):
9+
r"""
10+
SIGTERM Handler: https://docs.aws.amazon.com/lambda/latest/operatorguide/static-initialization.html
11+
Listening for os signals that can be handled,reference: https://docs.aws.amazon.com/lambda/latest/dg/runtimes-extensions-api.html
12+
Termination Signals: https://www.gnu.org/software/libc/manual/html_node/Termination-Signals.html
13+
"""
714
print("[runtime] SIGTERM received")
815

916
print("[runtime] cleaning up")
10-
# perform actual clean up work here.
17+
# perform actual clean up work here.
1118
time.sleep(0.2)
1219

1320
print("[runtime] exiting")
1421
sys.exit(0)
1522

23+
1624
signal.signal(signal.SIGTERM, exit_gracefully)
1725

1826

1927
def lambda_handler(event, context):
20-
2128
return {
2229
"statusCode": 200,
2330
"body": json.dumps({
24-
"message": "hello world",
31+
"message": "hello python3",
32+
"source ip": event['requestContext']['identity']['sourceIp'],
33+
"architecture": platform.machine(),
34+
"operating system": platform.system(),
35+
"node version ": platform.python_version(),
2536
}),
2637
}

python-demo/samconfig.toml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# More information about the configuration file can be found here:
2+
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html
3+
version = 0.1
4+
5+
[default]
6+
[default.global.parameters]
7+
stack_name = "python3-graceful-shutdown-demo"
8+
9+
[default.build.parameters]
10+
cached = true
11+
parallel = true
12+
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-build.html
13+
use_container = true
14+
15+
[default.validate.parameters]
16+
lint = true
17+
18+
[default.deploy.parameters]
19+
capabilities = "CAPABILITY_IAM"
20+
confirm_changeset = true
21+
resolve_s3 = true
22+
23+
[default.package.parameters]
24+
resolve_s3 = true
25+
26+
[default.sync.parameters]
27+
watch = true
28+
29+
[default.local_start_api.parameters]
30+
warm_containers = "EAGER"
31+
32+
[default.local_start_lambda.parameters]
33+
warm_containers = "EAGER"

python-demo/template.yaml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
AWSTemplateFormatVersion: '2010-09-09'
22
Transform: AWS::Serverless-2016-10-31
33
Description: >
4-
python-demo
5-
4+
python3-graceful-shutdown-demo
5+
66
Sample SAM Template for python-demo
77
88
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
@@ -14,13 +14,18 @@ Resources:
1414
HelloWorldFunction:
1515
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
1616
Properties:
17+
FunctionName: graceful-shutdown-python3
1718
CodeUri: hello_world/
1819
Handler: app.lambda_handler
1920
Runtime: python3.12
21+
Architectures:
22+
- arm64
2023
Layers:
21-
- !Sub "arn:aws:lambda:${AWS::Region}:580247275435:layer:LambdaInsightsExtension:14" # Add Lambda Insight Extension
24+
# Add Lambda Insight Extension: https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Lambda-Insights-extension-versions.html
25+
- !Sub "arn:aws:lambda:${AWS::Region}:580247275435:layer:LambdaInsightsExtension-Arm64:5"
2226
Policies:
23-
- CloudWatchLambdaInsightsExecutionRolePolicy # Add IAM Permission for Lambda Insight Extension
27+
# Add IAM Permission for Lambda Insight Extension
28+
- CloudWatchLambdaInsightsExecutionRolePolicy
2429
Events:
2530
HelloWorld:
2631
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
@@ -40,4 +45,4 @@ Outputs:
4045
Value: !GetAtt HelloWorldFunction.Arn
4146
HelloWorldFunctionIamRole:
4247
Description: "Implicit IAM Role created for Hello World function"
43-
Value: !GetAtt HelloWorldFunctionRole.Arn
48+
Value: !GetAtt HelloWorldFunctionRole.Arn

0 commit comments

Comments
 (0)