Skip to content

Commit fd88fb8

Browse files
committed
add python demo
1 parent fad7967 commit fd88fb8

File tree

19 files changed

+559
-43
lines changed

19 files changed

+559
-43
lines changed

README.md

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
# lambda-graceful-shutdown-demo
2-
3-
## Graceful shutdown with Lambda Extension
1+
# Graceful shutdown with Lambda Extension
42

53
AWS Lambda allows developers to run their code without managing servers, automatic scaling and pay for value. Many developers use Lambda to connect with databases and Redis. But when a Lambda execution environment shuts down, the connections remain open and hold up backend resources. Databases ususally can clean those connections after an idle timeout. However, developers want to gracefully clean up those connections when Lambda execution environment shuts down. Now, this can be achieved with Lambda Extensions.
64

@@ -12,48 +10,12 @@ If the runtime or an extension does not respond to the Shutdown event within the
1210

1311
![lambda extension shutdown phase](https://docs.aws.amazon.com/lambda/latest/dg/images/Shutdown-Phase.png)
1412

15-
## The demo
16-
17-
This repo contains a simple function with CloudWatch Lambda Insight enabled. CloudWatch Lambda Insight is monitoring and troubleshooting solution for serverless applicaiton. Its agent is an external extension.
18-
19-
In the function, a simple SIGTERM signal handler is added. It will be executed when the lambda runtime receives a SIGTERM signal.
20-
21-
```javascript
22-
// SIGTERM Handler
23-
process.on('SIGTERM', async () => {
24-
console.info('[runtime] SIGTERM received');
25-
26-
console.info('[runtime] cleaning up');
27-
// perform actual clean up work here.
28-
await new Promise(resolve => setTimeout(resolve, 200));
29-
30-
console.info('[runtime] exiting');
31-
process.exit(0)
32-
});
33-
```
34-
35-
Use the following AWS SAM CLI commands to build and deploy this demo.
3613

37-
```bash
38-
sam build
39-
sam deploy --guided
40-
```
14+
## Demos
4115

42-
Take note of the output value of HelloWorldApi. Use curl to invoke the api and trigger the lambda function once.
16+
This repo includes two examples. Please see the details in each demo folder.
4317

44-
```bash
45-
curl "replace this with value of HelloWorldApi"
46-
```
18+
[Node.js Demo](nodejs-demo/)
4719

48-
Waite for serveral minutes, check the function's log messages in CloudWatch. If you see a log line containing "SIGTERM received", it works!
20+
[Python Demo](python-demo/)
4921

50-
```
51-
2021/07/27/[$LATEST]0a35efaafbd24ecc9a5f4fad2dd94b49 2021-07-27T03:47:27.635000 START RequestId: 1aac889c-ccaf-4655-9ad1-018e464ab75d Version: $LATEST
52-
2021/07/27/[$LATEST]0a35efaafbd24ecc9a5f4fad2dd94b49 2021-07-27T03:47:27.789000 LOGS Name: cloudwatch_lambda_agent State: Subscribed Types: [platform]
53-
2021/07/27/[$LATEST]0a35efaafbd24ecc9a5f4fad2dd94b49 2021-07-27T03:47:27.789000 EXTENSION Name: cloudwatch_lambda_agent State: Ready Events: [INVOKE,SHUTDOWN]
54-
2021/07/27/[$LATEST]0a35efaafbd24ecc9a5f4fad2dd94b49 2021-07-27T03:47:27.880000 END RequestId: 1aac889c-ccaf-4655-9ad1-018e464ab75d
55-
2021/07/27/[$LATEST]0a35efaafbd24ecc9a5f4fad2dd94b49 2021-07-27T03:47:27.880000 REPORT RequestId: 1aac889c-ccaf-4655-9ad1-018e464ab75d Duration: 90.73 ms Billed Duration: 91 ms Memory Size: 128 MB Max Memory Used: 81 MB Init Duration: 232.27 ms
56-
2021/07/27/[$LATEST]0a35efaafbd24ecc9a5f4fad2dd94b49 2021-07-27T03:53:27.681000 2021-07-27T03:53:27.661Z 1aac889c-ccaf-4655-9ad1-018e464ab75d INFO [runtime] SIGTERM received
57-
2021/07/27/[$LATEST]0a35efaafbd24ecc9a5f4fad2dd94b49 2021-07-27T03:53:27.681000 2021-07-27T03:53:27.681Z 1aac889c-ccaf-4655-9ad1-018e464ab75d INFO [runtime] cleaning up
58-
2021/07/27/[$LATEST]0a35efaafbd24ecc9a5f4fad2dd94b49 2021-07-27T03:53:27.882000 2021-07-27T03:53:27.882Z 1aac889c-ccaf-4655-9ad1-018e464ab75d INFO [runtime] exiting
59-
```

nodejs-demo/READEM.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Node.js demo
2+
3+
This folder contains a simple python function with CloudWatch Lambda Insight enabled. CloudWatch Lambda Insight is monitoring and troubleshooting solution for serverless applicaiton. Its agent is an external extension. Any external extension will work. We use Lambda Insight because it is already avaiable.
4+
5+
```yaml
6+
Properties:
7+
Layers:
8+
- !Sub "arn:aws:lambda:${AWS::Region}:580247275435:layer:LambdaInsightsExtension:14" # Add Lambda Insight Extension
9+
Policies:
10+
- CloudWatchLambdaInsightsExecutionRolePolicy # Add IAM Permission for Lambda Insight Extension
11+
```
12+
13+
In the function, a simple SIGTERM signal handler is added. It will be executed when the lambda runtime receives a SIGTERM signal.
14+
15+
```javascript
16+
process.on('SIGTERM', async () => {
17+
console.info('[runtime] SIGTERM received');
18+
19+
console.info('[runtime] cleaning up');
20+
// perform actual clean up work here.
21+
await new Promise(resolve => setTimeout(resolve, 200));
22+
23+
console.info('[runtime] exiting');
24+
process.exit(0)
25+
});
26+
```
27+
28+
Use the following AWS SAM CLI commands to build and deploy this demo.
29+
30+
```bash
31+
sam build --use-container
32+
sam deploy --guided
33+
```
34+
35+
Take note of the output value of HelloWorldApi. Use curl to invoke the api and trigger the lambda function once.
36+
37+
```bash
38+
curl "replace this with value of HelloWorldApi"
39+
```
40+
41+
Waite for serveral minutes, check the function's log messages in CloudWatch. If you see a log line containing "SIGTERM received", it works!
42+
43+
```
44+
2021/07/27/[$LATEST]0a35efaafbd24ecc9a5f4fad2dd94b49 2021-07-27T03:47:27.635000 START RequestId: 1aac889c-ccaf-4655-9ad1-018e464ab75d Version: $LATEST
45+
2021/07/27/[$LATEST]0a35efaafbd24ecc9a5f4fad2dd94b49 2021-07-27T03:47:27.789000 LOGS Name: cloudwatch_lambda_agent State: Subscribed Types: [platform]
46+
2021/07/27/[$LATEST]0a35efaafbd24ecc9a5f4fad2dd94b49 2021-07-27T03:47:27.789000 EXTENSION Name: cloudwatch_lambda_agent State: Ready Events: [INVOKE,SHUTDOWN]
47+
2021/07/27/[$LATEST]0a35efaafbd24ecc9a5f4fad2dd94b49 2021-07-27T03:47:27.880000 END RequestId: 1aac889c-ccaf-4655-9ad1-018e464ab75d
48+
2021/07/27/[$LATEST]0a35efaafbd24ecc9a5f4fad2dd94b49 2021-07-27T03:47:27.880000 REPORT RequestId: 1aac889c-ccaf-4655-9ad1-018e464ab75d Duration: 90.73 ms Billed Duration: 91 ms Memory Size: 128 MB Max Memory Used: 81 MB Init Duration: 232.27 ms
49+
2021/07/27/[$LATEST]0a35efaafbd24ecc9a5f4fad2dd94b49 2021-07-27T03:53:27.681000 2021-07-27T03:53:27.661Z 1aac889c-ccaf-4655-9ad1-018e464ab75d INFO [runtime] SIGTERM received
50+
2021/07/27/[$LATEST]0a35efaafbd24ecc9a5f4fad2dd94b49 2021-07-27T03:53:27.681000 2021-07-27T03:53:27.681Z 1aac889c-ccaf-4655-9ad1-018e464ab75d INFO [runtime] cleaning up
51+
2021/07/27/[$LATEST]0a35efaafbd24ecc9a5f4fad2dd94b49 2021-07-27T03:53:27.882000 2021-07-27T03:53:27.882Z 1aac889c-ccaf-4655-9ad1-018e464ab75d INFO [runtime] exiting
52+
```
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

python-demo/.gitignore

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
2+
# Created by https://www.gitignore.io/api/osx,linux,python,windows,pycharm,visualstudiocode
3+
4+
### Linux ###
5+
*~
6+
7+
# temporary files which can be created if a process still has a handle open of a deleted file
8+
.fuse_hidden*
9+
10+
# KDE directory preferences
11+
.directory
12+
13+
# Linux trash folder which might appear on any partition or disk
14+
.Trash-*
15+
16+
# .nfs files are created when an open file is removed but is still being accessed
17+
.nfs*
18+
19+
### OSX ###
20+
*.DS_Store
21+
.AppleDouble
22+
.LSOverride
23+
24+
# Icon must end with two \r
25+
Icon
26+
27+
# Thumbnails
28+
._*
29+
30+
# Files that might appear in the root of a volume
31+
.DocumentRevisions-V100
32+
.fseventsd
33+
.Spotlight-V100
34+
.TemporaryItems
35+
.Trashes
36+
.VolumeIcon.icns
37+
.com.apple.timemachine.donotpresent
38+
39+
# Directories potentially created on remote AFP share
40+
.AppleDB
41+
.AppleDesktop
42+
Network Trash Folder
43+
Temporary Items
44+
.apdisk
45+
46+
### PyCharm ###
47+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
48+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
49+
50+
# User-specific stuff:
51+
.idea/**/workspace.xml
52+
.idea/**/tasks.xml
53+
.idea/dictionaries
54+
55+
# Sensitive or high-churn files:
56+
.idea/**/dataSources/
57+
.idea/**/dataSources.ids
58+
.idea/**/dataSources.xml
59+
.idea/**/dataSources.local.xml
60+
.idea/**/sqlDataSources.xml
61+
.idea/**/dynamic.xml
62+
.idea/**/uiDesigner.xml
63+
64+
# Gradle:
65+
.idea/**/gradle.xml
66+
.idea/**/libraries
67+
68+
# CMake
69+
cmake-build-debug/
70+
71+
# Mongo Explorer plugin:
72+
.idea/**/mongoSettings.xml
73+
74+
## File-based project format:
75+
*.iws
76+
77+
## Plugin-specific files:
78+
79+
# IntelliJ
80+
/out/
81+
82+
# mpeltonen/sbt-idea plugin
83+
.idea_modules/
84+
85+
# JIRA plugin
86+
atlassian-ide-plugin.xml
87+
88+
# Cursive Clojure plugin
89+
.idea/replstate.xml
90+
91+
# Ruby plugin and RubyMine
92+
/.rakeTasks
93+
94+
# Crashlytics plugin (for Android Studio and IntelliJ)
95+
com_crashlytics_export_strings.xml
96+
crashlytics.properties
97+
crashlytics-build.properties
98+
fabric.properties
99+
100+
### PyCharm Patch ###
101+
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
102+
103+
# *.iml
104+
# modules.xml
105+
# .idea/misc.xml
106+
# *.ipr
107+
108+
# Sonarlint plugin
109+
.idea/sonarlint
110+
111+
### Python ###
112+
# Byte-compiled / optimized / DLL files
113+
__pycache__/
114+
*.py[cod]
115+
*$py.class
116+
117+
# C extensions
118+
*.so
119+
120+
# Distribution / packaging
121+
.Python
122+
build/
123+
develop-eggs/
124+
dist/
125+
downloads/
126+
eggs/
127+
.eggs/
128+
lib/
129+
lib64/
130+
parts/
131+
sdist/
132+
var/
133+
wheels/
134+
*.egg-info/
135+
.installed.cfg
136+
*.egg
137+
138+
# PyInstaller
139+
# Usually these files are written by a python script from a template
140+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
141+
*.manifest
142+
*.spec
143+
144+
# Installer logs
145+
pip-log.txt
146+
pip-delete-this-directory.txt
147+
148+
# Unit test / coverage reports
149+
htmlcov/
150+
.tox/
151+
.coverage
152+
.coverage.*
153+
.cache
154+
.pytest_cache/
155+
nosetests.xml
156+
coverage.xml
157+
*.cover
158+
.hypothesis/
159+
160+
# Translations
161+
*.mo
162+
*.pot
163+
164+
# Flask stuff:
165+
instance/
166+
.webassets-cache
167+
168+
# Scrapy stuff:
169+
.scrapy
170+
171+
# Sphinx documentation
172+
docs/_build/
173+
174+
# PyBuilder
175+
target/
176+
177+
# Jupyter Notebook
178+
.ipynb_checkpoints
179+
180+
# pyenv
181+
.python-version
182+
183+
# celery beat schedule file
184+
celerybeat-schedule.*
185+
186+
# SageMath parsed files
187+
*.sage.py
188+
189+
# Environments
190+
.env
191+
.venv
192+
env/
193+
venv/
194+
ENV/
195+
env.bak/
196+
venv.bak/
197+
198+
# Spyder project settings
199+
.spyderproject
200+
.spyproject
201+
202+
# Rope project settings
203+
.ropeproject
204+
205+
# mkdocs documentation
206+
/site
207+
208+
# mypy
209+
.mypy_cache/
210+
211+
### VisualStudioCode ###
212+
.vscode/*
213+
!.vscode/settings.json
214+
!.vscode/tasks.json
215+
!.vscode/launch.json
216+
!.vscode/extensions.json
217+
.history
218+
219+
### Windows ###
220+
# Windows thumbnail cache files
221+
Thumbs.db
222+
ehthumbs.db
223+
ehthumbs_vista.db
224+
225+
# Folder config file
226+
Desktop.ini
227+
228+
# Recycle Bin used on file shares
229+
$RECYCLE.BIN/
230+
231+
# Windows Installer files
232+
*.cab
233+
*.msi
234+
*.msm
235+
*.msp
236+
237+
# Windows shortcuts
238+
*.lnk
239+
240+
# Build folder
241+
242+
*/build/*
243+
244+
# End of https://www.gitignore.io/api/osx,linux,python,windows,pycharm,visualstudiocode

0 commit comments

Comments
 (0)