GithubHelp home page GithubHelp logo

harrismleng / python-serverless-api Goto Github PK

View Code? Open in Web Editor NEW

This project forked from techjacker/python-serverless-api

0.0 0.0 0.0 36 KB

Boilerplate flask app that is portable between different serverless platforms (AWS, Azure, GCP).

Home Page: https://andrewgriffithsonline.com/blog/180412-deploy-flask-api-any-serverless-cloud-platform

License: MIT License

Shell 42.81% Python 32.45% Makefile 8.08% HCL 15.45% Dockerfile 1.21%

python-serverless-api's Introduction

Python Serverless API

Boilerplate Flask app that is portable between different serverless platforms.


Platforms

Deployment and application code adaptors are being added for the following:


Platform Deployment Status
AWS Lambda AWS SAM ✔️
AWS Lambda Terraform ✔️
Azure Functions Terraform
Google Cloud Functions Terraform
Google Kubernetes Engine gcloud & kubectl
Platform Adaptor Code/Config
Local Development None 💾
AWS Lambda Python >= 3.6 Flask-Lambda-Python36 💾
AWS Lambda Python <= 3.6 Flask-Lambda 💾
Azure Functions
Google Cloud Functions

Setup

1. Create .env file and update contents

This is used to set the environment variables required for deployment and local development.

$ cp .env.example .env
$ vim .env

2. Create a virtualenv then install requirements:

$ make env
$ source env/bin/activate
$ make deps

Example Usage

1. Set Environment

Ensure you have created your virtualenv and have the necessary environment variables set (see setup instructions above).

$ source env/bin/activate
$ source .env

2. Run server

On host
$ make server-debug
In docker
$ docker-compose up

3. Manually test development server

$ http-prompt localhost:5000
GET /artists

AWS Lambda

Terraform Deployment

Ensure you have created your virtualenv and have the necessary environment variables set (see setup instructions above).

Setup

Create terraform state bucket.

$ aws s3 mb --region eu-west-2 s3://<bucket_name>

Update bucket name in /terraform/main.tf.

terraform {
  backend "s3" {
    bucket = "<bucket_name>"
    key    = "terraform.tfstate"
    region = "eu-west-2"
  }
}

Deploy

Bundle the app into a zip and deploy it using terraform.

$ ./bin/deploy

Manually Test API

$ http-prompt $(cd terraform && terraform output api_url)
GET artists

Unlike Terraform SAM doesn't upload the zip bundle so do this using the aws-cli tool.

$ aws s3 mb s3://<mybucket>
$ aws s3 cp terraform/dist/python-serverless-api.zip s3://<mybucket>/python-serverless-api.zip

Update the S3 bucket value in the SAM config.

# template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: 'Boilerplate Python 3.6 Flask App.'
Resources:
  FlaskAPI:
    Type: 'AWS::Serverless::Function'
    Properties:
      CodeUri: s3://<mybucket>/flask-app.zip

Deploy the SAM template with Cloudformation.

$ aws cloudformation deploy \
		--template-file template.yaml \
		--stack-name python-serverless-stack-sam
		--capabilities CAPABILITY_IAM

Test

$ make test
$ make lint

python-serverless-api's People

Contributors

techjacker avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.