Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a938332

Browse files
Merge pull request #5 from bauerjan93/main
Online ML with AWS Lambda
2 parents ead8d7d + df18f8c commit a938332

File tree

17 files changed

+1080
-0
lines changed

17 files changed

+1080
-0
lines changed
Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
# Created by https://www.gitignore.io/api/osx,linux,python,windows,pycharm,visualstudiocode
2+
3+
### Linux ###
4+
*~
5+
6+
# temporary files which can be created if a process still has a handle open of a deleted file
7+
.fuse_hidden*
8+
9+
# KDE directory preferences
10+
.directory
11+
12+
# Linux trash folder which might appear on any partition or disk
13+
.Trash-*
14+
15+
# .nfs files are created when an open file is removed but is still being accessed
16+
.nfs*
17+
18+
### OSX ###
19+
*.DS_Store
20+
.AppleDouble
21+
.LSOverride
22+
23+
# Icon must end with two \r
24+
Icon
25+
26+
# Thumbnails
27+
._*
28+
29+
# Files that might appear in the root of a volume
30+
.DocumentRevisions-V100
31+
.fseventsd
32+
.Spotlight-V100
33+
.TemporaryItems
34+
.Trashes
35+
.VolumeIcon.icns
36+
.com.apple.timemachine.donotpresent
37+
38+
# Directories potentially created on remote AFP share
39+
.AppleDB
40+
.AppleDesktop
41+
Network Trash Folder
42+
Temporary Items
43+
.apdisk
44+
45+
### PyCharm ###
46+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
47+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
48+
49+
# User-specific stuff:
50+
.idea/**/workspace.xml
51+
.idea/**/tasks.xml
52+
.idea/dictionaries
53+
54+
# Sensitive or high-churn files:
55+
.idea/**/dataSources/
56+
.idea/**/dataSources.ids
57+
.idea/**/dataSources.xml
58+
.idea/**/dataSources.local.xml
59+
.idea/**/sqlDataSources.xml
60+
.idea/**/dynamic.xml
61+
.idea/**/uiDesigner.xml
62+
63+
# Gradle:
64+
.idea/**/gradle.xml
65+
.idea/**/libraries
66+
67+
# CMake
68+
cmake-build-debug/
69+
70+
# Mongo Explorer plugin:
71+
.idea/**/mongoSettings.xml
72+
73+
## File-based project format:
74+
*.iws
75+
76+
## Plugin-specific files:
77+
78+
# IntelliJ
79+
/out/
80+
81+
# mpeltonen/sbt-idea plugin
82+
.idea_modules/
83+
84+
# JIRA plugin
85+
atlassian-ide-plugin.xml
86+
87+
# Cursive Clojure plugin
88+
.idea/replstate.xml
89+
90+
# Ruby plugin and RubyMine
91+
/.rakeTasks
92+
93+
# Crashlytics plugin (for Android Studio and IntelliJ)
94+
com_crashlytics_export_strings.xml
95+
crashlytics.properties
96+
crashlytics-build.properties
97+
fabric.properties
98+
99+
### PyCharm Patch ###
100+
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
101+
102+
# *.iml
103+
# modules.xml
104+
# .idea/misc.xml
105+
# *.ipr
106+
107+
# Sonarlint plugin
108+
.idea/sonarlint
109+
110+
### Python ###
111+
# Byte-compiled / optimized / DLL files
112+
__pycache__/
113+
*.py[cod]
114+
*$py.class
115+
116+
# C extensions
117+
*.so
118+
119+
# Distribution / packaging
120+
.Python
121+
build/
122+
develop-eggs/
123+
dist/
124+
downloads/
125+
eggs/
126+
.eggs/
127+
lib/
128+
lib64/
129+
parts/
130+
sdist/
131+
var/
132+
wheels/
133+
*.egg-info/
134+
.installed.cfg
135+
*.egg
136+
137+
# PyInstaller
138+
# Usually these files are written by a python script from a template
139+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
140+
*.manifest
141+
*.spec
142+
143+
# Installer logs
144+
pip-log.txt
145+
pip-delete-this-directory.txt
146+
147+
# Unit test / coverage reports
148+
htmlcov/
149+
.tox/
150+
.coverage
151+
.coverage.*
152+
.cache
153+
.pytest_cache/
154+
nosetests.xml
155+
coverage.xml
156+
*.cover
157+
.hypothesis/
158+
159+
# Translations
160+
*.mo
161+
*.pot
162+
163+
# Flask stuff:
164+
instance/
165+
.webassets-cache
166+
167+
# Scrapy stuff:
168+
.scrapy
169+
170+
# Sphinx documentation
171+
docs/_build/
172+
173+
# PyBuilder
174+
target/
175+
176+
# Jupyter Notebook
177+
.ipynb_checkpoints
178+
179+
# pyenv
180+
.python-version
181+
182+
# celery beat schedule file
183+
celerybeat-schedule.*
184+
185+
# SageMath parsed files
186+
*.sage.py
187+
188+
# Environments
189+
.env
190+
.venv
191+
env/
192+
venv/
193+
ENV/
194+
env.bak/
195+
venv.bak/
196+
.aws-sam
197+
.idea
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
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# Serverless scikit-learn training and inference
2+
3+
This project contains source code and supporting files for a serverless application for training and serving a Machine Learning model in [scikit-learn](https://scikit-learn.org/). The following image illustrates the application's architecture:
4+
5+
![Alt text](./img/architecture_diagram.png?raw=true "Title")
6+
7+
This example includes the following files and folders:
8+
9+
- app/lambda_inference/ - Code for the application's inference Lambda function
10+
- app/lambda_inference/ - Code for the application's training Lambda function
11+
- app/*/app.py - Code for the application's Lambda function.
12+
- app/*/Dockerfile - The Dockerfile to build the container image.
13+
- app/*/requirements.txt - The pip requirements to be installed during the container build.
14+
- template.yaml - A template that defines the application's AWS resources.
15+
16+
The application uses several AWS resources, including Lambda functions and an API Gateway API. These resources are defined in the `template.yaml` file in this project. You can update the template to add AWS resources through the same deployment process that updates your application code.
17+
18+
While this template does not use any auth, you will almost certainly want to use auth in order to productionize. Please follow [these instructions](https://github.com/aws/serverless-application-model/blob/master/versions/2016-10-31.md#function-auth-object) to set up auth with SAM.
19+
20+
## Deploy the sample application
21+
22+
The Serverless Application Model Command Line Interface (SAM CLI) is an extension of the AWS CLI that adds functionality for building and testing Lambda applications. It uses Docker to run your functions in an Amazon Linux environment that matches Lambda. It can also emulate your application's build environment and API.
23+
24+
To use the SAM CLI, you need the following tools.
25+
26+
* SAM CLI - [Install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html)
27+
* Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community)
28+
29+
You may need the following for local testing.
30+
* [Python 3 installed](https://www.python.org/downloads/)
31+
32+
To build and deploy your application for the first time, run the following in your shell:
33+
34+
```bash
35+
sam build
36+
sam deploy --guided
37+
```
38+
39+
The first command will build a docker image from a Dockerfile and then copy the source of your application inside the Docker image. The second command will package and deploy your application to AWS, with a series of prompts:
40+
41+
* **Stack Name**: The name of the stack to deploy to CloudFormation. This should be unique to your account and region, and a good starting point would be something matching your project name.
42+
* **AWS Region**: The AWS region you want to deploy your app to.
43+
* **Confirm changes before deploy**: If set to yes, any change sets will be shown to you before execution for manual review. If set to no, the AWS SAM CLI will automatically deploy application changes.
44+
* **Allow SAM CLI IAM role creation**: Many AWS SAM templates, including this example, create AWS IAM roles required for the AWS Lambda function(s) included to access AWS services. By default, these are scoped down to minimum required permissions. To deploy an AWS CloudFormation stack which creates or modifies IAM roles, the `CAPABILITY_IAM` value for `capabilities` must be provided. If permission isn't provided through this prompt, to deploy this example you must explicitly pass `--capabilities CAPABILITY_IAM` to the `sam deploy` command.
45+
* **Save arguments to samconfig.toml**: If set to yes, your choices will be saved to a configuration file inside the project, so that in the future you can just re-run `sam deploy` without parameters to deploy changes to your application.
46+
47+
You can find your API Gateway Endpoint URL in the output values displayed after deployment.
48+
49+
## Use the SAM CLI to build and test locally
50+
51+
Build your application with the `sam build` command.
52+
53+
```bash
54+
online-ml$ sam build
55+
```
56+
57+
The SAM CLI builds a docker image from a Dockerfile and then installs dependencies defined in `app/*/requirements.txt` inside the docker image. The processed template file is saved in the `.aws-sam/build` folder.
58+
59+
Test a single function by invoking it directly with a test event. An event is a JSON document that represents the input that the function receives from the event source. Test events are included in the `events` folder in this project.
60+
61+
In order to have your local lambda function access the correct S3 bucket, access the AWS console, go to `CloudFormation` under stacks click on `serverless-online-ml`, click on `Resources` and copy the `Physical ID` of your `ModelBucket`.
62+
Paste the physical ID in the `env.json` in the respective fields of the two lambda functions:
63+
64+
```json
65+
{
66+
"TrainingFunction": {
67+
"BUCKET_NAME": "serverless-online-ml-modelbucket-xxxxxxxxxx" //Replace this string with your physical ID
68+
},
69+
"InferenceFunction": {
70+
"BUCKET_NAME": "serverless-online-ml-modelbucket-xxxxxxxxxx" //Replace this string with your physical ID
71+
}
72+
}
73+
```
74+
Run functions locally and invoke them with the `sam local invoke` command.
75+
```bash
76+
online-ml$ sam local invoke TrainingFunction --event events/train_event.json --env-vars env.json
77+
online-ml$ sam local invoke InferenceFunction --event events/inference_event.json --env-vars env.json
78+
```
79+
80+
## Add a resource to your application
81+
The application template uses AWS Serverless Application Model (AWS SAM) to define application resources. AWS SAM is an extension of AWS CloudFormation with a simpler syntax for configuring common serverless application resources such as functions, triggers, and APIs. For resources not included in [the SAM specification](https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md), you can use standard [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html) resource types.
82+
83+
## Fetch, tail, and filter Lambda function logs
84+
85+
To simplify troubleshooting, SAM CLI has a command called `sam logs`. `sam logs` lets you fetch logs generated by your deployed Lambda function from the command line. In addition to printing the logs on the terminal, this command has several nifty features to help you quickly find the bug.
86+
87+
`NOTE`: This command works for all AWS Lambda functions; not just the ones you deploy using SAM.
88+
89+
```bash
90+
online-ml$ sam logs -n InferenceFunction --stack-name online-ml --tail
91+
```
92+
93+
You can find more information and examples about filtering Lambda function logs in the [SAM CLI Documentation](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-logging.html).
94+
95+
## Cleanup
96+
97+
To delete the sample application that you created, use the AWS CLI. Assuming you used your project name for the stack name, you can run the following:
98+
99+
```bash
100+
aws cloudformation delete-stack --stack-name online-ml
101+
```
102+
103+
## Resources
104+
105+
See the [AWS SAM developer guide](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html) for an introduction to SAM specification, the SAM CLI, and serverless application concepts.
106+
107+
Next, you can use AWS Serverless Application Repository to deploy ready to use Apps that go beyond hello world samples and learn how authors developed their applications: [AWS Serverless Application Repository main page](https://aws.amazon.com/serverless/serverlessrepo/)

‎online-machine-learning-aws-lambda/__init__.py

Whitespace-only changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM public.ecr.aws/lambda/python:3.8
2+
3+
COPY requirements.txt ./
4+
5+
RUN python3.8 -m pip install -r requirements.txt -t .
6+
7+
COPY app.py ./
8+
9+
CMD ["app.lambda_handler"]

‎online-machine-learning-aws-lambda/app/lambda_inference/__init__.py

Whitespace-only changes.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /