AWS Lambda is Amazon’s serverless compute service. You can run your code on it without having to manage servers or even containers. It’ll automatically scale depending on how much work you feed into it.
You can use it in data pipelines or to respond to web requests or even to compose and send emails. It’s the jack-of-all-trades way to execute code in the AWS cloud.
Although intended for use in the cloud, you can absolutely run Lambdas locally during development. When you do run Lambdas in the cloud, you’ll only pay for what you use.
In some cases, this can save significant sums of money compared to the cost of running VMs or containers
While I’ve already touched on some of the reasons you’ll benefit from using AWS Lambdas, I want to focus on 10 specific reasons in more detail.
1. Most Major Languages Are Supported out of the Box
AWS Lambdas can be used today to run
- and even PowerShell.
Although you might be used to writing statefully in these languages, you do have to remember that Lambdas are stateless. They just run your function once, then dispose of everything.
You’ll be able to persist state in your database and rehydrate it of course, but for transactions, you’ll make it all happen in one go. If you’re used to stateful object-oriented programming, you might have to shift your style a bit to utilize the Lambdas.
And if your favourite language is not supported out of the box, don’t worry. AWS provides the ability to provided your own custom runtime. A custom runtime is exactly what you think it is, you can provide support in any programming language including Cobol, if that’s your preferred language.
2. You Only Pay for What You Use
As mentioned in the introduction, you’ll only pay for what you use. Billing for functions is based on 100 ms intervals.
Interestingly, this creates an incentive to use more efficient code if you’re considering the costs. Also, you can tune the maximum resource usage of each Lambda, which affects the base price for your function executions.
To give you a quick idea of what it might cost, consider a function that simply logs an Amazon Cognito authorization. With a minimum memory setting of 128 MB, this Node.js function simply does the following:
This logs to an AWS S3 bucket. It runs in under 100 ms, so the execution time charged for each run is 100 ms. The estimated charges for 10 million executions of this Lambda function per month is around $2.
And that’s only part of the cost savings. Lack of infrastructure maintenance is the bigger story here.
3. There’s No Infrastructure to Manage
Managers like this benefit, but there are plenty of reasons for developers to like it too. How many times have you turned a server upsidedown to resolve an issue with a server patch?
As a developer, would you want to spend a good portion of your time managing servers? Or would you rather be coding up some cool stuff that matters?
Sure, it’s a fun engineering project to fine-tune a cluster of containers or a VM using shared resources, but you can still have that if you’d like.
AWS Lambdas are simply another tool in your toolset. You can use them in many applications, but not every situation is right for a Lambda.
The benefit of having no infrastructure to manage is that you can spin up a new Lambda without having to add a new resource to your infrastructure.
It’s a good way to extend your existing infrastructure or deploy new services in a microservices
architecture. And for some services on AWS, Lambdas are the only real way to perform certain tasks such as logging Cognito login requests.
And because there is no infrastructure to maintain you also don’t need to take care about scalability. AWS Lambda automatically scales to support the rate of incoming requests without requiring you to configure anything.
There is no limit to the number of requests your code can handle, the performance remains the same when the number of requests increases. Just remember that your code needs to be stateless in order to scale.
4. You Can Edit Directly Online
This means you can create or edit a Lambda using your mobile device. It saves each version too, and you can switch between new and old versions. And you can map versions to “environments” using aliases.
Don’t get me wrong. You can still create and edit functions locally and deploy them using various deployment methods.
It’s just that you might come across a situation where it would be handy to create a function when you aren’t quite able to get to your favorite IDE.
You can still download the functions you create in the web console and add them to source control if you choose. It’s just nice to have options.
5. It Connects to API Gateway and Other Connection Points
In a manner of speaking, the API Gateway is the portal to your microcosm within AWS. It’ll connect to many of the cloud services directly, but when you need flexibility, Lambda is your go-to.
See, it’s where you put custom code to process requests that come in through the API Gateway. Use a Lambda when you need to access several services or do custom processing.
As data flows through services, you use Lambdas to run custom code on that data stream. This is useful in a Kinesis Pipeline that’s receiving data from things like IoT devices.
And besides, Lambdas can connect to so many of the other AWS services. It basically operates as an interlink between those services.
It’s useful when you want to send text messages via Amazon SMS
based on a trigger. Or you can use Lambdas to create tables in DynamoDB. They can connect to Code Commit or even process your Alexa Skills and drive your home automation system via Alexa Smart Home.
At this point, you’re probably wondering how you might end up developing Lambdas locally. Let’s take a look at how you might do this.
6. You Can Run AWS Lambdas Locally
Recently, AWS introduced the SAM (serverless architecture model) CLI. This is the tool for running serverless
architectures, the core of which are Lambdas, locally.
With SAM, you create a CloudFormation template to define the application. The template is written in YAML, which makes it about as clean as you can get.
Then you can invoke the Lambda locally using the SAM CLI. You’ll pass to the Lambda either through the output of a SAM CLI “generate-event” command or by passing a JSON file with static data. SAM requires Python, Docker, and AWS CLI.
Another option to run Lambdas locally is the serverless framework
. The serverless framework emulates AWS using Node.js. It has some limitations when it comes to Lambdas: it can only run Node.js, Python, and Java functions.
Another thing to keep in mind is that the emulation isn’t perfect. There are some contextual things that won’t quite translate, especially when invoking nested Lambdas. But for the most part, and for cross-cloud interoperability, it has its benefits compared to AWS SAM.
7. AWS Lambdas are Event Driven
AWS Lambda functions gets invoked only when a particular message or event is generated. Events can be something such as an object getting uploaded to an S3 bucket or a row getting deleted from a DynamoDB table, an HTTP call to your function and so on.
Consider the following scenarios: when a user uploads a file in a bucket an event is emitted that can trigger your Lambda function. Or when a new order is placed in your online store a Lambda function can be invoked to send an email.
And this is the true power of event driven architectures: the events are triggered asynchronously so that your application will not block until the execution of the Lambda is finished.
8. AWS Lambda Has Strong Security Support
Security in the cloud is important for any service and AWS Lambdas are no exceptions.
AWS is responsible for protecting the infrastructure that runs lambdas but the developer is responsible for the access of the services that will trigger execution and the services that are accessed by the functions.
You can apply IAM security fine grained access policies like you do for any AWS service. Identity and Access Management (IAM) is an AWS service that helps an administrator securely control access to AWS Lambdas.
9. You Can Orchestrate Your Lambdas Easily
So you’ve got a bunch of functions deployed but how do you orchestrate everything?
You can make use of AWS Step Functions.
AWS Step Functions lets you coordinate multiple AWS services into serverless workflows so you can build and update apps quickly.
With Step Functions you can create workflows that trigger Lambda functions using sequential, parallel, branching, and error-handling steps.
Step Functions have State Machines where each step can execute a Lambda function through a series of steps, with the output of one step acting as input into the next. It allows looping, branching, wait, timeout, retries on fails, etc
But the best use case is that it can be used to eliminate Lambda’s time execution limitation by having workflows of sequential function calls.
10. You Can Monitor AWS Lambda With Scalyr
You can write the lambda logs to the AWS CloudWatch as usual and then use Scalyr CloudWatch logs importer
to stream your logs to Scalyr in real time.
All you need is a Scalyr Write Logs API key to get started. Try Scalyr with your AWS CloudWatch logs now.
Key Takeaways and Benefits
Whatever way you choose to run Lambdas, they might just be the way to go when it comes to developing serverless applications.
You can write them in different languages, you can connect several AWS components together, and you only pay for what you use—all without managing any infrastructure.
All you have to do is code the function, configure them, and deploy them. These 10 reasons should be more than enough to inspire you to consider using AWS Lambdas in your near future.
You could even start right now by pulling down the AWS SAM CLI
and running it locally. Give it a try!
This post was written by Phil Vuollet. Phil uses software to automate processes to improve efficiency and repeatability. He writes about topics relevant to technology and business, occasionally gives talks on the same topics, and is a family man who enjoys playing soccer and board games with his children.