What is AWS Lambda?
An AWS Lambda function is essentially a small, single-purpose piece of code that you write and upload to AWS. It is designed to be invoked in response to specific events, like changes in data, HTTP requests, or even scheduled tasks. Once invoked, the code inside the Lambda function is run automatically, thus creating nearly endless possibilities for automation.
Here’s an example of something that can be automated using AWS Lambda: when an image is uploaded to an AWS S3 storage bucket, a Lambda function is triggered that processes the image and creates resized versions in different resolutions. This ensures that images are appropriately sized for various uses, such as thumbnails or high-resolution displays.
Lambda versions
Every time you update your Lambda function code, AWS creates a new version of that function. A version is a snapshot of your Lambda function code and its configuration at a specific point in time. Each version is immutable, which means once it’s published, it cannot be changed. This immutability ensures that your production environment is stable and predictable.
- Versioning: When you publish a version of your Lambda function, AWS assigns it a unique version number. For example, the initial version of your function might be
1
, and subsequent updates would create versions2
,3
, and so on. - $LATEST: Note that the code for AWS Lambda that you see by default, is for the version called $LATEST.
Lambda aliases
Aliases are pointers to specific versions of a Lambda function. They provide a way to manage and switch between versions of your function easily without modifying the code itself.
Think of aliases as domain names pointing to specific IP addresses. Just as you can change an IP address behind a domain name, you can change the version number an alias points to. If you invoke an alias of a Lambda function, whatever version of that function the alias is pointing to, will be invoked. Versions and aliases are very useful features for managing different environments inside the same AWS Lambda function.
Managing environments using versions and aliases of AWS Lambda
This is how you can combine versions and aliases to construct a robust environment for your AWS Lambda function. Let’s say you have deployed your code on your Lambda function, and also published a number version, say version 1.
Creating aliases: create an alias
dev
pointing to version$LATEST
, andtest
andprod
aliases both pointing to version 1.Updating aliases: let’s say you change your code and redeploy it to your Lambda function. You’ve done a few checks and think the new code is ready for testing. You can publish another numbered version, 2. Update your
test
alias to point to version 2. Once testing is completed, you can have yourprod
alias point to version 2 as well.
Quick rollbacks: If a newly deployed version in production has issues, you can quickly rollback by updating the
prod
alias to point back to a previous stable version.Version control: With versions, you can always track which version of the code is running in which environment. This makes debugging easier and ensures consistency across deployments.
Gradual rollouts: You can use aliases to implement blue/green deployments or canary releases by gradually shifting traffic between different versions. For example, direct 10% of traffic to the new version while keeping 90% on the old version, and then fully roll out once the new version is validated.
Lambda versions and aliases are essential tools for managing your Lambda functions across different environments. They allow you to isolate development work, ensure stable production deployments, and quickly roll back if issues arise. By using these features strategically, you can enhance your deployment workflows, reduce risks, and improve the overall reliability of your serverless applications.