https://github.com/gabriele-mastrapasqua/serverless-lambda-flask
Sample serverless framework lambda using flask and python 3.9
https://github.com/gabriele-mastrapasqua/serverless-lambda-flask
aws-lambda flask python3 serverless
Last synced: 3 months ago
JSON representation
Sample serverless framework lambda using flask and python 3.9
- Host: GitHub
- URL: https://github.com/gabriele-mastrapasqua/serverless-lambda-flask
- Owner: gabriele-mastrapasqua
- Created: 2022-06-06T07:09:38.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-06-23T15:26:11.000Z (almost 4 years ago)
- Last Synced: 2025-01-20T17:40:30.825Z (about 1 year ago)
- Topics: aws-lambda, flask, python3, serverless
- Language: Python
- Homepage:
- Size: 15.6 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# python 3.9 Flask serverless lambda
Sample Serverless framework using API gateway that calls a lambda with python 3.9 with Flask.
## prerequisites
Setup the prerequisites:
- python3 and virtualenv
- nodejs: https://nodejs.org/en/download/
- servless framework:
```
npm install -g serverless
```
## setup and use a virtualenv
See also: https://www.infoworld.com/article/3239675/virtualenv-and-venv-python-virtual-environments-explained.html
- setup a new virtualenv:
```
virtualenv venv -p python3
```
- activate the virtualenv (unix / macos):
```
. venv/bin/activate
```
or on Windows using the Command Prompt:
```
venv\Scripts\activate.bat
```
## setup flask support for lambda on serverless framework
install (in the virtual env) the dependencies:
```
pip install Flask
pip freeze > requirements.txt
```
```
sls plugin install -n serverless-wsgi
sls plugin install -n serverless-python-requirements
```
## first time setup for serverless framework to work
on aws create a IAM user for programmatic access: https://www.serverless.com/framework/docs/providers/aws/guide/credentials/
## change service name of the new api
Change service name 'hello-world-lambda' in serverless.yml file with a new name.
## local development (on port 5050)
```
python app.py
```
## Using Lambda custom url for direct access the lambda without api gateway
Using this new functionality will remove the max timeout of 30s from API Gateway.
If you want instead use an API GW, comment those lines and uncomment the lines above in serverless.yml.
```
# 1 - Enable direct Lambda Function URL, this can bypass API Gateway 30s timeout
url:
# Allow CORS for all requests from any origin
cors: true
```
See also: https://www.serverless.com/blog/aws-lambda-function-urls-with-serverless-framework#:~:text=Unlike%20API%20Gateway%2C%20Function%20URLs,(up%20to%2015%20minutes).
## Using Lambda through an api gateway
This will have max 30s timeout and more configuration options available.
Uncomment those lines in serverless.yml:
```
# 2 - Enable lambda through API Gateway HTTP API. Note, using API Gateway will force a 30s max timeout
# on all endpoints
#events:
# - httpApi: '*'
```
## test sample endpoints
```
curl http://localhost:5050/ -H 'Content-Type: application/json'
curl http://localhost:5050/quote -H 'Content-Type: application/json'
curl -X POST http://localhost:5050/testpost -H 'Content-Type: application/json' -d '{"message":"this is a test!"}'
```
## DEPLOY to AWS
```
export AWS_ACCESS_KEY_ID=
export AWS_SECRET_ACCESS_KEY=
sls deploy
```
or in windows:
```
SET AWS_ACCESS_KEY_ID=
SET AWS_SECRET_ACCESS_KEY=
sls deploy
```
after some seconds it should prints an AWS api gateway url like this:
https://XXXXXXX.execute-api.us-east-1.amazonaws.com/
now you can access your flask apis like GET 'https://XXXXXXX.execute-api.us-east-1.amazonaws.com/quote'!