Create Your First AWS Lambda Function with Java using Eclipse IDE

--

Before going through the practice first lets talk about a little bit about Serverless.

It is a new paradigm in which the developers don’t have to manage servers anymore. We just deploy code and magic happens. It is the cloud providers responsilibity to run the functions. I mean, under-the-hood, they provision a machine, create a runtime for your code and run it. AWS Lambda is the technology we can build serverless functions in AWS cloud environment. There are also a lot of serverless technology built within the AWS, for example S3, DynamoDb, API GW etc. AWS Lambda can integrate those technologies as well and these makes things easier to build an application in AWS.

Well, what about the features that AWS Lambda provides us. I think one of the most important thing is scalability. It is automatically handled from the cloud provider. This is a very nice feature because we dont need to worry about the load coming to our functions. It is a hard topic as you may know. The other feature of Lambda is, it just runs on-demand. For ex: the most popular IAAS technology is EC2 in AWS, it has to be continuously running to be able to handle the requests. AWS Lambda runs on-demand and you pay for only when your functions run. It makes so much sense to use Lambda if you have a proper application needs to fit it.

Pricing is very easy for AWS Lambda. AWS bills for pay per request and the compute time. First 1 million request are free, then every one million request charged 0.20 USD plus, 0,0000166667 USD for every GB of compute time. Overall, it makes sense to use Lambda especially a workload for you don’t want to manage the infrastructure and build the feature easily and cheaper costs.

On the other hand, Lambda has some limitations.

  • In default, it allows 1000 concurrent executions. But it can be increased with AWS ticket to hundreds of thousands.
  • Function memory allocation can be 128 MB to 10,240 MB
  • Function timeout 15 minutes.

These are some of the limitations.

So, thats enough for the theory lets practice it and create our first Lambda function in java. Lambda supports most of the popular languages like Node.js, Python, Java, C#, Golang. I am gonna build an example using Java and in Eclipse IDE. So, lets go!

The first thing we need to do is to install AWS toolkit for Eclipse IDE.

Install the Maven Plugin for Eclipse.

  1. Start Eclipse. From the Help menu in Eclipse, choose Install New Software.
  2. In the Work with box, type https://aws.amazon.com/eclipse and then press Enter.
  3. Choose the components of the AWS Toolkit for Eclipse that you want to install. Click Select All to install all components at once.

I’ve just selected the 3 components as below.

AWS Toolkit Components

Now, we’re gonna create a project.

  1. In the Eclipse go with this steps, File-New-AWS Lambda Function
  2. Specify Package and Name for your Lambda function.
  3. Select Input Type Custom in Lambda Function Handler. This defines the entry point for your function. Our entry point should be custom, we are gonna invoke it by own.
  4. Then click Finish.
Creating a new AWs Lambda Function
Creating a new AWS Lambda Function

So, if you make it properly, a class called HelloWorldLambda created in the src/main/java directory and some test classes created under the src/test/java directory which are called respectively HelloWorldLambda, TestContext, TestUtils.

Lets take a look at to the class HelloWorldLambda. It implements an interface called RequestHandler which takes two generic type, one for input other for output, and provide an abstract method called handleRequest. It’s so simple right.

public interface RequestHandler<I, O> {public O handleRequest(I input, Context context);}

We need to implement this method to be able to run the Lambda function.

I am gonna change the content of HelloWorldLambda class a little bit like this.

Also change the input variable to String in HelloWorldLambdaTest class.

The next thing to we’re gonna do is to create an IAM role to be able to execute this function. You can either create an IAM role in Eclipse using AWS Toolkit or follow along with me to create from AWS Console.

  1. Go to AWS console. Type IAM at the search bar. Now we are in IAM console
  2. At the left side of the console click Role and click Create Role button. It looks like this.

3. For Select type of trusted entity, choose AWS service, and then choose Lambda for the service that will use this role. Then choose Next: Permissions.

3. For Attach permissions policy, choose AWSLambdaBasicExecutionRole. This allows Lambda to write to your CloudWatch Logs resources. Then choose Next: Review.

4. Add a name for your role, such as hello-world-lambda-role, and a description for the role. Then choose Create role to finish creating the IAM role

So, IAM role created and you can see the role in the IAM Console. Have we finished it yet? No

The next thing we’re gonna do is to create an S3 bucket to store our Lambda function code.You can either use a bucket that already exists in the AWS Region in which you’ll run your code, or you can create a new one specifically for Lambda to use (recommended). As you’ve already know S3 bucket name must have a globally unique name but its locked to a region. Our Lambda function and S3 bucket must be in the same region.

You can also create an S3 bucket from the Eclipse IDE or follow me through the AWS console.

  1. Type S3 in the search bar at the AWS Console.
  2. There is a page opened up to list all buckets created within your account. Click for Create Bucket button at the top right corner.
  3. Name your bucket that must have a globally uniqeue name and leave all other configurations as default.

So, thats it for configuration. Now lets jump into Eclipse and run the Lambda function. For this, first we should upload our Lambda function.

  1. Right click your Lambda class and click Amazon Web Services, then click Upload function to AWS Lambda.

2. In the opening window, provide an AWS region with the same region with your S3 bucket nad then provide a function name and click Next.

3. In the opening window you can optionally provide a description for your function. Select the appropriate IAM role for created our lambda function and select the S3 bucket in the list. Leave default other configurations then click Finish.

That’s it, we’re now upload our code. If the operation is done with succesfully then you are gonna see your function name along side your class name Project Explorer.

Or you can go to AWS Console ant type Lambda to the search bar to be able to go Lambda Console.

So, it seems it’s ok. Our function uploaded to AWS. Let’s now invoke our function to be able to see the behaviour.

There are two ways to do run your function. You can also invoke it from AWS console or within Eclipse.

In the Eclipse, right click your class and then select Amazon Web Services -> Run function on AWS. A window appears and ask you to provide an input, lets type it, then click Invoke button.

Thats it, congratulations you ran your first Lambda function.

Lets look at the Console and see what’s happened.

It logs about input parameters and the output that out function returned. Also request id, memory usage, init duration and billed duration information.

Lets look at in the AWS console for details. Please type CloudWatch in the searchbar and go to CloudWatch console. In the left side on the console click Log groups. You can see your lambda function with the name of /aws/lambda/HelloWorld. Please click it.

In the opening windows, please click log streams. This shows us the logs whenever we invoke our function. If you click this log, you can see the same information like in the Eclipse Console.

You can say that how is it happen? Because we created an IAM role hello-world-lambda-role and add a policy to this role called AWSLambdaBasicExecutionRole. This means that we created an execution role and this way we can integrate with CloudWatch and see the logs streams.

You can view this policy with the following. Go to your Lambda console in AWS. Click your function and click Configuration, click Permission.

As you can see, we are created 3 actions on resource with the IAM role. You can also see the role policy in the IAM Console of course.

One last thing is we can also be able to test our function on AWS Console. For this purpose, please click Test with the above image and provide an input. Than you can also test your function from AWS Console.

So, that’s it for the post. We created Lambda function in Eclipse and see how we can write a Lambda function within the java. Also look at the AWS console to which configuration we need and so much within the AWS Lambda.

I hope you enjoy it.

See you…

--

--

Cihad Güzel
Big Data, Cloud Computing and Distributed Systems

AWS Certified Solutions Architect | Datastax Certified Apache Cassandra Developer. Big fan of distributed systems.