Going Serverless With AWS Lambda

In serverless API creation, we have to create the lambda functions. In this blog, we will cover the process of a typical AWS Lambda workflow using AWS Toolkit for Eclipse. We will see how to create, upload, and invoke AWS lambda functions in java.

Prerequisites

To integrate the AWS toolkit, you need to have an AWS account. If you have already then you are good to go but if not then you can sign up here.

Also, you have to install the AWS Toolkit for Eclipse, and that you understand the basic concepts and features of Lambda. If you want to learn more at the Lambda home page and in the AWS Lambda Developer Guide.

Let’s see the following points one by one,

  1. Create AWS lambda project
  2. Implement the Handler Method
  3. Allow Lambda to Assume an IAM Role
  4. Create an Amazon S3 Bucket for Your Lambda Code
  5. Upload the Code
  6. Invoke the Lambda Function

We Built A Healthcare Platform Using AWS Lambda, Dynamodb, And API Gateway

Create an AWS Lambda Project

To create a lambda project, first, we need to implement code as a method in a handler class. The AWS Toolkit for Eclipse provides a new project wizard to help you create a new handler class. The Lambda project is a Maven project that uses a POM.xml file to manage package dependencies. You can use the Maven command-line tool for building, testing, and deploying your application. For more information about Maven, see the Maven project documentation.

To create an AWS lambda project go through the following steps,

  • Go to the Eclipse menu bar, choose File->New-> AWS Lambda Java Project. Then following window will open.

  • Here, add a Project name, Group ID, Artifact ID, and class name in the associated input boxes. The Group ID and Artifact ID are the IDs that identify a Maven build artifact.
    Here, I have created the following demo project.
  • Project name: DemoLambda
  • Group ID: com.amazonaws.lambda
  • Artifact ID: demo
  • Class name: LambdaFunctionHandler

The Package Name field is the package namespace for the AWS Lambda handler class. The default value of this field is generated automatically when we change Group ID and Artifact ID. The generated value for the package name is a concatenation of the Group ID and Artifact ID, which is as per Maven project conventions.

  • For Input Type, here we choose Custom. To know more about input types, see New AWS Lambda Java Project Dialog.
  • After you choose Finish, your project’s directory and source files are generated in your Eclipse workspace. A new web browser window opens, displaying README.html (which was created for you in your project’s root directory). README.html provides instructions to guide you through the next steps of implementing, testing, uploading, and invoking your new Lambda function. Read through it to gain some familiarity with the steps that are described here.

Next, we will implement the lambda function in our created DemoLambda java project.

Implement the Handler Method

Now we will add our code to the LambdaFunctionHandler lambda created while creating the project. To open this method Go to the Project Explorer in the eclipse then open LambdaFunctionHandler.java in the DemoLambda project. This lambada have the following code,

package com.amazonaws.lambda.demo;


import com.amazonaws.services.lambda.runtime.Context;

import com.amazonaws.services.lambda.runtime.RequestHandler;

public class LambdaFunctionHandler implements RequestHandler<Object, String> {


    @Override

    public String handleRequest(Object input, Context context) {

        context.getLogger().log("Input: " + input);


        // TODO: implement your handler

        return "Hello from Lambda!";

    }


}

Here we are considering a simple example for demo. So now we will add code which returns String Hello with input passed to the request. These changes are added in the handleRequest method. So the updated code is as follows,

package com.amazonaws.lambda.demo;


import com.amazonaws.services.lambda.runtime.Context;

import com.amazonaws.services.lambda.runtime.RequestHandler;


public class LambdaFunctionHandler implements RequestHandler<Object, String> {


    @Override

    public String handleRequest(Object input, Context context) {

        context.getLogger().log("Input: " + input);
       

        String output = "Hello, " + input + "!";

        
        return output;
       

    }


}

Allow Lambda to Assume an IAM Role

To access your lambda function, we need to create an IAM role that gives access to our AWS resources. There are two ways to create the role one is through the AWS Management console or the second is by using the AWS Toolkit for Eclipse. Here we will see how to create an IAM role using the console. And in the next section Upload the code, We will see using AWS ToolKit for Eclipse.

To create an IAM role follow the following steps,

  • Go to the AWS Management Console and sign in using your credentials.
  • Then from the Services menu, open the IAM console.
  • In the Navigation pane, select Roles, and then select Create role.
  • After that for Select the type of trusted entity, select AWS service, and then choose Lambda for the service that will use this role. Then choose Next: Permissions.

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

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

Create an Amazon S3 Bucket for Your Lambda Code

Once Lambda is created we need an Amazon S3 bucket to store your Java project when you upload it. We can use the already existing bucket which is in the same region in which we are running our lambda code. But best practice is to create a new one specifically for Lambda.

There are two ways to create the Amazon S3 bucket, one is through the AWS Management Console or the second is by using the AWS Toolkit for Eclipse. Here we will see how to create an Amazon S3 bucket using a console. And in the next section Upload the code, We will see using AWS ToolKit for Eclipse.

To create an Amazon S3 bucket follow the following steps,

  • Go to the AWS Management Console and sign in using your credentials.
  • Then from the Services menu, open the S3 console.
  • Choose Create bucket.
  • Then enter a bucket name, and then choose a region for your bucket. This region should be the same one in which you intend to run your Lambda function.
  • Choose to Create to finish creating your bucket.

Upload the Code

Once you have created your S3 bucket and IAM role. You can use this for all your lambda functions while uploading it to the console.

To upload the lambda function please follow the following steps,

  • Go to your lambda function in the Eclipse code window, choose AWS Lambda, and right click, after that choose Amazon web services option, and then choose Upload function to AWS Lambda.

  • Then it will open the Select Target Lambda Function page, choose the AWS Region to use. This should be the same region that you chose for your Amazon S3 bucket.
  • When we are uploading for the first time, then select create a new lambda function and give a name for your lambda function. Or when you did some changes in already uploaded lambda then choose an existing lambda function and select your lambda function name. Here, I am uploading a new lambda function.
  • Choose Next.
  • Then open the Function Configuration page, here enter a description for your target Lambda function, and then choose the IAM role and Amazon S3 bucket that your function will use. Here I am going to use the IAM role and Amazon s3 bucket created in steps 3 and 4.

  • If you want to create a new role for a lambda function, select Create in Function Role. It will open create role dialogue. Here give the name for your IAM role.

  • You can give a version to your lambda functions for that On the Function Configuration page, choose to Publish new version. To know more about versioning and aliases in Lambda, see AWS Lambda Function Versioning and Aliases.
  • Same like the IAM role you can create an S3 bucket from the Function Configuration page, select Create in the S3 Bucket for Function Code section if you want to create a new Amazon S3 bucket for your Lambda function. Enter a bucket name in the Create Bucket dialogue box.

  • In Advanced Settings, you can change memory and Timeout for your lambda function.If you don’t want to change leave as it is, the AWS Toolkit for Eclipse selects default values for you. Choose Finish to upload your Lambda function to AWS.

If the upload succeeds, you will see the Lambda function name that you chose appear next to your Java handler class in the Project Explorer view.

Invoke the Lambda Function

Once you upload a lambda function, you are ready to invoke the lambda function in eclipse.

To invoke lambda follow the below steps,

  • Right-click on your lambda function and then select Amazon web services->Run Function on AWS Lambda.
  • Then select the lambda function class you want to invoke.
  • In the Enter the json input for your function field, you can enter the inputs you want to pass your lambda function. Here we will pass a string like “This is demo lambda function”.

By following the above steps you are able to create, upload, and invoke the lambda function in Eclipse using AWS Toolkit.

We Keep Sharing Tips And Learnings On Growth And Technology. Join Our Popular Newsletter That Goes Out Once In 2 Weeks

Poonam

Tech Expert

Poonam is a full-stack developer with around 1+ years of experience in developing web applications. She has good knowledge of ReactJs, Javascript and Python Django framework. She is having experience in creating REST APIs, developing web applications using python, and react. She loves to solve technical problems.

Get in touch for a detailed discussion.

What’s on your mind? Tell us a little bit about yourself and your question, and we will be in touch with you within 12 hours

Free eBook on Telemedicine Platform Development: All About Telemedicine

Download Free eBook Now!