AWS Lambda Application running on localhost; errors on AWS

0

I made an AWS Lambda Application with some NodeJS lambda and layer resources. Now I'm trying to add a Java lambda resource to this project, but running into issues after deploying to AWS.

Here's the source code for the project that is hooked up to the AWS Lambda Application CI/CD pipeline.

https://github.com/bennebbenneb/sample-app

When I run this project on localhost I am able to invoke the Java lambda successfully. When I deploy this project it is unable to find the class. Here's the error I see in CloudWatch

Class not found: helloworld.App: java.lang.ClassNotFoundException
java.lang.ClassNotFoundException: helloworld.App. Current classpath: file:/var/task/

Here's how I am running this project on localhost.

First I run sam build which creates a ./.aws-sam/ folder with all the lambdas. Then I run sam local start-api --host 0.0.0.0. After that I invoke the localhost endpoint and get the hello world response successfully.

I've tried adding sam build to the build script in buildspec.yml, but it throws errors saying Architectures is not a valid config setting and then when I removed that it said nodejs14.x is not a valid value for Runtime.

What do I need to change to get the Java classes to load without breaking the current NodeJS lambdas?

1 Answer
0

It looks like helloworld.App class has not been packaged as part of your Java Lambda function. There aren't any Java examples in the Github repo you've specified, but to try and explain the problem.

The JVM attempts to load classes from the locations in the classpath, these locations can be separate JAR files or physical directories. If you had a directory of C:\mycode\myjavaapp and attempted to load a class called helloworld.App then the JVM will look under C:\mycode\myjavaapp\helloworld for a file called App.class. You can specify multiple locations in your classpath using this syntax "C:\mycode\myjavaapp;C:\mycode\libs\sharedcode.jar;C:\mycode\libs\someotherstuff.jar"

Now coming back to your Lambda function, the reason it works locally is that the file for the class helloworld.App exists on your local machine, under whatever path that may be. Now when you deploy the function you need to ensure that the file for helloworld.App can still be found in the files of the deployed Lambda function.

Hope that helps, there is a great article here on the AWS knowledge center that goes through some debugging steps - https://aws.amazon.com/premiumsupport/knowledge-center/lambda-troubleshoot-java-classnotfound/

jamesuk
answered 10 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions