AWS Lambda versioning and alias , a short Deployment script
While doing the development as part of creating different Lambda function as part of the middle ware development our team had to do the following activity again and again
- Zipping the node modules and few other dependencies into a file
- Opening the AWS console
- Navigating to the respective Lambda function
- Uploading the zip file
- Publishing a new version of the function
- Going to Alias and then pointing the latest version to the right Alias ( we had multiple alias ,Sanbox, Sanbox2, Dev, Test)
This was taking a considerable time for the team as a single line of code change to be tested we needed to deploy this in the Sanbox2 ( as per our enviornment ) and then test it. A quick stock of time wasted came out to be as below
Additional benefit apart from effort savings
1. Running unit testing
2. Reduction of Un necessary updates in S3 bucket
3. Considerable reduction in Debugging time
We could have thought different possible solution
- Use serverless framework https://serverless.com/
- Or used AWS SDK (S3 copy) and batch file
- Or use DevOps in Sandbox environment
But all of the above seemed to be too much for simple activity so we thought of going towards old developer route of creating a very crude hack ,of using aws cli (we needed to configure AWS CLI with secret key and access key and also configure the default region)to do all the activity which look something like below
# Remove zip file if already exitrm index.zip# Creating zip filezip -r index.zip *# Update lambda function. we assumed lambda name to be same as the current directory name where from we will be executing the scriptlambdaName=${PWD##*/}aws lambda update-function-code — function-name $lambdaName — zip-file fileb://index.zip# Publish versionaws lambda publish-version — function-name $lambdaName# Get latest versionversion=$(aws lambda publish-version — function-name $lambdaName — description “updated via cli” — query Version | bc)# Map alias for our case this was SANDBOX2 to latest versionaws lambda update-alias — function-name $lambdaName \ — function-version $version — name SANDBOX2# Create new alias# aws lambda create-alias — function-name loyalty-gift-card-link-sl \ — function-version 2 — name SANDBOX2
This may not be the most robust code but this did the job what we wanted to achieve