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 towards your ultimate career goals or certifications. They have hands-on labs that grant users 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.

 

Written by

Chandan Patra

Cloud Computing and Big Data professional with 10 years of experience in pre-sales, architecture, design, build and troubleshooting with best engineering practices.Specialities: Cloud Computing - AWS, DevOps(Chef), Hadoop Ecosystem, Storm & Kafka, ELK Stack, NoSQL, Java, Spring, Hibernate, Web Service

Related Posts

Joe Nemer
Joe Nemer
— April 1, 2019

AWS EC2 Instance Types Explained

Amazon Web Services’ resource offerings are constantly changing, and staying on top of their evolution can be a challenge. Elastic Cloud Compute (EC2) instances are one of their core resource offerings, and they form the backbone of most cloud deployments. EC2 instances provide you with...

Read more
  • AWS
  • EC2
Sanket Dangi
— February 11, 2019

WaitCondition Controls the Pace of AWS CloudFormation Templates

AWS's WaitCondition can be used with CloudFormation templates to ensure required resources are running.As you may already be aware, AWS CloudFormation is used for infrastructure automation by allowing you to write JSON templates to automatically install, configure, and bootstrap your ...

Read more
  • AWS
  • formation
Andrew Larkin
— January 24, 2019

The 9 AWS Certifications: Which is Right for You and Your Team?

As companies increasingly shift workloads to the public cloud, cloud computing has moved from a nice-to-have to a core competency in the enterprise. This shift requires a new set of skills to design, deploy, and manage applications in cloud computing.As the market leader and most ma...

Read more
  • AWS
  • AWS certifications
Andrew Larkin
— November 28, 2018

Two New EC2 Instance Types Announced at AWS re:Invent 2018 – Monday Night Live

The announcements at re:Invent just keep on coming! Let’s look at what benefits these two new EC2 instance types offer and how these two new instances could be of benefit to you. If you're not too familiar with Amazon EC2, you might want to familiarize yourself by creating your first Am...

Read more
  • AWS
  • EC2
  • re:Invent 2018
Guy Hummel
— November 21, 2018

Google Cloud Certification: Preparation and Prerequisites

Google Cloud Platform (GCP) has evolved from being a niche player to a serious competitor to Amazon Web Services and Microsoft Azure. In 2018, research firm Gartner placed Google in the Leaders quadrant in its Magic Quadrant for Cloud Infrastructure as a Service for the first time. In t...

Read more
  • AWS
  • Azure
  • Google Cloud
Khash Nakhostin
Khash Nakhostin
— November 13, 2018

Understanding AWS VPC Egress Filtering Methods

In order to understand AWS VPC egress filtering methods, you first need to understand that security on AWS is governed by a shared responsibility model where both vendor and subscriber have various operational responsibilities. AWS assumes responsibility for the underlying infrastructur...

Read more
  • Aviatrix
  • AWS
  • VPC
Jeremy Cook
— November 10, 2018

S3 FTP: Build a Reliable and Inexpensive FTP Server Using Amazon’s S3

Is it possible to create an S3 FTP file backup/transfer solution, minimizing associated file storage and capacity planning administration headache?FTP (File Transfer Protocol) is a fast and convenient way to transfer large files over the Internet. You might, at some point, have conf...

Read more
  • Amazon S3
  • AWS
Guy Hummel
— October 18, 2018

Microservices Architecture: Advantages and Drawbacks

Microservices are a way of breaking large software projects into loosely coupled modules, which communicate with each other through simple Application Programming Interfaces (APIs).Microservices have become increasingly popular over the past few years. The modular architectural style,...

Read more
  • AWS
  • Microservices
Stuart Scott
— October 2, 2018

What Are Best Practices for Tagging AWS Resources?

There are many use cases for tags, but what are the best practices for tagging AWS resources? In order for your organization to effectively manage resources (and your monthly AWS bill), you need to implement and adopt a thoughtful tagging strategy that makes sense for your business. The...

Read more
  • AWS
  • cost optimization
Stuart Scott
— September 26, 2018

How to Optimize Amazon S3 Performance

Amazon S3 is the most common storage options for many organizations, being object storage it is used for a wide variety of data types, from the smallest objects to huge datasets. All in all, Amazon S3 is a great service to store a wide scope of data types in a highly available and resil...

Read more
  • Amazon S3
  • AWS
Cloud Academy Team
— September 18, 2018

How to Optimize Cloud Costs with Spot Instances: New on Cloud Academy

One of the main promises of cloud computing is access to nearly endless capacity. However, it doesn’t come cheap. With the introduction of Spot Instances for Amazon Web Services’ Elastic Compute Cloud (AWS EC2) in 2009, spot instances have been a way for major cloud providers to sell sp...

Read more
  • AWS
  • Azure
  • Google Cloud
  • SpotInst
Guy Hummel and Jeremy Cook
— August 23, 2018

What are the Benefits of Machine Learning in the Cloud?

A Comparison of Machine Learning Services on AWS, Azure, and Google CloudArtificial intelligence and machine learning are steadily making their way into enterprise applications in areas such as customer support, fraud detection, and business intelligence. There is every reason to beli...

Read more
  • AWS
  • Azure
  • Google Cloud
  • Machine Learning