Skip to content

dudeperf3ct/12-serverless-deploy

Repository files navigation

Serverless

In this exercise, transformers sentiment classifier fastapi application is deployed using Serverless Framework

Serverless Version

sls --version

Framework Core: 3.1.0 (standalone)
Plugin: 6.0.0
SDK: 4.3.0

Serverless Framework

Serverless Framework provides hassle free develop, deploy, troubleshoot and secure serverless applications.

Under the hood, the Serverless Framework is deploying your code to a cloud provider like AWS, Microsoft Azure, Google Cloud Platform, Apache OpenWhisk, Cloudflare Workers, or a Kubernetes-based solution like Kubeless.

Pre-requisites

Run

Locally

Test the sentiment classifier model

docker build -t sentiment -f project/sentiment/Dockerfile.sentiment project/sentiment/
docker run --rm -it sentiment

Run tests using pytest

docker build -t sentiment -f Dockerfile.test .
docker run -p 8000:8000 -it -v $(pwd):/app --entrypoint bash sentiment
pytest --cov

Build a docker image and test the application that will be used to deploy as AWS Lambda function.

docker build -t sentiment -f Dockerfile.prod .
docker run -p 8000:8000 sentiment
curl -X 'POST' \
  'http://0.0.0.0:8000/classify?input_text=i%20like%20you' \
  -H 'accept: application/json' \
  -d ''

Deploy using serverless framework

Reference: Serverless Docker deploy

We will now deploy this application using serverless. This will add the application to Monitor on serverless dashboard.

# add monitoring to serverless dashboard
serverless

Deploy the application using Serverless Framework.

serverless deploy --stage dev --verbose

Once application is deployed, get information about deployment.

serverless info

Test application

Vist <api-endpoint>/docs to see if Swagger UI is available.

curl -i <api-endpoint>

Test the classification endpoint

curl -X 'POST' \
  '<api-endpoint>/classify?input_text=i%20like%20you' \
  -H 'accept: application/json' \
  -d ''

Fetch logs

serverless logs -f sentimentapi

Remove service

sls remove --stage dev

Additional: CI/CD can be integrated using Serverless Dashboard.