What Is a Lambda Function?
In a recent blog article, I talked about [Going Serverless in 2018] (https://curiousorbit.com/blog/going-serverless-in-2018/), so I figured I follow that article with a high-level overview of Lambda.
When I mention this strategy during a training session or maybe during a customer visit I often get this following question: “What the heck is a Lambda function?!”
I understand why I get this question so often. In the majority of cases when an organization decides to start using AWS for their computing needs they begin by ‘Lifting and Shifting’ a workload to the Amazon platform - while this approach is an entirely valid strategy it doesn’t mean your work is finished.
Almost immediately after shifting the workload I highly recommend you start to decouple your solution - what this means is to find ways to pull the application apart (where possible) into smaller, more discrete components. Building decoupled (or loosely coupled) solutions on AWS makes your deployment easier to scale, more durable and typically more cost-effective.
One of the easiest ways to start decoupling your application is to look for scheduled jobs your application is currently using (they are there - trust me) and transform those jobs into one (or more) Lambda functions.
This is probably a good time to answer the question - what is a Lambda function? Here’s how I think of it. A Lambda function is merely a chunk of code which performs a straightforward action (more on that later) based on a schedule or event which occurs in your environment. The infrastructure and execution of Lambda functions are handled on our behalf by AWS - Lambda is a Managed Service - another fantastic reason to see where you can use it.
Nothing magical here - generally it’s pretty simple, but there are a couple of things to consider.
Refactoring your Code
First, you’re most likely going to have to refactor some code. While AWS is working hard to support more languages in Lambda, it’s still a pretty small list. I chose Python years ago, but pick whichever language you’d like - maybe it’s a good time to learn Go?!
Keep the Function Simple
I mentioned early about keeping your Lambda functions straightforward. There’s an upper time limit on Lambda functions, and they are stateless meaning they don’t remember (by default) anything from their last execution. With those two items in mind, you should always try to have your functions complete a single - simple - task. Don’t just take an existing script and expect to Lambda to work immediately.
Bundling Libraries
Also, you should consider including all the libraries your function needs in a ZIP archive rather than relying on Lambda to provide them. I have experienced a couple of odd situations where the Lambda service is updated, and suddenly a function doesn’t work as expected…food for thought.
Least Privilege
Lambda functions rely on IAM Roles to provide temporary access to other AWS services. As with all things make sure you are practicing least privileges. If your Lambda function doesn’t need access to the EC2 service, then don’t give it access.
Monitoring your Lambda Functions
Finally, just because AWS is executing your code doesn’t mean you are no longer responsible for monitoring the execution of your functions. Lambda is integrated with AWS Cloudwatch, and you can extend your monitoring with X-Ray. Instrument your Lambda functions, so you have a clear understanding of how the functions are operating.