Before we start with amazon lambda, what all things do we need to do in the production env to run our service.
1) We setup a machine ( say a VM), with certain configurations like os, ram, space etc.
2) You setup a server there.
3) You deploy your app.
4) You start the server.
Now your application is live and ready to serve requests, but what happens when the number of requests increase from few hundred to thousands per seconds, you server begins to blow up!!!!!.
What do you do next? Yes!!!, Scale!!!!.
You will probably repeat all the above steps again to maybe start a new server. Then configure a load Balancer to route the requests to the servers.
This is really painful.
Amazon Lambda can take care of lot of things which I mentioned above.
So, what is Amazon Lambda?
Amazon Lambda is a service which runs your code that you upload to the AWS Lambda infrastructure. AWS lambda infrastructure takes care of everything, managing servers, OS maintenance, Scaling, logging etc.
When does your code run?
Amazon lambda runs your code in response to
1) events that occur at different event sources, e.g adding or deleting object in S3 or some change in DynamoDB.
2) http requests.
What languages does it support?
Node.js, Java, Python.
So as I mentioned above the lambda function is run in response to events (like adding, deleting updating etc) that occur in various event sources (Amazon s3, Amazon DynamoDB) etc. So the event based execution can be done using the Push or the Pull model.
Pull Model: Amazon lambda (your code) polls the event source(e.g S3) to check for the occurrence of the event. So the association of the lambda function (your code) to the event source is maintained at the Lambda side
Push Model: The event source has the information about the lambda function, and calls the function when the event occurs.
Using https: You can also invoke your lambda code using the https request. When you send an HTTPS request to the API endpoint, the Amazon API Gateway service invokes the corresponding Lambda function. see http://aws.amazon.com/api-gateway/
We will basically talk about writing lambda in Node.js.
Basic Components of Lambda:Handler function: This function is the starting point of your lambda function. In node you make this function available to the lambda infrastructure using the typical exports keyword.
e.g:exports.myHandler = function(event, context, callback) {
...
}
Parameters in the handler function:
1) event: using this parameter event data is passed to the handler e.g S3 may pass the object added in bucket for processing.
2) context: using this parameter you get the information about the timeout for your lambda, the log streams associated with your lambda etc.
3) callback: this is optional and used to send information to the caller. It has the typical callback format: callback( , ).
Following is a image showing the triggering of lambda expression on image upload in S3, and then resizing the image and storing it in other S3 bucket:
image describing the workflow is taken from AWS Documentation
This is the basics of Amazon lambda. To get started with your first lambda function visit http://docs.aws.amazon.com/lambda/latest/dg/getting-started.html.