Code Monkey home page Code Monkey logo

scaleway-serverless-apigateway's Introduction

scaleway-serverless-apigateway

This example demonstrate how to setup kong DB-less as API Gateway for Serverless Functions in Scaleway ecosystem.

It uses Serverless Compose and Scaleway Serverless Framework plugin to link and automate the deployment of some functions, get the associated endpoint and feed them in Kong configuration.

Pre-requisites:

  • gnu make
  • curl
  • jq
  • serverless cli (Serverless Compose is added as dependancy in packages.json)
  • Docker Desktop to build and push the container.
  • Having SCW_ACCESS_KEY and SCW_SECRET_KEY environment variables set.

serverless-compose.yml

This file links all the serverless.yml services located in the different folders:

├── apiGateway
│   └── serverless.yml
├── getToken
│   └── serverless.yml
├── myApp
│   └── serverless.yml
└── serverless-compose.yml

It allows passing output from one service to an other as parameter.

myApp folder

This folder contains two very simple Python functions to display html. The commands function display a link to orders using only /orders subpath.

Both these functions are deployed as "private". They can't be accessed without an X-Auth-Token header using a token which should be generated on the namesapce.

getToken folder

This folder contains a custom plugin to execute shell scripts using Serverless framework.

  • The deploy.sh script will generate a token using Scaleway API and store it as a file. It uses jq to parse the JSON output.
  • The info.sh script will output the token in a format expected by Serverless Compose.
  • The remove.sh script only will delete the token file (the token is deleted together with the namespace when myApp service is deleted).

apiGateway folder

This folder deploy a DB-less Kong container, build from the Dockerfile in the kong folder:

apiGateway
├── kong
│   ├── Dockerfile
│   ├── kong.conf
│   ├── kong.yml.template
│   └── start.sh
└── serverless.yml

Kong is an API Gateway which can either be deployed with a database or with a static configuration file (DB-less mode).

The container is started with two environment variables (the HTTP endpoints of the functions) and a secret (the token). These are provided via Serverless Compose from the output of the myApp and getToken services.

The kong.yml.template is the DB-less configuration used by the API gateway and the start.sh script will parse it together with the environment variable to build the config file used by Kong.

The yaml file define routes (default as / and orders as /orders) and associate services to them:

routes:
- name: default-route
  paths:
  - /
  service: commands
- name: orders
  paths:
  - /orders
  service: orders

The services are specific url:

- name: commands
  url: https://${COMMANDS_URL}
- name: orders
  url: https://${ORDERS_URL}

The request-transformer plugin is used to automatically add the X-Auth-Token to all queries send to the services.

plugins:
- name: request-transformer
  config:
    add:
      headers:
      - X-Auth-Token:${TOKEN}

Other parameters can be added to the file to handle more use cases: https://docs.konghq.com/gateway/latest/production/deployment-topologies/db-less-and-declarative-config/

Running the example

Just execute make to install the packages.json dependancies and execute the deployment.

scaleway-serverless-apigateway's People

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

webclinic017

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.