I previously forked serverless-flask which was good to learn from but I wanted to create a new template for myself and others to use on a seperate project. I wanted clear documentation on how to setup and deploy a new flask microservice. Using custom domains and setting up base_url's for multiple endpoints based on your git resposity branch so multiple developers can deploy test environments easily.
- Getting Started
- Creating a flask microservice
- Deploying your microservice to Amazon Web Services
- Adding a custom domain to your microservice
-
npm
For installing nodejs packages to extend serverless. How to install nodejs & npm -
npm install -g serverless
This is the serverless cli (npm
must be installed) -
python
The primary language of a flask microservice. How to install -
pip
For installing pything packages and creating your microservice How to install
While I won't go through how to setup an AWS account, once you have an account read this to find out how to get your access key and secret. Then run the following to store your credentials for serverless to use when deploying:
export AWS_ACCESS_KEY_ID=1234-your-key-here
export AWS_SECRET_ACCESS_KEY=5678-your-secret-here
Generating your first Flask microservice with serverless is realatively easy and straight forward. As long as you meet all the above requirements you will only need to run a few lines to serve your first GET request.
$ sls install --name my_new_microservice --url https://github.com/NotOddity/sls-flask.git
$ cd my_new_microservice
$ npm install
$ python3 -m venv .venv
$ source .venv/bin/activate
(venv)$ pip install -r requirements.txt
$ sls wsgi serve
* Running on http://localhost:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 123-456-789
$ curl http://localhost:5000/
{"message": "Hello World!"}
Serverless makes this super simple, just run sls deploy
(This can take a while)
You should get this
Service Information
service: sls-flask
stage: dev
region: us-east-1
stack: sls-flask-dev
resources: 13
api keys:
None
endpoints:
ANY - https://ucpykgtlf2.execute-api.us-east-1.amazonaws.com/dev
ANY - https://ucpykgtlf2.execute-api.us-east-1.amazonaws.com/dev/{proxy+}
functions:
api: sls-flask-dev-api
Lets test your new aws endpoint
$ curl https://ucpykgtlf2.execute-api.us-east-1.amazonaws.com/dev
{"message": "Hello World!"}
- Ensure you have the domain you wish to use avaiable on your AWS Console
- Once you have a domain you need to request a certificate
npm install serverless-domain-manager
custom:
customDomain:
createRoute53Record: true
domainName: my-domain.com
certificateName: my-domain.com
stage: dev
basePath: api
Again to deploy (or re-deploy) just type sls deploy
and give it a while
Result:
Service Information
service: sls-flask
stage: dev
region: us-east-1
stack: sls-flask-dev
resources: 13
api keys:
None
endpoints:
ANY - https://my-domain.com/api/
ANY - https://my-domain.com/api/{proxy+}
functions:
api: sls-flask-dev-api
Lets test just to make sure
$ curl https://my-domain.com/api
{"message": "Hello World!"}
Coming Soon
Coming Soon
Coming Soon