A Serverless plugin to implement gradual deployment of Lambda functions
npm install serverless-gradual-traffic-shifting
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.
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
npm i --save-dev serverless-gradual-traffic-shifting
To enable gradual deployments for Lambda functions, the serverless.yml should be configured like this:
``yaml
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.
* 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:
If deploymentSettings section is defined with settings
`yaml`
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.
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
3. It modifies events that trigger Lambda functions, so that they invoke the newly created alias.
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.
`yaml
provider:
versionFunctions: true
``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.
ISC © Pritam Paul