Skip to content

Commit 64b2e94

Browse files
committed
add Flask example and update README files for other examples
1 parent 7917baf commit 64b2e94

File tree

16 files changed

+679
-28
lines changed

16 files changed

+679
-28
lines changed

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ AWS Lambda Adapter is written in Rust and based on [AWS Lambda Rust Runtime](htt
2222
AWS Lambda executes functions in x86_64 Amazon Linux Environment. We need to compile the adapter to that environment.
2323

2424
### Compiling with Docker
25-
On x86_64 Windows, Linux and macOS, you can run one command to compile Lambda Adapter with docker. The Dockerfile is [here](Dockerfile.x86).
25+
On x86_64 Windows, Linux and macOS, you can run one command to compile Lambda Adapter with docker.
26+
The Dockerfile is [here](Dockerfile.x86). [AWS CLI](https://aws.amazon.com/cli/) should have been installed and configured.
2627

2728
```shell
2829
$ make build
@@ -88,6 +89,14 @@ ADD src/ /var/task
8889
CMD ["node", "index.js"]
8990
```
9091

92+
Line 2 and 3 copy lambda adapter binary and set it as ENTRYPOINT. This is the minimum configuration required to use Lambda Adapter. No need to change the code.
93+
94+
```dockerfile
95+
COPY --from=aws-lambda-adapter:latest /opt/bootstrap /opt/bootstrap
96+
ENTRYPOINT ["/opt/bootstrap"]
97+
```
98+
99+
91100
The readiness check port/path and traffic port can be configured using environment variables.
92101

93102
|Environment Variable|Description |Default|
@@ -98,12 +107,12 @@ The readiness check port/path and traffic port can be configured using environme
98107

99108
## Show me examples
100109

101-
3 examples are included under the 'examples' directory. Check them out, find out how easy it is to run a web application on AWS Lambda.
110+
4 examples are included under the 'examples' directory. Check them out, find out how easy it is to run a web application on AWS Lambda.
102111

103-
- [nginx](examples/nginx)
112+
- [Flask]()
104113
- [express.js](examples/expressjs)
105114
- [SpringBoot](examples/springboot)
106-
115+
- [nginx](examples/nginx)
107116

108117
## Acknowledgement
109118

examples/expressjs/README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Express.js example
2+
3+
A basic express.js application example. You can build and test it locally as a typical Express.js application.
4+
5+
Using AWS Lambda Adapter, you can package this web application into Docker image, push to ECR, and deploy to Lambda, ECS/EKS, or EC2.
6+
7+
The application can be deployed in an AWS account using the [Serverless Application Model](https://github.com/awslabs/serverless-application-model). The `template.yaml` file in the root folder contains the application definition.
8+
9+
The top level folder is a typical AWS SAM project. The `app` directory is an express.js application with a [Dockerfile](app/Dockerfile).
10+
11+
```dockerfile
12+
FROM public.ecr.aws/lambda/nodejs:14
13+
COPY --from=aws-lambda-adapter:latest /opt/bootstrap /opt/bootstrap
14+
ENTRYPOINT ["/opt/bootstrap"]
15+
EXPOSE 8080
16+
WORKDIR "/var/task"
17+
ADD src/package.json /var/task/package.json
18+
ADD src/package-lock.json /var/task/package-lock.json
19+
RUN npm install --production
20+
ADD src/ /var/task
21+
CMD ["node", "index.js"]
22+
```
23+
24+
Line 2 and 3 copy lambda adapter binary and set it as ENTRYPOINT. This is the only change to run the express.js application on Lambda.
25+
26+
```dockerfile
27+
COPY --from=aws-lambda-adapter:latest /opt/bootstrap /opt/bootstrap
28+
ENTRYPOINT ["/opt/bootstrap"]
29+
```
30+
31+
## Pre-requisites
32+
33+
The following tools should be installed and configured.
34+
* [AWS CLI](https://aws.amazon.com/cli/)
35+
* [SAM CLI](https://github.com/awslabs/aws-sam-cli)
36+
* [Node](https://nodejs.org/en/)
37+
* [Docker](https://www.docker.com/products/docker-desktop)
38+
39+
Container image `aws-lambda-adapter:latest` should already exist. You could follow [README](../../README.md#how-to-build-it?) to build Lambda Adapter.
40+
41+
## Deploy to Lambda
42+
Navigate to the sample's folder and use the SAM CLI to build a container image
43+
```shell
44+
$ aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws
45+
$ sam build
46+
```
47+
48+
This command compiles the application and prepares a deployment package in the `.aws-sam` sub-directory.
49+
50+
To deploy the application in your AWS account, you can use the SAM CLI's guided deployment process and follow the instructions on the screen
51+
52+
```shell
53+
$ sam deploy --guided
54+
```
55+
Please take note of the container image name.
56+
Once the deployment is completed, the SAM CLI will print out the stack's outputs, including the new application URL. You can use `curl` or a web browser to make a call to the URL
57+
58+
```shell
59+
...
60+
---------------------------------------------------------------------------------------------------------
61+
OutputKey-Description OutputValue
62+
---------------------------------------------------------------------------------------------------------
63+
PetStoreApi - URL for application https://xxxxxxxxxx.execute-api.us-west-2.amazonaws.com/
64+
---------------------------------------------------------------------------------------------------------
65+
...
66+
67+
$ curl https://xxxxxxxxxx.execute-api.us-west-2.amazonaws.com/
68+
```
69+
70+
## Run the docker locally
71+
72+
We can run the same docker image locally, so that we know it can be deployed to ECS Fargate and EKS EC2 without code changes.
73+
74+
```shell
75+
$ docker run -d -p 8080:8080 {ECR Image}
76+
77+
```
78+
79+
Use curl to verify the docker container works.
80+
81+
```shell
82+
$ curl localhost:8080/
83+
```

examples/expressjs/template.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,5 @@ Resources:
4040
Outputs:
4141
ExpressApi:
4242
Description: "API Gateway endpoint URL for Prod stage for Express function"
43-
Value: !Sub "https://${ServerlessHttpApi}.execute-api.${AWS::Region}.amazonaws.com/"
43+
Value: !Sub "https://${ServerlessHttpApi}.execute-api.${AWS::Region}.${AWS::URLSuffix}/"
4444

examples/flask/.gitignore

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

0 commit comments

Comments
 (0)