1

i am trying to insert items into dynamodb through a lambda function. i have given all the required credentials required for creating the iam role and lambda function. but my program throws the following exception

{
  "errorMessage": "com/amazonaws/client/builder/AwsSyncClientBuilder",
  "errorType": "java.lang.NoClassDefFoundError",
  "stackTrace": [
    "java.lang.ClassLoader.defineClass1(Native Method)",
    "java.lang.ClassLoader.defineClass(ClassLoader.java:763)",
    "java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)",
    "java.net.URLClassLoader.defineClass(URLClassLoader.java:467)",
    "java.net.URLClassLoader.access$100(URLClassLoader.java:73)",
    "java.net.URLClassLoader$1.run(URLClassLoader.java:368)",
    "java.net.URLClassLoader$1.run(URLClassLoader.java:362)",
    "java.security.AccessController.doPrivileged(Native Method)",
    "java.net.URLClassLoader.findClass(URLClassLoader.java:361)",
    "java.lang.ClassLoader.loadClass(ClassLoader.java:424)",
    "java.lang.ClassLoader.loadClass(ClassLoader.java:357)",
    "com.bridgelabz.LambdaFunctionHandler.handleRequest(LambdaFunctionHandler.java:20)",
    "com.bridgelabz.LambdaFunctionHandler.handleRequest(LambdaFunctionHandler.java:1)"
  ],
  "cause": {
    "errorMessage": "com.amazonaws.client.builder.AwsSyncClientBuilder",
    "errorType": "java.lang.ClassNotFoundException",
    "stackTrace": [
      "java.net.URLClassLoader.findClass(URLClassLoader.java:381)",
      "java.lang.ClassLoader.loadClass(ClassLoader.java:424)",
      "java.lang.ClassLoader.loadClass(ClassLoader.java:357)",
      "java.lang.ClassLoader.defineClass1(Native Method)",
      "java.lang.ClassLoader.defineClass(ClassLoader.java:763)",
      "java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)",
      "java.net.URLClassLoader.defineClass(URLClassLoader.java:467)",
      "java.net.URLClassLoader.access$100(URLClassLoader.java:73)",
      "java.net.URLClassLoader$1.run(URLClassLoader.java:368)",
      "java.net.URLClassLoader$1.run(URLClassLoader.java:362)",
      "java.security.AccessController.doPrivileged(Native Method)",
      "java.net.URLClassLoader.findClass(URLClassLoader.java:361)",
      "java.lang.ClassLoader.loadClass(ClassLoader.java:424)",
      "java.lang.ClassLoader.loadClass(ClassLoader.java:357)",
      "com.bridgelabz.LambdaFunctionHandler.handleRequest(LambdaFunctionHandler.java:20)",
      "com.bridgelabz.LambdaFunctionHandler.handleRequest(LambdaFunctionHandler.java:1)"
    ]
  }
}

here is my code

package com.bridgelabz;

import com.amazonaws.regions.Regions;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

import com.amazonaws.services.lambda.runtime.events.DynamodbEvent;

public class LambdaFunctionHandler implements RequestHandler<DynamodbEvent, Object> {

    @Override
    public Object handleRequest(DynamodbEvent input, Context context) {
        context.getLogger().log("Input: " + input);

        AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().withRegion(Regions.US_WEST_2).build();
        DynamoDB dynamoDB = new DynamoDB(client);
        String tableName = "AddNumbers";

        Table table = dynamoDB.getTable(tableName);
        Model model = new Model();

        int uniqueId = model.getUniqueId();
        int number1 = model.getNumber1();
        int number2 = model.getNumber2();
        int result = number1 + number2;

        Item item = new Item().withPrimaryKey("uniqueId", uniqueId).withNumber("number1", number1)
                .withNumber("number2", number2).withNumber("result1", result);

        table.putItem(item);

        return true;
    }

}

and my model class

package com.bridgelabz;

public class Model {

    int uniqueId;
    int number1;
    int number2;

    public int getUniqueId() {
        return uniqueId;
    }

    public void setUniqueId(int uniqueId) {
        this.uniqueId = uniqueId;
    }

    public int getNumber1() {
        return number1;
    }

    public void setNumber1(int number1) {
        this.number1 = number1;
    }

    public int getNumber2() {
        return number2;
    }

    public void setNumber2(int number2) {
        this.number2 = number2;
    }

}

the table is created beforehand in the dynamodb. i cant seem to understand what i am doing wrong. please help.

2
  • 1
    The error message is clearly stating that you are missing the com.amazonaws.client.builder.AwsSyncClientBuilder class. Are you testing this locally or running it on AWS? Commented Feb 14, 2017 at 14:22
  • I am running it on AWS. As for the class I tried including the aws-java-sdk-core-1.11.87.jar file and added it to the build path but still it throws a new error which is concerned with the JSON libraries. As far as i understand these libraries are already included in the AWS SDK so i cant seem to understand the issue. Commented Feb 15, 2017 at 3:29

1 Answer 1

1

Are you uploading the code using the AWS SDK Eclipse Plugin? Or on your own? (Export JAR, & upload using aws cli).

I wrote a Java Lambda function to accept a row, and enter it into DynamoDB; Exported the code to a JAR using Eclipse Export, and uploaded it to AWS using aws cli and got a NoClassDefError.

Error loading class <xyz>.MFZLambdaStoreEvents: com/amazonaws/AmazonServiceException: class java.lang.NoClassDefFoundError
java.lang.NoClassDefFoundError: com/amazonaws/AmazonServiceException
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)

I do not get the above error, if I just have a simple function with no AWS SDK dependencies....

Then I came across http://docs.aws.amazon.com/toolkit-for-eclipse/v1/user-guide//lambda-tutorial.html And got to understand that I could upload the code directly using options the Eclipse - AWS SDK Plugin.

I tried doing so, and my function now runs successfully.

Some code snippets from my trial code

import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.PutItemRequest;
import com.amazonaws.services.dynamodbv2.model.PutItemResult;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class LambdaStoreEvents implements RequestHandler<StepsEvent, Response> {

    static AmazonDynamoDB dynamoDB;

    @Override
    public Response handleRequest(StepsEvent event, Context context) {    

    String tableName = "<tablename>";
    Map<String, AttributeValue> item = newItem(event);
    PutItemRequest putItemRequest = new PutItemRequest(tableName, item);

    dynamoDB = AmazonDynamoDBClient.builder().build();
    PutItemResult putItemResult = dynamoDB.putItem(putItemRequest);

    Response response = new Response(putItemResult.toString());

    return response;

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.