Learn AWS Lambda in 5 minutes with Java example

Introduction

This post is useful for those who are new to AWS lambda and want a quick configuration and testing. In this post, we will cover both approaches (implement RequestHandler.handleRequest, custom handler) to create a lambda function. Follow this link to know about approaches we cover: Lambda Function Handler.

Please visit AWS lambda documentation to know more: Lambda Functions.

Prerequisites

  • AWS Lambda

AWS Lambda configuration

Let’s configure a new lambda function.

  1. Open Lambda Manager Console
  2. Click on ‘Author from scratch’
  3. Add trigger – No change. Click Next
  4. Basic Information
    1. Enter function name – Name*: simpleLambdaFunction
    2. Select runtime – Runtime*: Java 8
  5. Lambda function code
    1. Function package*: Upload aws-examples-0.0.1-SNAPSHOT.jar file (Download from here). This jar contains sample lambda functions you need to run after finishing configuration setup
    2. Leave remaining fields same
  6. Lambda function handler and role
    1. Handler*: com.aws.example.lambda.SimpleLambdaFunction::lambdaFunction
    2. Role*: Create new role from template(s)
      1. Role name*: lambdaFunctionTestRole
      2. Policy templates: Basic Edge Lambda permissions
  7. Expand Advanced Settings
    1. Memory (MB)*: 128 MB
  8. Click Next
  9. Review – make sure following would appear same in related sections:
    1. Handler – com.aws.example.SimpleLambdaFunction::lambdaFunction
  10. Click on ‘Create Function’
  11. Wait for some seconds and you will get a message ‘Congratulations! Your Lambda function “simpleLambdaFunction” has been successfully created…’. At the top right of Lambda management console you will get ARN – arn:aws:lambda:us-east-2:11111111111:function:simpleLambdaFunction.

Now we are done with configuration part.

AWS Lambda function test

Open Lambda management console and click on ‘Manage functions’. Click on simpleLambdaFunction. It will show you lambda function details.

Ignore warning: This function contains external libraries. Uploading a new file will override these libraries.’

Let’s test lambda function:

  1. Click on Test button available at the top right of simpleLambdaFunction.
  2. Input test event
    1. Sample test event: Hello World
    2. Remove JSON string : {“key3”: “value3″,”key2”: “value2″,”key1”: “value1”}
    3. Enter String: “My First Lambda Test Message”
    4. Click on ‘Save and test’.
    5. You will see ‘Execution result: succeeded’.
    6. Expand details: It will show returned message from lambda function: “Lambda function executed successfully with input My First Lambda Test Message!”
    7. Now our test is complete and we send and input and received a String output after execution.

I believe you completed all steps within 5 minutes.. 🙂

Now let’s take a look at the java lambda function code we used during configuration and testing.

public class SimpleLambdaFunction {
  public String lambdaFunction(String input, Context context) {
    String message = String.format("Lambda function executed successfully with input %s!", input);
    context.getLogger().log(message);
    return message;
  }
}

Source class: SimpleLambdaFunction.java

In the above class, lambdaFunction is the method that is executed with a String argument. You can see function logs into Cloud Watch.

Implement RequestHandler – handleRequest

AWS Lambda also supports interface method handleRequest as function. In this case, we need to implement RequestHandler interface and implement method handleRequest. See sample code below:

public class SimpleRequestHandler implements RequestHandler<String, String> {
  public String handleRequest(String input, Context context) {
    String message = String.format("Lambda function executed successfully with input %s!", input);
    context.getLogger().log(message);
    return message;
  }
}

Source class: SimpleRequestHandler.java

To use SimpleRequstHandler as lambda function follow the process to create a new lambda function and enter Name*: lambdaRequestHandler and Handler*: com.aws.example.lambda.SimpleRequestHandler::handleRequest, put everything else same we did in the last configuration. handleRequest method will process the input string given in test configuration and log output to cloud watch.

Conclusion

In above post, we learned interface based and non-interface based AWS lambda function implementation using Java. Hope this helps to understand the basics of AWS lambda.

Leave a Reply

Your email address will not be published. Required fields are marked *