0

I am trying to deploy a simple springboot app in AWS Lambda/API-Gateway using RequestStreamHandler, but am keep getting the below error.

after looking at internet found that its due to spring-boot package issue so included shade plugin but still no luck.

{
  "errorMessage": "Error loading class com.aws.lambda.testlambda.StreamLambdaHandler: org/springframework/boot/context/embedded/EmbeddedServletContainer",
  "errorType": "java.lang.NoClassDefFoundError"
} 

pom.xml

<properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.amazonaws.serverless</groupId>
            <artifactId>aws-serverless-java-container-spring</artifactId>
            <version>1.5</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <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>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.aws.lambda.testlambda.TestlambdaApplication</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
public class StreamLambdaHandler implements RequestStreamHandler {
    private static SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;

    static {
        try {
            handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(TestlambdaApplication.class);
            // we use the onStartup method of the handler to register our custom filter
            handler.onStartup(servletContext -> {
                FilterRegistration.Dynamic registration = servletContext.addFilter("CognitoIdentityFilter", CognitoIdentityFilter.class);
                registration.addMappingForUrlPatterns( EnumSet.of( DispatcherType.REQUEST), true, "/*");
            });
        } catch (ContainerInitializationException e) {
            // if we fail here. We re-throw the exception to force another cold start
            e.printStackTrace();
            throw new RuntimeException("Could not initialize Spring Boot application", e);
        }
    }

    public StreamLambdaHandler() {
        // we enable the timer for debugging. This SHOULD NOT be enabled in production.
        Timer.enable();
    }

    @Override
    public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context)
            throws IOException {
        handler.proxyStream(inputStream, outputStream, context);

        // just in case it wasn't closed by the mapper
        outputStream.close();
    }
}

Any help is much appreciated. Thanks in advance.

1

2 Answers 2

1

I was able to fix this issue, there are couple of issues to fix and make it work end to end.

Referred below url for this issue.

https://github.com/awslabs/aws-serverless-java-container

  1. Packaging needs to exclude tomcat dependency.
  2. RequestStreamHandler needs to be modified little to process the request and response.
     AwsProxyResponse resp = handler.proxy(req, context);
     LambdaContainerHandler.getObjectMapper().writeValue(outputStream, resp);
  1. Had to choose Proxy Integration option while deploying to API Gateway.
Sign up to request clarification or add additional context in comments.

1 Comment

I have a similar issue, couldn't solve it yet. It's urgent. can you pls help on this query: stackoverflow.com/questions/76577207/…
0

AVAI!

I have a guess on this problem. Could you check the version of Java that is defined on your Lambda function?

I see you are using Java 8 your application. Your lambda runtime should match this version.

You may also face some issues if you are using locally Oracle JVM and AWS Coretto JVM on your lambda.

2 Comments

thanks for your response, all the places are configured as java 8 version. am guessing the issue could be packaging part.
I have a similar issue, couldn't solve it yet. It's urgent. can you pls help on this query: stackoverflow.com/questions/76577207/…

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.