Lambda is an AWS service that allows you to run your code completely in the cloud without the need for on-prem servers. As the world has shifted away from on-prem and toward serverless computing, Lambda has grown in popularity: 1700 companies report using Lambda as part of their data infrastructure in StackShare.
In a nutshell, Lambda allows you to portion out pieces of your own code and trigger those pieces to run when certain events, determined by you, occur.
You can write your code in your own code editor and upload it to Lambda, or you can write directly in the Lambda UI itself.
Once you’ve written code, you need to customize your configuration with the amount of memory and max execution time for each function and when to run it. You can also integrate your function with various AWS services like S3, DynamoDB, Kinesis, SNS, etc.
You can set a trigger to run your packaged code based on certain events. When your event occurs, your function runs and executes your code, then shuts off to wait for the next time it triggers.
Check out AWS docs for sample events.
Because your code is running only some of the time (just when it’s triggered), Lambda can be economical. You’re not charged for when it’s not running. Lambda pricing is based on number of requests (20 cents per 1M requests) and their duration (.00166667 cents for every GB-second). There’s also a free tier of Lambda for up to 1M free requests per month and 400,000 GB-seconds of compute time per month.
Lambda is highly flexible and can be used for a number of different use cases. There are basically two types of Lambda invocations: synchronous and asynchronous.
With synchronous invocations, services depend on the response from your function. If there are errors, they will propagate to the caller.
For example: If someone makes an API request on your website and you’re using API Gateway, API Gateway can pass that request on to Lambda, which then performs the function and passes it back to API Gateway, which then presents the user with the result.
For asynchronous invocations, Lambda puts the event in a queue to pass to your function. The other service registers the item queued and gets a success response immediately. In case of error, Lambda will try again. Asynchronous invocations are probably the most common use case for Lambda, so we’ve included a couple of examples this time.
Example 1: Say you run an email newsletter and have a database of subscribers in DynamoDB. This database is accessed by several systems determining who is interested in which topics, what the spread of time zones are among your subscribers, etc.
When someone unsubscribes, data about the user that unsubscribes is sent to Lambda, and the function written to handle that data is triggered. As the function executes, it can trigger other functions that do cleanup work, as well as delete any associated data from S3, RDS, and anywhere else it might live.
Example 2: If you’re running a real-estate app, people probably want to see a lot of photos displaying the outside and inside of every home listed. You might want to determine default image sizes for different sections of your app or resize certain images for thumbnails.
You can trigger a Lambda function that will automatically take an uploaded photo and adjust it to the sizes you need and store them asynchronously.
Out of the box, Lambda supports:
However, Lambda also provides a runtime API to allow you to use additional programming languages.
Lambda can read events from Kinesis, DynamoDB, and SQS.
ELB/ALB, API Gateway, Cloudfront, and more invokes functions synchronously with Lambda.
Services that use asynchronous invocations include:
For a full list, check out the AWS Lambda docs.
Lambda is a convenient, cost-effective way to run your code in the cloud instead of on-prem servers. It's highly flexible and is useful in a number of situations.