Giter Site home page Giter Site logo

pritamstyz4ever / serverless-gradual-traffic-shifting Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 1.0 59 KB

Serverless Plugin to shift trafffic gradually between lambda versions

License: MIT License

JavaScript 100.00%
serverless-framework serverless-plugin aws-lambda aws-cloudformation cicd

serverless-gradual-traffic-shifting's Introduction

npm version

Serverless Gradual Traffic Shifting for Lambda

A Serverless plugin to implement gradual traffic shifting of Lambda functions, using lambda traffic shifting feature

This project takes inspiration from the plugin Serverless Canary Plugin by David García.

Why do we need this?

Presently CodeDeploy Canary and Linear deployments are very restrictive in nature and allow only single interval deployment setting as default presets. Even if we create a custom deployment config we can only specify Linear/Canary with single interval and it needs the CodeDeploy application to be running. AWS Lambda provides the Traffic Shifting api's which can be leveraged to route desired traffic to any version of lambda without worrying about the CodeDeploy application restrictions.

(As per AWS the multiple interval there are features requests to respect multiple intervals and traffic percentages)

This type of traffic shifting may not be required by many projects in which 100% traffic is routed within a couple of hours and the existing Canary/Linear Configuration works fine in those cases. However, if we need a custom solution in which on day 1 say only 5% of traffic is allowed and the analytics and usage need to be stufied for a longer duration, then this feature will be helpful.

Furthermore, a custom Lambda can be created to route the remaining traffic after running analytics on the api's which update the alias with new traffic info

Installation

npm i --save-dev serverless-gradual-traffic-shifting

Usage

To enable gradual deployments for Lambda functions, the serverless.yml should be configured like this:

service: gradual-traffic-shifting
provider:
  name: aws
  runtime: nodejs6.10
  
plugins:
  - serverless-gradual-traffic-shifting

functions:
  hello:
    handler: handler.hello
    events:
      - http: GET hello
    deploymentSettings:
      alias: Live
      versionWeight: 0.20
      liveVersion: 2

Check out the working example in the example folder.

Configuration

  • alias: (required) name that will be used to create the Lambda function alias.

  • versionWeight: (optional) defines the weight of the traffic to be shifted to the new version after sls deploy, if not present shifts 100% traffic to newwer version.

  • liveVersion: (optional, but required if versionWeight is specified) defines the required version which will get the remaining traffic from the versionWeight. E.g:

Traffic configurations

If deploymentSettings section is defined with settings

functons:
  helloWorldFunction:
    handler: index.handler
    deploymentSettings:
        alias: Live
        versionWeight: 0.60 #any number from 0.00 - 1 (0 - 100 %)
        liveVersion: 2 #provived version 2 is already present
resources:
  ...

In this case 60% Traffic will be routed to new version (3 after deployment) and remaining 40% wil be routed to version 2 as specifed in config.

How it works

The plugin is based on the AWS Lambda traffic shifting feature to balance traffic between versions. It takes the versionWeight and version info in params to create the route config for lambda alias It updates the CloudFormation template generated by Serverless, so that:

  1. It creates a Lambda function Alias for each function with RoutingConfig and AdditionalVersionWeights getting the function version and function weight from the deployment settings
  2. It modifies events that trigger Lambda functions, so that they invoke the newly created alias.

Note

AWS CloudFormation creates a new version for Lambda only when there is change in the function code, so if there is no code change then specifying liveVersion setting with function version number will result in an error.

  Function version already specified in Routing Config

Please ensure the serverless.yml file has the versionFunctions flag set to true.

provider:
  versionFunctions: true

Limitations

The plugin works with Lambda functions invoked by API Gateway, Streams, Sns, S3 and needs the additional function version to be specfied. It can be further enhanced (if required) to get the existing version if present and route the remaining traffic to it.

License

ISC © Pritam Paul

serverless-gradual-traffic-shifting's People

Contributors

pritamstyz4ever avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

liamchilds

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.