There is no better way to explain the function (pun unintended) of complicated cloud services than walking through a step-by-step code example of an easy CS problem. We will be creating an AWS Lambda Function using Java as the service code. This function will be responsible for reversing strings that we pass to it as a payload. We will also be invoking that function using client Java code using the AWS SDK for Java.


Prerequisites

  1. This is partly an AWS tutorial. So make sure you have an AWS account.
  2. Because we will be making use of AWS on the client side to invoke our function, make sure you have AWS CLI installed on your system. I use CLI v2. Also, make sure you run the post-setup configuration steps, which will involve creating and authenticating with access keys. You can then run “aws configure” for a faster set up.
  3. I will assume you already have Java installed as well as a reasonable IDE. I used IntelliJ for this one.
  4. Install Apache Maven. We will need this to package-up our server-side Java code into a JAR file we can then upload to our Lambda.

Client-side code

As mentioned, we will invoke our AWS Lambda function using a Java client. It will be created in the next section, but let’s first create our client. Create a new Java project using your IDE of choice; and make sure you have maven integrated; this means that you should have a pom.xml file where you can declare dependencies.

More installation (sorry): Install the AWS SDK for Java. This adds the necessary components we can use to invoke our function. I used Maven to add this as a dependency. IntelliJ has nice Maven support, btw. Note, as the SDK guide mentions, you don’t need to declare the entire SDK as a dependency. Just use the SDK for Lambda.

Now, we can begin coding.

For simplicity purposes, our client-side code will have only a single class (remember, this is not maintainable, so not recommended when actually building an application).

Following the instructions outlined in the official AWS code examples for invoking a Lambda function using Java, we arrive at something that looks like this:

import com.amazonaws.auth.profile.ProfileCredentialsProvider;
	import com.amazonaws.regions.Regions;
	import com.amazonaws.services.lambda.AWSLambda;
	import com.amazonaws.services.lambda.AWSLambdaClientBuilder;
	import com.amazonaws.services.lambda.model.InvokeRequest;
	import com.amazonaws.services.lambda.model.InvokeResult;
	import com.amazonaws.services.lambda.model.ServiceException;

	public class LambdaClientInvoker {

	    private final String FUNCTION_NAME;
	    private final String PAYLOAD = "{\n" +
	            " \"message1 \": \"Hello, World!\",\n" +
	            " \"message2\": \"Epstein didn't kill himself\"\n" +
	            "}";

	    public LambdaClientInvoker(String functionName) {
	        this.FUNCTION_NAME = functionName;
	    }

	    public static void main(String[] args) {
	        String functionName = args[0];
	        LambdaClientInvoker client = new LambdaClientInvoker(functionName);
	        client.invokeLambda();
	    }

	    /**
	     * Invokes the lambda and prints the returned result to console.
	     */
	    public void invokeLambda() {
	        InvokeRequest invokeRequest = new InvokeRequest().withFunctionName(FUNCTION_NAME).withPayload(PAYLOAD);
	        InvokeResult invokeResult = null;

	        try {
	            AWSLambda lambda = AWSLambdaClientBuilder.standard().withCredentials(new ProfileCredentialsProvider()).withRegion(Regions.US_EAST_2).build();
	            invokeResult = lambda.invoke(invokeRequest);

	            String answer = new String(invokeResult.getPayload().array());
	            System.out.println("Lambda returned: " + answer);
	        } catch (ServiceException e) {
	            System.out.println(e);
	        }
	        System.out.println(invokeResult.getStatusCode());
	    }
	}

#aws #microservices #java #aws-lambda #programming

Create a String Reversal Microservice on AWS Lambda Using Java
1.25 GEEK