Rahul Raj
Rahul Raj

Reputation: 3459

AWS Lambda: java.lang.ClassNotFoundException even if the full path is mentioned in the AWS Lambda function

I'm trying to test a lambda function (Spring Boot cloud app), but getting java.lang.ClassNotFoundException even though the stream handler is properly mentioned under runtime settings.

enter image description here

Error message:

{
  "errorMessage": "Class not found: com.myexample.handler.ServiceHandler",
  "errorType": "java.lang.ClassNotFoundException"
}

Here is the stream handler code:

package com.myexample.handler;

@Slf4j
@Component
public class ServiceHandler implements RequestHandler<String,Object> {

    @Autowired
    MyService myService ;

    @Override
    public Object handleRequest(String s, Context context) {
            // myService.executeMethod();
    }

I'm using Maven Shade plugin to build the jar with all dependencies:

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <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>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

Other important Maven dependencies added (apart from regular Spring Boot dependencies) in my project for this purpose:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-function-adapter-aws</artifactId>
            <version>3.2.5</version>
        </dependency>
        <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.11.0</version>
        </dependency>

Few things to note here is: I'm using @Component annotation in the ServiceHandler class. I had to do this, because I need to autowire the service.

I don't see anything wrong at lambda side as well as the code. What is going wrong here?

Upvotes: 0

Views: 1027

Answers (1)

smac2020
smac2020

Reputation: 10704

As I mentioned in my comment, it appears you are trying to introduce the Spring Framework into a Lambda function that is coded by using the com.amazonaws.services.lambda.runtime API. There is no need to introduce the Spring Boot APIs into a Lambda function.

You can do a lot of things from an AWS Lambda function that is built by using the AWS Lambda Java runtime API - such as invoking AWS Services using the AWS SDK for Java V2 from within a Lambda function. However - trying to turn a Lambda function into a Spring Managed component is not one of them.

To learn how to use the AWS Lambda Java Runtime API to build an AWS Lambda function that works and invokes other AWS Services using the AWS SDK for Java V2, such as Amazon S3 and Amazon Rekognition (and other AWS Services), see:

https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javav2/usecases/creating_lambda_ppe

Upvotes: 1

Related Questions