Question
How to deploy Java SDK code in AWS Lambda service.
Answer
Prerequisites:
- We will use the Java SDK example code in this KB Link go ahead and download the example and make sure it runs successfully.
- AWS Lambda only supports Java 8 as of writing this article, make sure to use JDK 1.8
Follow the steps below to run Java SDK as Lambda Function:
- Open the pom.xml file in the downloaded project and add the text below under <dependencies> block:
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.0.0</version>
</dependency>
2. Open file SplitSDK_Sample.java and add the following imports in the beginning:
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
3. Update the class SplitSDK_Sample definition with the line below.
public class SplitSDK_Sample implements RequestHandler<Object, String> {
4. The code to implement Split SDK should be under the handleRequest function instead of main method remove the code under the main method and paste it under the handleRequest function as below:
@Override
public String handleRequest(Object input, Context context) {
String myinput=input.toString();
System.out.print("Input "+myinput+"\n\n");
HashMap<String, String> mapInput = (HashMap<String, String>) input;
String userId = mapInput.get("key1");
String treatment="";
try {
MySplit split = new MySplit("API KEY");
treatment = split.GetSplitTreatment(userId, "Split Name");
System.out.print("Treatment: "+treatment+"\n\n");
split.Destroy();
} catch (Exception e) {
System.out.print("Exception: "+e.getMessage());
}
return treatment;
}
5. Build the Project in Eclipse using menu item Project->Build Project, verify no build errors. Make sure to build it as Package.
6. In Eclipse Project Explorer view, right-click on your project and select Export.
7. Select Runnable JAR file option and click Next.
8. Provide path and JAR file name, keep selection of library handling to Extract required libraries into generated JAR option and click Finish to generate the JAR file.
9. Login to AWS, click on Lambda->Functions link and create new function and use Author from Scratch option, select Java 8 Runtime option, type a function name and click Create Function.
10. Under Function code section, click Upload button and upload your exported JAR, then click Save button at upper right corner.
11. Next step is to configure test event, click on the drop down arrow and select Configure test events item.
12. Use Hello World template to pass key dictionaries to your Lambda function, the user id used in GetTreatment call will be the value for key1, type a name for your event and click Create.
13. Under the Function code section, overwrite the Handler edit box with the line below and click Save.
sample.SplitSDK_Sample::handleRequest
14. The Lambda function is now ready to be used, click on Test button to run it, the expected output should be the treatment value, the log output will also show any logging info.
Comments
0 comments
Please sign in to leave a comment.