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,
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,
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.
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; } }
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,
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.
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,
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,
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.
Once you upload a lambda function, you are ready to invoke the lambda function in eclipse.
To invoke lambda follow the below steps,
By following the above steps you are able to create, upload, and invoke the lambda function in Eclipse using AWS Toolkit.