Using SAM deploy new version of Lambda using CodeDeploy. The CodeDeploy Canary Deployment will shift 10% of traffic for 10 minutes to new alias of Lambda function then it’ll run tests and if passed shift whole traffic to the new alias.
Using SAM deploy new version of Lambda using CodeDeploy. The CodeDeploy Canary Deployment will shift 10% of traffic for 10 minutes to new alias of Lambda function then it’ll run tests and if passed shift whole traffic to the new alias. Sample template with resources will be used and above Lambda function and CodeDeploy, an API Gateway and IAM Roles will be created.
- Create and build SAM package
- Deploy SAM package
- Trigger a new deployment of Lambda function using CodeDeploy
- AWS account
- SAM CLI installed
- Create a new application in the working folder by running this SAM command in console:
sam init --runtime python3.9
Then choose AWS Quick Start Templates ans package type zip. And then Hello world Example this will clone the app to local folder.
- Go to sam-app folder and run:
- Go to template.yaml and after Events add those lines of code:
AutoPublishAlias: live DeploymentPreference: Type: Canary10Percent10Minutes
This will use CodeDeploy to switch from aliases of Lambda function in Canary type of deployement.
- Re-run sam build after the code update.
- Time to deploy the build.
sam deploy --guided
This will create a changeset in CloudFrotmation and ask if you want to deploy all of the resources:
So now Lambda function, API, CodeDeploy, IAM has been deployed. This environment is up and running.
- Time to make some changes to the Lambda fuction code. Then again run the sam commands and deploy the updated package. When checking if the CodeDeploy Canary deployment is in progress it show that indeed it is, which will take 10 minutes as stated in sam template:
After ten minutes of this canary traffic shifting it should go into Step and shift 100% of traffic to new Lambda alias.
- The Lambda function has been shifted to the new version
- Time for clean-up, run the delet command, that will delete the S3 bucket and the CloudFormation stack