Serverless

To be as portable as possible, even with severless, we use serverless frameworkarrow-up-right. It allows us to deploy our functions across multiple clouds like AWS, GCP, Azure and IBM OpenWisk.

Setup & Installation

Make sure you have your Developer Environment Setup

npm install serverless -g

In your repo create your serverless template for Python

mkdir notes-app-api
cd notes-app-api
sls create --template aws-python3

Compile non-pure Python modules (e.g. C?)

To compile non-pure Python modules, install Dockerarrow-up-right, the Lambda Docker Imagearrow-up-right and serverless-python-requirementsarrow-up-right . Enable dockerizePip in serverless.yml and serverless deploy again.

npm init
npm install --save serverless-python-requirements

To configure our serverless.yml file to use the plugin, we'll add the following lines in our serverless.yml:

serverless.yml
plugins: 
    - serverless-python-requirements 
custom: 
    pythonRequirements: 
        dockerizePip: non-linux

Implementation

Create virtual env local, activate and deactivate

Install python dependency for this function:

Store a reference to my dependencies:

Optional: Re-install the dependencies from the requirements.txt:

Implement the function

  1. Define the function in the serverless.yml including the events (e.g. http) that trigger the function and the handler

  2. Implement the unit test: what is the best serverless unit test framework for python?

  3. Implement the handler function in handler.py

  4. Test the function locally

Run the unit tests

Protect Endpoint with API Key

Examples

Deployment

Deploy a project

The deployment takes very long. I have to see how to optimize this.

Test the deployed function

You can use the endpoint endpoint and use Postman to make a "real world" request:

Or you can use the CLI and use:

Deploy single function

Sources:

Last updated