How to Configure Eclipse/STS for AWS Lambda Java Development with the AWS Toolkit for Eclipse

How to Configure Eclipse/STS for AWS Lambda Java Development with the AWS Toolkit for Eclipse

Skip a trip to the AWS Management Console – Work with Lambda directly from your IDE instead!

In our previous posts, I introduced the building blocks of the Amazon API Gateway and AWS Lambda and their functionality. I am a Java developer and sometimes I need to test my code within my IDE (e.g. Eclipse or SpringSource Tool Suite) which is standard for our application.

In this blog post, I will present the configurations and examples of how to run AWS Lambda code from STS. You’ll be able to do this without taking a trip the AWS Management Console (though you may of course still need to sometimes). We’ll be using the AWS Toolkit for Eclipse.

Need a primer in AWS Lambda? Cloud Academy offers a suite of Lambda resources to get you started:

AWS Lambda Training

Let’s get going!

I have a Windows laptop with STS-3.7.3 based on Eclipse-4.5.2. We should install the AWS Toolkit for Eclipse before we dive any deeper. If you have the Toolkit for Eclipse, please skip the below step.

Install the AWS Toolkit for Eclipse:

  1. As pre-requisite, you should have an AWS account and sufficient privileges to execute Lambda and other services.
  2. You’ll need STS-3.7.3 or Eclipse IDE 3.6+.
  3. In Eclipse or STS, open help and then Install New Software.
  4. In next window, enter https://aws.amazon.com/eclipse in the text box labeled “Work with” at the top of the dialog. Press Add button and in the dialog box enter “AWS Toolkit” or any name that you are comfortable with but signifies it is the AWS Toolkit. Press OK.AWS Toolkit for Eclipse
  5. It will take some time to fetch all the required tools from the “Location” mentioned above and once the Name box populated, press Select All. Click Next.

AWS Toolkit for Eclipse
6. The installation will take a bit of time (depending on your internet bandwidth).
AWS Toolkit for Eclipse
7. The above items will be installed. Follow the instruction path by clicking the “Next” button, and you are done.
8. Once you restart your STS/Eclipse, you’ll notice the AWS Icon in the menu bar. Click on that icon, provide your AWS Credentials, and you are ready to go. You can see what services and which regions you have access to by reviewing the AWS Explorer in STS. As I am admin of my AWS Account, I have the following screen.

AWS Toolkit for Eclipse
(AWS Explorer)

Create the Project for AWS Lambda:

  1. Create AWS Lambda Java Project from AWS Toolkit.

AWS Lambda

       2. You might need to configure AWS SDK for Java which will be done in case you get the following error:

AWS Lambda

3. The configuration screen is as follows:

AWS Lambda

4. The project configuration for our example is as follows:

AWS Lambda

  • Project Name: AWSLambdaDemo
  • Package Name: aws.lambda.demo
  • Class Name: DemoLambdaFunctionHandler
  • Input Type: S3Event
  • Output Type: Object

Click on finish to get the welcome page in STS for instruction. Remove any kind of dependency that might be causing an error message. Add two more jars for future examples and use. Acquire the following jars through maven or download them from github/maven repo:

5.  Below you can see the output of the project:
AWS Lambda
6. The generated artifacts are as follows:

  • The DemoLambdaFunctionHandler class implements of the RequestHandler interface that defines the Lambda function you need to implement.
  • The DemoLambdaFunctionHandlerTest class is for the unit tests cases.
  • The TestContext class is an implementation of the Context interface, which acts as a parameter for the Lambda function.
  • The TestUtils class will be used for parsing the JSON file.
  • A sample S3 event source configuration file, s3-event.put.json for testing.

7. Implement the Lambda function inside handleRequest(..) method. In this sample implementation of the Lambda function, it returns the bucket name from the S3 Event.

package com.aws.lambda.demo;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.S3Event;
public class DemoLambdaFunctionHandler implements RequestHandler<S3Event, Object> {
    @Override
    public Object handleRequest(S3Event input, Context context) {
        context.getLogger().log("Input: " + input);
        return input.getRecords().get(0).getS3().getBucket().getName();
    }
}

8. Test the Lambda function test case using JUnit.
9. Upload & Run the Lambda function as below:
AWS Lambda functions
10 . Provide function name as S3DemoLambdaFunction and configure the function. Create the bucket if you do not have one.
AWS Lambda functions

AWS Lambda functions
11. I had an existing Lambda role as AWSLambdaRole which was populated here. I previously created the bucket “cplambdatestbucket“, which is shown below. I have reduced the memory print to 256 MB, instead of the default of 512 MB. That was our goal.
Our goal was to complete all AWS Console activities without visiting AWS Management Console or AWS CLI and I believe we have done this.
Allow some time to upload the function and click on Run function on AWS Lambda.
AWS Lambda functions

12. It will ask for the Lambda Function Input which, is obviously our s3-event.put.json file which is auto-populated.
AWS Lambda functions
13. Click on invoke. We are expecting to print the bucket name. The output we receive shows below:
AWS Lambda functions
14.  Go to the AWS Management Lambda Console to verify if the function is indeed available. Here is the snapshot:
AWS Lambda functions
15. Verify the authenticity of the Eclipse-based Lambda function with a test.
Select S3 Put as a sample event template, and test. The bucket name is “sourcebucket”. Click on Test. The output should appear as below. Notice that it takes the configured memory size 256MB rather than the default 512MB.
AWS Lambda functions

Conclusion

Java developers often use their IDE the same way they use Eclipse or STS, or Netbeans. These IDEs offer a great method of coding. They assist in maintaining versions using SVN/ Git, CI/CD with Jenkins and deploying programs directly to any web servers or cloud, like AWS, PWS, etc. from one place.
As Lambda executes code without servers (Lambda is basically PaaS of AWS), developers benefit by shortening their development and deployment lifecycle in the cloud. More efficiency and greater use of resources builds higher productivity and better projects. 
This is a simple example of how the AWS Toolkit is configured and used for Lambda function invocation without opening the Lambda service page. You should certainly try some more examples and explore ways they help increase your productivity as long as you are using Java as your development language.

Get Started with Cloud Academy Today!

Cloud Academy offers a free 7-day trial subscription where you can follow a Learning Path toward your ultimate career goals or certifications. If you’re new to Java, you can begin with the Introduction to Java Learning Path. To take your skills to the next level, advanced programmers can begin with our Advanced Java Learning Path.

Cloud Academy has Hands-on Labs that grant you access to real development environments without leaving the Cloud Academy site. There are video courses and quizzes for reinforcing knowledge. This is a complete eLearning system and a great community. Check it out and provide feedback.

 

Cloud Academy