Serverless Gradual Traffic Shifting for LambdaA 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
npm i --save-dev serverless-gradual-traffic-shifting
UsageTo enable gradual deployments for Lambda functions, the
serverless.yml should be configured like this:
- http: GET hello
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
versionWeightis specified) defines the required version which will get the remaining traffic from the versionWeight. E.g:
Traffic configurationsIf deploymentSettings section is defined with settings
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.
versionWeight: 0.60 #any number from 0.00 - 1 (0 - 100 %)
liveVersion: 2 #provived version 2 is already present
How it worksThe 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:
- It creates a Lambda function Alias for each function with RoutingConfig and AdditionalVersionWeights getting the function version and function weight from the deployment settings
- It modifies events that trigger Lambda functions, so that they invoke the newly created alias.
NoteAWS 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.