0

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?

1
  • Are you trying to build a Lambda function with the AWS Lambda Java Run time API AND attempting to use Spring BOOT APIs from within the AWS Lambda function? There is no need at all to use Spring BOOT APIs to build a Lambda function with Java and no need to use annotations like @Component in the logic of an AWS Lambda function. Commented May 30, 2022 at 15:04

1 Answer 1

1

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

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

2 Comments

Thank you, let me modify it to a regular Java app and will update you here how it goes.
Take a look at the above example as it works perfectly

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.