AWS Lambda, creating your first lambda function…

Rukshani Athapathu
Coder's Corner
Published in
5 min readAug 24, 2017

Get ready to build a serverless app using AWS lambda.

Image Courtesy: Pexels

AWS lambda eliminates the need for you to worry about operating systems, servers, patching, scaling as it takes care of, all of that for you. Simply, all you ever have to think about is your business logic and not about provisioning of servers. Intrigued? Let’s dig in.

What is a lambda function and what can it do?

Lambda falls under serverless computing category which works in an event- driven manner in it’s core. What does that mean? Simply, it reacts to events. That is, Lambda will run your code in response to events. For example, when you upload something to your S3 bucket, you can tell lambda to run your code. Something as simple as that. They do provide several triggers to execute lambdas and in the following figure, you can see some of the them. As an example, if you take API Gateway, with that you can run your code in response to HTTP requests.

Lambda Triggers

Pricing and Limits

Before we get our hands dirty with coding, if we briefly go through their pricing, you will see how ridiculously low their prices are. They charge you, based on the number of requests and how long your code executes. If we take the requests, you get the first 1 million requests for free and thereafter $0.20 per 1 million requests ($0.0000002 per request). Then the duration is calculated from the time your code begins executing until it returns or otherwise terminates and the price depends on the amount of memory you allocate to your function. To find out more info about this check out their pricing details. However, for the duration, it is important to remember that it has a maximum of 5 minutes threshold. If your code executes for more than 5 minutes you have to break it into smaller functions. You can view the full details on lambda limits in here. Okay, now that we got it cleared, let’s jump into coding.

Creating your first lambda function

First you need to create an AWS account and then login.

AWS provides code inline facility only for python and nodejs. We are going to create our lambda function using Java. So in that case, we have to upload our code as a jar or a zip file.

I’ll be using eclipse for my development work so first let’s install AWS toolkit in there. You can find the plugin for eclipse in here. Once your eclipse is ready, let’s start coding.

Step 1: Select “New AWS Lambda Java Project” from eclipse and you will get presented with a dialog box as follows. Give a project name and select the input type as “custom” and click “Finish”.

Here I’ve changed input type to String and it returns a simple hello message back to the user. Now, this “LambdaFunctionHandler.java” is the entry point to my function.

package com.amazonaws.lambda.demo;import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
public class LambdaFunctionHandler implements RequestHandler<String, String> {@Override
public String handleRequest(String input, Context context) {
context.getLogger().log("Input: " + input);
// TODO: implement your handler
return "Hello " + input + "!";
}
}

Step 2: Now login to your AWS account and select “Lambda” which is listed under “compute” section. Once you are redirected to Lambda page, click on “Create a function” and that’s it. After that, it is just a matter of going through few steps to create your first Lambda function.

  • We are not going to use any blueprints, so we will create a lambda from scratch. So in step 1, instead of selecting a blueprint, click on “Author from scratch” button and you will see something like below.
  • Click on the Next button and fill the basic information. (Give a name and description and here we have selected Java 8 as our runtime.)
  • Next we need to create a zip or a jar file to package our code. In eclipse right click on your project and export it as a jar file.
  • Now let’s head back to our AWS account and upload that jar file.
  • Next we need to give out handler name. It should be package name followed by your handler class name. In my case, this would be “com.amazonaws.lambda.demo.LambdaFunctionHandler”. Then you need to give permission to your function. Either create a new role or select an existing role for this.
  • Then click on the Next button and review whether all the information listed out is correct. If yes, then click “Create Function” button.
  • Now you will get a message like the following and all you have to do is to test it out.
  • Click on the “Test” and you will see something like below. Give an input text and click “Save and Test”.
  • And you will get your response as below.

So that’s it. You have created your first lambda function.

--

--