简体   繁体   中英

AWS Lambda ClassNotFoundException

I am having trouble when lambda function is called on dynamoDB update. i have checked AWS Lambda: ClassNotFoundException and AWS Lambda NoClassDefFoundError but no success.

i am writing this lambda function to call when there is any update in dynamo . i followed this tutorial. https://docs.aws.amazon.com/lambda/latest/dg/with-dynamodb-create-package.html

All the thing have been done successfully but the problem is when i update dynamodb and check the logs for AWS Cloud watch there is an exception.

Error loading method handleRequest on class com.amazonaws.lambda.demo.LambdaFunctionHandler: java.lang.NoClassDefFoundError
java.lang.NoClassDefFoundError: com/amazonaws/services/lambda/runtime/events/DynamodbEvent
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetPublicMethods(Class.java:2902)
at java.lang.Class.getMethods(Class.java:1615)
Caused by: java.lang.ClassNotFoundException: com.amazonaws.services.lambda.runtime.events.DynamodbEvent
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 4 more

my java code is.

package com.amazonaws.lambda.demo;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.DynamodbEvent;
import com.amazonaws.services.lambda.runtime.events.DynamodbEvent.DynamodbStreamRecord;


public class LambdaFunctionHandler implements RequestHandler<DynamodbEvent, String> {

    @Override
    public String handleRequest(DynamodbEvent ddbEvent, Context context) {
        // TODO Auto-generated method stub
        for (DynamodbStreamRecord record : ddbEvent.getRecords()){
               System.out.println(record.getEventID());
               System.out.println(record.getEventName());
               System.out.println(record.getDynamodb().toString());

            }
            return "Successfully processed " + ddbEvent.getRecords().size() + " records.";
    }

}

my pom.xml is

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.amazonaws.lambda</groupId>
  <artifactId>demo</artifactId>
  <version>1.0.0</version>
  <packaging>jar</packaging>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
          <encoding>UTF-8</encoding>
          <forceJavacCompilerUse>true</forceJavacCompilerUse>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
      <createDependencyReducedPom>false</createDependencyReducedPom>
    </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-bom</artifactId>
        <version>1.11.321</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-lambda-java-events</artifactId>
      <version>1.3.0</version>
    </dependency>
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-lambda-java-core</artifactId>
      <version>1.1.0</version>
    </dependency>
  </dependencies>
</project>

i am naming my handler as com.amazonaws.lambda.demo.LambdaFunctionHandler::handleRequest

i have checked all the things are fine but still it is giving me error on com.amazonaws.services.lambda.runtime.events.DynamodbEvent this class is in aws sdk.

dependency tree. 在此处输入图片说明

You have to build your project via Maven by using the following command: mvn clean package . Then go to target directory where you can find your built jar file.

When you upload the jar file to AWS Lambda you have to upload the jar file which includes all dependencies ( aws-lambda-java-events in your example). Please see the screen for your example:

在此处输入图片说明

In your case, you have to upload demo-1.0.0.jar instead of original-demo-1.0.0.jar .

If everything's fine, even package name and generated JAR & all and still someone's facing an error then you can add following configurations to the maven-plugin..

instead of maven-shade-plugin, I'm using spring-boot-maven-plugin plugin with following configs.

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot.experimental</groupId>
        <artifactId>spring-boot-thin-layout</artifactId>
        <version>1.0.26.RELEASE</version>
    </dependency>
</dependencies>
<configuration>
    <createDependencyReducedPom>false</createDependencyReducedPom>
    <shadedArtifactAttached>true</shadedArtifactAttached>
    <shadedClassifierName>aws</shadedClassifierName>
    <transformers>
        <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
            <resource>META-INF/spring.handlers</resource>
        </transformer>
        <transformer implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
            <resource>META-INF/spring.factories</resource>
        </transformer>
        <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
            <resource>META-INF/spring.schemas</resource>
        </transformer>
    </transformers>
</configuration>

and then you can run

./mvnw clean package

or if you're using STS/Eclipse, then run Maven build.. with goals clean package

This will generate a jar file in KB in your target folder and you can use this JAR to deploy on AWS lambda code.

PS:

  1. remove maven-shade-plugin
  2. I've refereed Official documentation of spring.cloud.io

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM