AWS Lambda runs your code only when something asks for it, and charges you only while it runs. You hand Amazon a function rather than a server, and it finds a machine, runs the function and tidies up afterwards.
What Lambda runs
Lambda is a 'function as a service'. Instead of renting a virtual machine and keeping it running, you upload a function: a handler plus whatever libraries it needs. You pick a runtime such as Python, Node.js, Java or .NET, and you set how much memory the function gets, from 128 MB up to 10,240 MB. CPU power scales with the memory you choose, so a slow function sometimes just needs more of it.
'Serverless' doesn't mean there are no servers. It means they aren't yours to look after. You never patch an operating system, choose an instance size or decide how many machines to run. AWS does all of that and gives you back one thing to think about: the code.
The trade is control. Each run can last at most 15 minutes, you can't log in to the machine, and anything you need to keep has to live somewhere else, such as a database or a storage bucket.
Events start everything
A Lambda function sits there doing nothing until an event invokes it. Events come from many places:
- an HTTP request, through API Gateway or a function URL;
- a file landing in an S3 bucket;
- a message arriving on an SQS queue;
- a schedule in EventBridge, like a cron job.
How the caller waits depends on the source, and it changes how you handle failure:
- Synchronous: the caller waits for the answer. An HTTP request works this way, so an error goes straight back to the user.
- Asynchronous: Lambda queues the event, replies 'accepted' at once and runs the function afterwards, retrying if it fails. S3 notifications work this way.
Wondering how this was made? How we make these