INTRO TO AWS LAMBDA
AWS has a great set of tools for running your application in the cloud. One of them Amazon calls Lambda.
INTRO TO LAMBDA
As the service’s name suggests, Lambda is about executing one function with arguments, processing and returning some output. It has an attractive pricing model, as AWS charges you for each started 100ms execution time along with the cost for allocated memory. Each execution has five minutes before timing out. There is also a limitation in memory, up to 1.5GB.
CREATING A SAMPLE LAMBDA FUNCTION
To create a new function, you need to open AWS console from a browser. You can upload Lambda functions from a terminal console, but let’s keep things simple and execute it from the visual editor.
Let’s head to the Lambda home page. The first time you open the page, AWS presents to you a default screen where you can get started with the AWS Lambda service.
This view offers you various of blueprints. I strongly encourage you to check them all afterwards. There is plenty of sample examples, so it is one of the great ways to start looking into the service.
For now let’s start with something plainly simple: creating a function which takes our input and displays it in the console. Click the Skip button at the bottom right-hand corner of the page
That takes you to the page where you can preconfigure triggers. Triggers are responsible for starting a function for a certain action. Lambda is a little tightly coupled to other AWS services, and we definitely can take advantage of this. When you click on the square with a greyish striped border, you are presented with a list of other services which can execute the function when there is the event.
For instance, such event can be a new file uploaded to S3 or a request made to the API Gateway service. There are lots of possibilities, and that is great. As Lambda offers scalability, you might find that Lambda can act as a scalable back-end for a huge amount of requests.
Let’s click the Next button and move the section about configuring a new function. For this case, we use Node.js 4.3. Add a descriptive name for the function, e.g.ProcessingTestData, create a dummy description, the description is required.
Now you can create the code. Right now there are three ways to put your code into Lambda:
Upload zipped files
Use link to uploaded zipped files to S3
Let’s keep this plainly and edit the function in the browser.
Lambda requires having exported a function handler which can take up to three params:
event – data passed while executing the function
context – useful info about runtime
callback – used for returning data to the caller
Function template created by AWS:
Let’s display in the console log our first name, last name and company. Then let’s return to the caller our secret password passed from the event object:
We can use console.log, and AWS stores Lambda logs in the CloudWatch service.
Now let’s create a new role from the template and assign it appropriate Role name:
In the next section, you can configure memory, timeout and VPC.
Let’s stick to the default settings. Then click the Next button.
Now you can see a concise summary of settings for the Lambda instance. Let’s go and click Create function. It takes a while, but after a few seconds AWS redirects you to the page where you can see the function and play with that:
Now we are interested in the Test button. The first time you click the button, it opens a modal box, where you can specify dummy data for executing given function. It is an excellent way to test a new or existing functions:
It is time to edit the dummy data. Our function requires a few params, like in the screen:
After filling it, click the Save and test button in the bottom right-hand corner. AWS executed the function with provided params, and we can see it immediately in the area below code editor:
Congratulations! You executed your first AWS Lambda function. Now it is time to put this knowledge into practice. Go ahead and create a few AWS functions, connect it with S3 or API Gateway. Play with the samples provided in the new AWS Lambda screen view.