Coder Social home page Coder Social logo

alexellis / funker-dispatch Goto Github PK

View Code? Open in Web Editor NEW
37.0 4.0 3.0 77 KB

An API Gateway for serverless functions on Docker Swarm (deprecated, see: openfaas.com)

Home Page: https://www.openfaas.com

License: GNU General Public License v3.0

JavaScript 96.77% Shell 3.23%

funker-dispatch's Introduction

Docker Swarm serverless API gateway

This project provides a PoC for serverless functions backed by Docker Swarm. This reposistory hosts an API gateway / HTTP dispatch for Amazon AWS triggers such as Alexa SDK, S3, DynamoDB, CloudWatch or Github webhooks.

The Amazon Alexa voice service will invoke either an AWS Lambda function or an HTTPs endpoint. In this project we extend the funker framework/idea to create a Docker Swarm which is capable of running our own serverless functions across a high-availability swarm.

Video overview & demo

Watch the high level overview of what serverless is, the aims of my PoC and for the live demo with an Echo Dot.

Changelog

13 Jan 2017

12 Dec 2016

  • Pushed remote branch services/json - looking into a manifest file for defining available functions. Includes support for Github push webhooks.

11 Dec 2016

  • Default platform is now x86_64, Raspberry Pi (ARM) accessible by Dockerfile.armhf
  • sample_request.json's intent matches README.md

8 Dec 2016

  • Validation and override added for applicationId in JSON request
  • Validation added for JSON body
  • Support for Python functions with working sample
  • Docker Captains handler - counts captains via HTML scrape
  • Hostname handler - returns container hostname for showing round-robin from swarm
  • Allow mounting of -v /var/run/docker.sock:/var/run/docker.sock for checking services exist and validation

Backlog

  • Dynamically create swarm services through remote API
  • HTML tester page for API gateway with test events
  • Register handlers and Intents via YAML or JSON file
  • Handle Github webhooks in addition to the Alexa SDK
  • Upload page for function definitions

Technical architecture:

The Alexa custom skill invokes our API gateway or HTTP dispatcher. Docker's attachable swarm network and name resolution allows us to lookup a matching Docker service by hostname. The funker-node library is then used to invoke the serverless function and collect the response which is returned to AWS. Alexa will then speak out the response.

Functions are written to implement a simple TCP socket on port 9999. The original concept for the functions was put together by Ben F / Justin Cormack from the Docker team.

The code is written in Node.js which is cross-platform. The Dockerfiles provided have a base image designed for ARM/Raspberry Pi because I'm running this experiment on my Raspberry Pi 2 Swarm:

See also: Getting started with Docker & Swarm on the Raspberry Pi

Example of funker handler function in Node.js

This function finds the count of people in space by querying a remote JSON API over HTTP.

This Node.js function scrapes Docker's website and counts HTML elements to determine the number of Docker captains.

A Python version is also available: dockercaptains-python-funker

Request validation

The initial implementation has basic validation for the following:

  • request matches Alexa skill JSON format
  • applicationId is allowed to execute (must match index.js or environmental variable authorizedApplicationId)
  • swarm service exists (and can be invoked)

Usage:

Make sure your nodes are running Docker 1.13-rc or newer. A quick upgrade can be to untar the Docker binaries from the Releases page straight over the top of a 1.12 installation from curl -sSL get.docker.com |sh.

git clone https://github.com/alexellis/funker-dispatch
cd funker-dispatch
git clone https://github.com/alexellis/funker-node
git checkout light_weight_refactor
cd ../

docker build -t funker-dispatch .

docker network create --attachable -d overlay funker
docker run --net=funker -p 3000:3000 --name dispatch -v /var/run/docker.sock:/var/run/docker.sock funker-dispatch

Once your dispatch container is running head over to the HelloWorldIntent repo, build your image and create a service. If you have a mutli-node swarm, then push the container to the Hub or a registry first, for a single node just use the image name.

Once the dispatch container is running and the HelloIntent service has been created you can configure your Alexa Skill in the Alexa SDK console to use "HTTPs" for its invocations. An Alexa skill will not invoke a HTTP insecure endpoint, for that reason if you are tight on time then use ngrok to set up a quick HTTPs gateway.

ngrok http 3000

Set up an utterance to fire off the request. I used:

Sample Utterances:

HelloIntent how many people are in space
HostnameIntent give me a hostname
CaptainsIntent find out how many docker captains there are

Intent Schema

{
  "intents": [
    {
      "intent": "HelloIntent"
    },
    {
      "intent": "CaptainsIntent"
    },
    {
      "intent": "HostnameIntent"
    }
  ]
}

If you not have access to an Echo / Dot curl will allow you to bypass the voice portion completely (just use the sample .json file in the repo).

Here's an example of invoking the (HelloIntent) with curl:

cd funker-dispatch
curl -Sv -H "Content-Type: application/json" -X POST http://localhost:3000 -d @./sample_request.json

You should see a response like this:

{"version":"1.0","response":{"outputSpeech":{"type":"PlainText","text":"There's currently 6 people in space"},"card":{"content":"There's currently 6 people in space","title":"People in space","type":"Simple"},"shouldEndSession":true},"sessionAttributes":{}}

Get in touch / feedback

Please get in touch on Twitter @alexellisuk

funker-dispatch's People

Contributors

alexellis avatar

Stargazers

 avatar Curtis avatar  avatar Monami Sharma avatar Senthil_M avatar Martin Holzhauer avatar Dr Luke Angel avatar Facundo Olivera avatar /dev/fd/ran avatar John Jediny avatar Alex Casalboni avatar Grégory Horion avatar Eric Briand avatar Luca G. Soave avatar John Henry avatar Michael Romanenko avatar M Haidar Hanif avatar  avatar  avatar Kyle J Carlton avatar Alec Cunningham avatar timothy avatar Goncalo Gomes avatar Joe Tague avatar Michael Sheedy avatar ⊣˚∆˚⊢ avatar Edgard Pineda avatar Svenson avatar Edward Vielmetti avatar Chan Hee Lee (MarkiiimarK) avatar Robertus Suhendro avatar Mahmut Gundogdu avatar Lucas avatar Philipp Muens avatar Elijah Wright avatar  avatar Florent Bourgeois avatar

Watchers

Edward Vielmetti avatar James Cloos avatar  avatar  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.