Skip to content

Commit 366824e

Browse files
committed
Initial commit
0 parents  commit 366824e

11 files changed

Lines changed: 559 additions & 0 deletions

File tree

.aws-sam/build.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# This file is auto generated by SAM CLI build command
2+
3+
[function_build_definitions]
4+
[function_build_definitions.6b94021c-9634-4578-ae0e-1dd62a2d50b3]
5+
packagetype = "Image"
6+
functions = ["InferenceFunction"]
7+
8+
[function_build_definitions.6b94021c-9634-4578-ae0e-1dd62a2d50b3.metadata]
9+
Dockerfile = "Dockerfile"
10+
DockerContext = "/Users/pnh/Projects/img-clf/app"
11+
DockerTag = "python3.9-v1"
12+
13+
[layer_build_definitions]

.gitignore

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
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+
197+
# Spyder project settings
198+
.spyderproject
199+
.spyproject
200+
201+
# Rope project settings
202+
.ropeproject
203+
204+
# mkdocs documentation
205+
/site
206+
207+
# mypy
208+
.mypy_cache/
209+
210+
### VisualStudioCode ###
211+
.vscode/*
212+
!.vscode/settings.json
213+
!.vscode/tasks.json
214+
!.vscode/launch.json
215+
!.vscode/extensions.json
216+
.history
217+
218+
### Windows ###
219+
# Windows thumbnail cache files
220+
Thumbs.db
221+
ehthumbs.db
222+
ehthumbs_vista.db
223+
224+
# Folder config file
225+
Desktop.ini
226+
227+
# Recycle Bin used on file shares
228+
$RECYCLE.BIN/
229+
230+
# Windows Installer files
231+
*.cab
232+
*.msi
233+
*.msm
234+
*.msp
235+
236+
# Windows shortcuts
237+
*.lnk
238+
239+
# Build folder
240+
241+
*/build/*
242+
243+
# End of https://www.gitignore.io/api/osx,linux,python,windows,pycharm,visualstudiocode

README.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# img-clf
2+
3+
This project contains source code and supporting files for a serverless application for classifying handwritten digits using a Machine Learning model in [Tensorflow](https://www.tensorflow.org/). It includes the following files and folders:
4+
5+
- app/app.py - Code for the application's Lambda function including the code for ML inferencing.
6+
- app/Dockerfile - The Dockerfile to build the container image.
7+
- app/model - A simple Tensorflow model for classifying handwritten digits trained against the MNIST dataset.
8+
- app/requirements.txt - The pip requirements to be installed during the container build.
9+
- events - Invocation events that you can use to invoke the function.
10+
- template.yaml - A template that defines the application's AWS resources.
11+
12+
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.
13+
14+
## Deploy the sample application
15+
16+
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.
17+
18+
To use the SAM CLI, you need the following tools.
19+
20+
* SAM CLI - [Install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html)
21+
* Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community)
22+
23+
You may need the following for local testing.
24+
* [Python 3 installed](https://www.python.org/downloads/)
25+
26+
To build and deploy your application for the first time, run the following in your shell:
27+
28+
```bash
29+
sam build
30+
sam deploy --guided
31+
```
32+
33+
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:
34+
35+
* **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.
36+
* **AWS Region**: The AWS region you want to deploy your app to.
37+
* **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.
38+
* **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.
39+
* **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.
40+
41+
You can find your API Gateway Endpoint URL in the output values displayed after deployment.
42+
43+
## Use the SAM CLI to build and test locally
44+
45+
Build your application with the `sam build` command.
46+
47+
```bash
48+
img-clf$ sam build
49+
```
50+
51+
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.
52+
53+
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.
54+
55+
Run functions locally and invoke them with the `sam local invoke` command.
56+
57+
```bash
58+
img-clf$ sam local invoke InferenceFunction --event events/event.json
59+
```
60+
61+
The SAM CLI can also emulate your application's API. Use the `sam local start-api` to run the API locally on port 3000.
62+
63+
```bash
64+
img-clf$ sam local start-api
65+
img-clf$ curl http://localhost:3000/classify_digit
66+
```
67+
68+
The SAM CLI reads the application template to determine the API's routes and the functions that they invoke. The `Events` property on each function's definition includes the route and method for each path.
69+
70+
```yaml
71+
Events:
72+
Inference:
73+
Type: Api
74+
Properties:
75+
Path: /classify_digit
76+
Method: post
77+
```
78+
79+
## Add a resource to your application
80+
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.
81+
82+
## Fetch, tail, and filter Lambda function logs
83+
84+
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.
85+
86+
`NOTE`: This command works for all AWS Lambda functions; not just the ones you deploy using SAM.
87+
88+
```bash
89+
img-clf$ sam logs -n InferenceFunction --stack-name img-clf --tail
90+
```
91+
92+
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).
93+
94+
## Cleanup
95+
96+
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:
97+
98+
```bash
99+
aws cloudformation delete-stack --stack-name img-clf
100+
```
101+
102+
## Resources
103+
104+
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.
105+
106+
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/)

__init__.py

Whitespace-only changes.

app/Dockerfile

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

app/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)