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.

 

Avatar

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

Avatar
Stuart Scott
— June 20, 2019

Working with AWS Networking & Amazon VPC

Being able to architect your own isolated segment of AWS is a simple process using VPCs; understanding how to architect its related networking components and connectivity architecture is key to making it a powerful service.Many services within Amazon Web Services (AWS) require you t...

Read more
  • AWS
  • VPC
Avatar
Stuart Scott
— June 19, 2019

AWS Compute Fundamentals Update

AWS is renowned for the rate at which it reinvents, revolutionizes, and meets customer demands and expectations through its continuous cycle of feature and service updates. With hundreds of updates a month, it can be difficult to stay on top of all the changes made available.  Here ...

Read more
  • AWS
Jeff Hyatt
Jeff Hyatt
— June 18, 2019

10 Steps for an Effective Reserved Instances Strategy

Amazon Web Services (AWS) offers three different ways to pay for EC2 Instances: On-Demand, Reserved Instances, and Spot Instances. This article will focus on effective strategies for purchasing Reserved Instances. While most of the major cloud platforms offer pre-pay and reservation dis...

Read more
  • AWS
  • EC2
Joe Nemer
Joe Nemer
— June 18, 2019

AWS Certification Practice Exam: What to Expect from Test Questions

If you’re building applications on the AWS cloud or looking to get started in cloud computing, certification is a way to build deep knowledge in key services unique to the AWS platform. AWS currently offers 11 certifications that cover major cloud roles including Solutions Architect, De...

Read more
  • AWS
  • AWS Certifications
Avatar
John Chell
— June 13, 2019

AWS Certified Solutions Architect Associate: A Study Guide

The AWS Solutions Architect - Associate Certification (or Sol Arch Associate for short) offers some clear benefits: Increases marketability to employers Provides solid credentials in a growing industry (with projected growth of as much as 70 percent in five years) Market anal...

Read more
  • AWS
  • AWS Certifications
Chris Gambino and Joe Niemiec
Chris Gambino and Joe Niemiec
— June 11, 2019

Moving Data to S3 with Apache NiFi

Moving data to the cloud is one of the cornerstones of any cloud migration. Apache NiFi is an open source tool that enables you to easily move and process data using a graphical user interface (GUI).  In this blog post, we will examine a simple way to move data to the cloud using NiFi c...

Read more
  • AWS
  • S3
Avatar
Chandan Patra
— June 11, 2019

Amazon DynamoDB: 10 Things You Should Know

Amazon DynamoDB is a managed NoSQL service with strong consistency and predictable performance that shields users from the complexities of manual setup.Whether or not you've actually used a NoSQL data store yourself, it's probably a good idea to make sure you fully understand the key ...

Read more
  • AWS
  • DynamoDB
Avatar
Andrew Larkin
— June 6, 2019

The 11 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
Sam Ghardashem
Sam Ghardashem
— May 15, 2019

Aviatrix Integration of a NextGen Firewall in AWS Transit Gateway

Learn how Aviatrix’s intelligent orchestration and control eliminates unwanted tradeoffs encountered when deploying Palo Alto Networks VM-Series Firewalls with AWS Transit Gateway.Deploying any next generation firewall in a public cloud environment is challenging, not because of the f...

Read more
  • AWS
Joe Nemer
Joe Nemer
— May 3, 2019

AWS Config Best Practices for Compliance

Use AWS Config the Right Way for Successful ComplianceIt’s well-known that AWS Config is a powerful service for monitoring all changes across your resources. As AWS Config has constantly evolved and improved over the years, it has transformed into a true powerhouse for monitoring your...

Read more
  • AWS
  • Compliance
Avatar
Francesca Vigliani
— April 30, 2019

Cloud Academy is Coming to the AWS Summits in Atlanta, London, and Chicago

Cloud Academy is a proud sponsor of the 2019 AWS Summits in Atlanta, London, and Chicago. We hope you plan to attend these free events that bring the cloud computing community together to connect, collaborate, and learn about AWS. These events are all about learning. You can learn how t...

Read more
  • AWS
  • AWS Summits
Paul Hortop
Paul Hortop
— April 2, 2019

How to Monitor Your AWS Infrastructure

The AWS cloud platform has made it easier than ever to be flexible, efficient, and cost-effective. However, monitoring your AWS infrastructure is the key to getting all of these benefits. Realizing these benefits requires that you follow AWS best practices which constantly change as AWS...

Read more
  • AWS
  • Monitoring