DeveloperPlayBook
Python
Python
  • Introduction
  • Architecture
    • Technology Stack
    • ADR Records
  • Design
  • Bootstraping
  • Development Environment
    • Accounts (AWS, GCP, CircleCI)
  • Services/API
    • Serverless
    • Containers
    • Python
    • Firebase
    • Chatbots
    • Testing
  • Frontend
    • Serverless
    • Containers
    • Chatbot
  • Plattform
  • IAM - IAMaaS
  • Persistance - DBaaS
    • Serverless
    • Container
  • Event Driven / Streaming aaS
    • Kinesis
  • AI - AIaaS
  • Production / Reliability Engineering
  • create-k8s-secrets
  • VI
  • Tools
Powered by GitBook
On this page
  • Setup & Installation
  • Compile non-pure Python modules (e.g. C?)
  • Implementation
  • Create virtual env local, activate and deactivate
  • Install python dependency for this function:
  • Store a reference to my dependencies:
  • Implement the function
  • Protect Endpoint with API Key
  • Examples
  • Deployment
  • Deploy a project
  • Deploy single function
  • Sources:
  1. Services/API

Serverless

To be as portable as possible, even with severless, we use serverless framework. 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 Docker, the Lambda Docker Image and serverless-python-requirements . 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

#http://sourabhbajaj.com/mac-setup/Python/virtualenv.html
virtualenv venv
source venv/bin/activate
deactivate

Install python dependency for this function:

pip install boto3 google-api-python-client requests

Store a reference to my dependencies:

pip freeze > requirements.txt

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

pip install -r 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

sls invoke local --function create_api --path tests/create-api-event.json
sls invoke local -f hello

Run the unit tests

python -m unittest discover -s tests

Protect Endpoint with API Key

Examples

Deployment

Deploy a project

sls deploy

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

Test the deployed function

Or you can use the CLI and use:

sls invoke -f create_api --path tests/create-api.json

Deploy single function

sls deploy function --function hello

Sources:

  • https://github.com/serverless/examples/tree/master/aws-python-pynamodb-s3-sigurl

  • https://github.com/AnomalyInnovations/serverless-python-starter

  • https://serverlesscode.com/post/python-3-on-serverless-framework/

PreviousServices/APINextContainers

Last updated 7 years ago

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

GitHub - serverless/examples: Serverless Examples – A collection of boilerplates and examples of serverless architectures built with the Serverless Framework on AWS Lambda, Microsoft Azure, Google Cloud Functions, and more.GitHub
Serverless Framework - AWS Lambda Events - API Gateway
Logo
Logo