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

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

— November 28, 2018

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

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. Both of the new instance types are built on the AWS Nitro System. The AWS Nitro System improves the performance of processing in virtualized environments by...

Read more
  • AWS
  • EC2
  • re:Invent 2018
— 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
— November 13, 2018

Understanding AWS VPC Egress Filtering Methods

Security in AWS is governed by a shared responsibility model where both vendor and subscriber have various operational responsibilities. AWS assumes responsibility for the underlying infrastructure, hardware, virtualization layer, facilities, and staff while the subscriber organization ...

Read more
  • Aviatrix
  • AWS
  • VPC
— 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
— 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
— 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
— 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
— 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
— 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
— August 17, 2018

How to Use AWS CLI

The AWS Command Line Interface (CLI) is for managing your AWS services from a terminal session on your own client, allowing you to control and configure multiple AWS services.So you’ve been using AWS for awhile and finally feel comfortable clicking your way through all the services....

Read more
  • AWS
Albert Qian
— August 9, 2018

AWS Summit Chicago: New AWS Features Announced

Thousands of cloud practitioners descended on Chicago’s McCormick Place West last week to hear the latest updates around Amazon Web Services (AWS). While a typical hot and humid summer made its presence known outside, attendees inside basked in the comfort of air conditioning to hone th...

Read more
  • AWS
  • AWS Summits
— August 8, 2018

From Monolith to Serverless – The Evolving Cloudscape of Compute

Containers can help fragment monoliths into logical, easier to use workloads. The AWS Summit New York was held on July 17 and Cloud Academy sponsored my trip to the event. As someone who covers enterprise cloud technologies and services, the recent Amazon Web Services event was an insig...

Read more
  • AWS
  • AWS Summits
  • Containers
  • DevOps
  • serverless