AWS Lambda NoClassDefFoundError - amazon-web-services

I am having difficulty with a Java based Lambda function setup to receive messages from SNS. My function looks like the below:
package com.mycompany;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.events.SNSEvent;
public class LambdaHandler {
public void Handler(SNSEvent event, Context context) {
//Process the event
}
}
It compiles just fine and I don't have any problems uploading the jar file to Lambda (via the web console).
However, when I publish to it (via SNS through to the subscribed Lambda function) with JSON representing the SNSEvent model, the Lambda function throws the following exception:
Error loading method handler on class com.mycompany.LambdaHandler:
class java.lang.NoClassDefFoundError java.lang.NoClassDefFoundError:
com/amazonaws/services/lambda/runtime/events/SNSEvent 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.SNSEvent at
java.net.URLClassLoader.findClass(URLClassLoader.java:381) at
java.lang.ClassLoader.loadClass(ClassLoader.java:424) at
java.lang.ClassLoader.loadClass(ClassLoader.java:357)
I use Maven + Netbeans and it's a Maven Java Application project. I downloaded the function from the Lambda console and confirmed, the jar has a lib/ directory with all of the jar's for the imports, including aws-lambda-java-events-1.1.0.jar, which itself includes the /com/amazonaws/services/lambda/runtime/events/SNSEvent.class file.
Why is the runtime unable to find the class when it's definitely in the jar file? Is there anything else I need to do, set any environment variables, etc?
Any help would be appreciated!
EDIT 1
I tried downgrading to aws-lambda-java-events 1.0.0 and it's still reporting the same exception. As requested, below is my POM file (with just project name changed). I don't know how to tell Maven to put the libraries in a tree structure.
<?xml version="1.0" encoding="UTF-8"?>
<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.app</groupId>
<artifactId>Handler</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-lambda</artifactId>
<version>1.10.6</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>

Use the maven-shade plugin so that the JAR contains the dependencies in an uber-jar.
So, add this to your pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Source: http://docs.aws.amazon.com/lambda/latest/dg/java-create-jar-pkg-maven-no-ide.html
Potentially you may have this issue https://github.com/aws/aws-lambda-java-libs/issues/2 which requires a downgrade to aws-lambda-java-events-1.0.0.jar

=== If this issue exists even after including shaded jar ===
If you have this issue even after having shaded jar then the issue should be related to aws-lambda-java-events package version (should be some incompatibility between AWS lamda version and newer aws-lambda-java-events version) . i.e. I had this issue with latest version (2.0.2) of aws-lambda-java-events package and I have to downgrade the version to 1.3.0.
Seems like newer aws-lambda-java-events version doesn't have many dependencies.

Sometimes you have to upload your lambda again. Also I got the same issue I fixed with this pom.xml:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-bom</artifactId>
<version>1.11.83</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

In the plugins section of your pom.xml, add the Apache Maven Shade Plugin.It is used during the build process. This plugin is used for packaging jars to create a standalone .jar.The maven-shade-plugin will take artifacts (jars) produced by the package goal , and created a standalone .jar that contains the compiled code, and the resolved dependencies from the pom.xml.
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>

If you have <scope>provided</scope>
in aws-lambda-java-events artifact, remove it.

Whenever We tried to upload java based Jar or Zip into AWS lambda Console we have to take care of some basic things like,
The Code URI, Which is present in SAM Template or the template.yml file.
eg:
Eg: runwayDetails - package Name
App - class name
handleRequest - lambda handler method.
Syntax should be like this - packageName.className::methodName
It will solve the the error

upload the function.zip instead of jar to lambda

Related

AWS CDK v2 Java maven alpha dependencies

I started out with the CDK generated dependencies
<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>aws-cdk-lib</artifactId>
<version>${cdk.version}</version>
</dependency>
<dependency>
<groupId>software.constructs</groupId>
<artifactId>constructs</artifactId>
<version>${constructs.version}</version>
</dependency>
But those don't resolve e.g. the HttpApi from API Gateway v2(?)
HttpApi httpApi = HttpApi.Builder.create(this, "MyApi").build();
so I added (since it's apparently in alpha on the CDK level?)
<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>apigatewayv2-alpha</artifactId>
<version>2.24.1-alpha.0</version>
</dependency>
and the next stop was LambdaProxyIntegration
Integration lambdaIntegration = LambdaProxyIntegration.Builder.create().handler(lambda).build();
so I tried adding
<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>apigatewayv2-integrations-alpha</artifactId>
<version>2.65.0-alpha.0</version>
</dependency>
and
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>3.10.0</version>
</dependency>
but no go. Is there some better way of hunting down these maven dependencies that googling around. Would the LambdaProxyIntegration class be in some lambda dependency? Or in some lambda-alpha?
Thanks in advance,
Nik

AWS Lambda function throws ClassNotFoundException

I have a Spring Boot demo project. I am trying to deploy them in AWS Lambda, but I get ClassNotFoundException even thought my jar that I upload contains of the necessary dependencies.
Here goes my code:
pom.xml
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>com.amazonaws.serverless</groupId>
<artifactId>aws-serverless-java-container-spring</artifactId>
<version>[0.1,)</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Application Class
#SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
System.out.println("Welcome to demo project");
}
}
Controller Class
#RestController
public class DemoController {
#GetMapping(value="/getValue")
public String getId() {
return " Call from controller";
}
}
LambdaHandler Class
public class DemoLambdaHandler implements RequestStreamHandler {
public static SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
static {
try {
handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(DemoApplication.class);
} catch (ContainerInitializationException e) {
e.printStackTrace();
throw new RuntimeException("Container not initialized", e);
}
}
#Override
public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException {
// TODO Auto-generated method stub
handler.proxyStream(input, output, context);
}
}
Not sure what I could be missing in this. Kindly help. Below is my inspected jar
Did you find a solution to this? I have been getting the same exact error.
What I can tell so far is that unlike other languages like nodejs, the docker image for java has the command specified like CMD [ "com.example.LambdaHandler::handleRequest" ], instead the nodejs one is CMD [ "app.handler" ]. The difference here is that the nodejs one specifies what the file is e.g. app.js with function called handler.
But the java one only has the class path without the ability to specify the path to the fat JAR file even when the jar file is located in "var/task". How would lambda know the JAR file name?
I had the same issue, I fixed it marking Spring Boot as a dependency.
By default, Spring Boot plugin will pack your classes into "BOOT-INF/classes" directory, but AWS is looking for the handler class from the root directory so it can't find it.
You can check this by extracting your .jar file and seeing if your file is in:
BOOT-INF/classes/com/example/demo/handler/LambdaHandler.class
instead of:
com/example/demo/handler/LambdaHandler.class
To solve it, just mark your package as exec in your pom.xml file:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.6.1</version>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
More info here: Spring Boot as a Dependency
Try building the project with Maven and ensure you have the required maven-shade-plugin in your POM so that the JAR that is built with the dependencies when you run mvn package. If you are missing this plugin, you will create a JAR that does not contain the dependencies and you will encounter ClassNotFoundException.
To learn how to build and deploy a Lambda function by using the Lambda runtime Java API, see this AWS tutorial. It will walk you step by step through the process of building Lambda functions that work:
https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javav2/usecases/creating_workflows_stepfunctions
This tutorial then uses the Lambda functions within Step Functions to create a workflow.
Lambda functions are created using the Lambda runtime Java API -- com.amazonaws.services.lambda.runtime.RequestHandler. Please do not use Spring BOOT APIs to create a Lambda function.
However, If you want to invoke a Lambda function from a Spring boot app and then for example display the result in a view, then you can use the Lambda Client Java API - which is software.amazon.awssdk.services.lambda.LambdaClient.
So in summary:
com.amazonaws.services.lambda.runtime.RequestHandler is used to create a Lambda function by using the Java Lambda runtime API.
software.amazon.awssdk.services.lambda.LambdaClient is the Lambda client Java API V2 that lets you interact with deployed Lambda functions. For
example, you can use this client to invoke a Lambda function from a
Java app - like a Spring boot web app. To see a working example of how to use the Lambda client API to invoke a Lambda function, see https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javav2/example_code/lambda/src/main/java/com/example/lambda/LambdaInvoke.java.
Please add these two dependencies in pomfile
1)aws-lambda-java-core
2)aws-lambda-java-events
This should fix it

Not able to run Jmeter Performance Test directly using the JAR created from MAVEN

I have been trying to run one Jmeter script through MAVEN, I was able to do that using command
"mvn verify" and able to see the reports also inside /target folder
However, I want to run the Jmeter Performance Test directly through the JAR which is created after running the "mvn verify" command.
Below is my POM.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>jmeter-testproject</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>jmeter-testproject</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.lazerycode.jmeter</groupId>
<artifactId>jmeter-maven-plugin</artifactId>
<version>2.1.0</version>
<executions>
<execution>
<id>jmeter-tests</id>
<goals>
<goal>jmeter</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
A /target folder is being generated after running "mvn verify" and also jmeter-testproject-1.0-SNAPSHOT created.
However, if I want to run the package by directly executing the JAR, it's giving just "Hello World" as an output which is present in the main class. I want the Jmeter Performance Test also to execute after directly clicking the JAR. Please help.
enter image description here
You can't do this with the jmeter-maven-plugin.
The jmeter-maven-plugin is taking your test plan/configuration and then running a JMeter process for you.
It is not physically building a runnable jar with everything that you need inside it to be able to run performance tests.

Arquillian tomee remote jacoco code coverage

I am doing integration test using Arquillian in TomEE-Plus 7.0.4 remote and trying to get Code coverage using Jacoco 0.8.2. My code coverage is not covered since I am using arquillian-tomee-remote. Since code is not covered not able to take build. I need sample code with has TomEE-plus arquillian remote and Code coverage using Jacoco. I will appreciate if I get any sample working code or sample project.
I used prepare-agent goal which will generate surefireArgLine ( javaagent) and passed the same in surefire plugin. issue here is, I am using remote Tomee and don't know how to generate correct java agent surefireArgLine set to -javaagent:/home/user/.m2/repository/org/jacoco/org.jacoco.agent/0.8.2/org.jacoco.agent-0.8.2-runtime.jar=destfile=/home/user/project/target/coverage-reports/jacoco-ut.exec,append=true,excludes=/config/*.class:/util/*Constants.class
what is the correct javaagent option for my configuration which will connect to arquillian-remote-tomee ?
Jacoco plugin
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${plugin.maven.jacoco.version}</version>
<configuration>
<propertyName>coverageAgent</propertyName>
<append>true</append>
<excludes>
<exclude>**/config/*.class</exclude>
<exclude>**/util/*Constants.class</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${sonar.jacoco.reportPath}</destFile>
<propertyName>surefireArgLine</propertyName>
<append>true</append>
</configuration>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${sonar.jacoco.reportPath}</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
<append>true</append>
</configuration>
</execution>
<execution>
<id>check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<dataFile>${sonar.jacoco.reportPath}</dataFile>
<haltOnFailure>true</haltOnFailure>
<rules>
<rule>
<element>BUNDLE</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.99</minimum>
</limit>
<limit>
<counter>BRANCH</counter>
<value>COVEREDRATIO</value>
<minimum>0.99</minimum>
</limit>
<limit>
<counter>CLASS</counter>
<value>MISSEDCOUNT</value>
<maximum>0</maximum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
Dependency
<dependency>
<groupId>org.jboss.arquillian.testng</groupId>
<artifactId>arquillian-testng-container</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.config</groupId>
<artifactId>arquillian-config-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.extension</groupId>
<artifactId>arquillian-jacoco</artifactId>
<version>1.0.0.Alpha10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<classifier>runtime</classifier>
<scope>test</scope>
<version>${plugin.maven.jacoco.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jacoco/org.jacoco.core -->
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.core</artifactId>
<version>${plugin.maven.jacoco.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomee</groupId>
<artifactId>arquillian-tomee-remote</artifactId>
<version>${tomee.version}</version>
<scope>test</scope>
</dependency>
Arquillian.xml
<extension qualifier="jacoco">
<property name="includes">com.demo.*</property>
</extension>
You can set catalina_opts in arquillian.xml for tomee container. Filter it with maven to pass jacoco javaagent and you are done :).
I have added the proper java agent ( surefireArgLine) to TomEE remote server via catalina opts in surefire pluggin. it works.
surefireArgLine - Will be populate by Surefire prepare-agent at runtime.
<tomee.catalina_opts> ${surefireArgLine}</tomee.catalina_opts>
Disclaimer: I'm not an expert in neither Arquillian nor TomEE, so you might adjust the answer for your purposes.
Anyway, in a nutshell, JaCoCo instruments bytecode in order to provide a coverage report.
Since when Arquillian is used, the actual test execution happens in a TomEE JVM and not in a JVM that actually runs the test suite (probably a CI server or just a build script that runs the test), so configuring JaCoCo on this test machine won't do much, you'll have to configure the server itself.
JaCoCo has a -javaagent option for doing this, and this Java Agent will "intercept" the loading of classes by the server and instrument them.
Now, when JaCoCo works, it produces a jacoco.exec file that actually contains a coverage report that can be shown later in various ways (jenkins plugin to show coverage, sonar integration whatever).
And this is by far the most used option AFAIK, so if you go with this approach, given the instrumentation really works, after the tests are done, you'll have to find the server on the test machine and download it to the build machine and integrate with CI/Sonar whatever.
However, there are alternative solutions:
JaCoCo Documentation states that there are three modes of running an instrumenting Java Agent:
File System: At JVM termination execution data is written to a local file.
TCP Socket Server: External tools can connect to the JVM and retrieve execution data over the socket connection. Optional execution data reset and execution data dump on VM exit is possible.
TCP Socket Client: At startup, the JaCoCo agent connects to a given TCP endpoint. Execution data is written to the socket connection on request. Optional execution data reset and execution data dump on VM exit is possible.
Technically you can just give different parameters to that javaagent so that it will run JaCoCo in one of these modes.
Anyway, we've discussed the first option, but you can also work with TCP configurations if it's required. Of course, here you'll have to handle security concerns (like permission to expose/access the port, etc).
If you work with TCP mode, there is a Maven Plugin that can come in handy. I haven't used it by myself, just googled so I can't comment whether its any good, it has only 2 stars on Github, so probably it's not production ready but maybe you could get some ideas from its source code.

AP_SOUTH_1 region is not present in Regions enum

I am trying a small dynamoDB example. Because regions for my dynamoDB tables is Mumbai(ap-south-1), I am setting client as following:
AmazonDynamoDBClient client = new AmazonDynamoDBClient();
client.setRegion(Region.getRegion(Regions.AP_SOUTH_1));
this.dynamoDb = new DynamoDB(client);
Unfortunately, AP_SOUTH_1 is not getting resolved.
My pom file looks like below:
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>1.10.1</version>
</dependency>
</dependencies>
What am I missing?
It is possible your SDK is not the latest and has no enum for AP_SOUTH_1. Either get the latest SDK or use:
Region.getRegion("ap-south-1")
From: AWS Region Selection
If the region you are attempting to use isn't in the Regions enum, you
can set the region using a string that represents the name of the
region.

Categories