Jagadheeswaran Mohan
Jagadheeswaran Mohan

Reputation: 381

Deploying Spring boot with Spring data jpa in AWS lambda

I have created a spring boot application with spring data JPA that should connect with a RDS instance,i have api's exposed which will deo CRUD basically I have provided the DB connection details as below in application.yml file,

spring:
  datasource:
    url: url
    username: username
    password: password

I have added the below dependency too,

  <dependency>
        <groupId>com.amazonaws.serverless</groupId>
        <artifactId>aws-serverless-java-container-springboot2</artifactId>
        <version>1.4</version>
    </dependency>

I have my initializer class as below,

private static SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
    static {
        try {
            
            if (handler == null) {
                LambdaContainerHandler.getContainerConfig().setInitializationTimeout(60_000);
                handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(Application.class);



                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);
        }
    }

Everything seems to be proper, i have created a lambda from AWS console, the have also packing the jar by using below assembler,

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
    <id>lambda-package</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <!-- copy runtime dependencies with some exclusions -->
        <fileSet>
            <directory>${project.build.directory}${file.separator}lib</directory>
            <outputDirectory>lib</outputDirectory>
            <excludes>
                <exclude>tomcat-embed*</exclude>
            </excludes>
        </fileSet>
        <!-- copy all classes -->
        <fileSet>
            <directory>${project.build.directory}${file.separator}classes</directory>
            <includes>
                <include>**</include>
            </includes>
            <outputDirectory>${file.separator}</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

Iam using below, plugin

<profiles>
        <profile>
            <id>shaded-jar</id>
            <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>
                                <configuration>
                                    <artifactSet>
                                        <excludes>
                                            <exclude>org.apache.tomcat.embed:*</exclude>
                                        </excludes>
                                    </artifactSet>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>assembly-zip</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <!-- select and copy only runtime dependencies to a temporary lib folder -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-dependency-plugin</artifactId>
                        <version>3.1.1</version>
                        <executions>
                            <execution>
                                <id>copy-dependencies</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>copy-dependencies</goal>
                                </goals>
                                <configuration>
                                    <outputDirectory>${project.build.directory}/lib</outputDirectory>
                                    <includeScope>runtime</includeScope>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-assembly-plugin</artifactId>
                        <version>3.1.0</version>
                        <executions>
                            <execution>
                                <id>zip-assembly</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>single</goal>
                                </goals>
                                <configuration>
                                    <finalName>${project.artifactId}-${project.version}</finalName>
                                    <descriptors>
                                        <descriptor>src${file.separator}assembly${file.separator}bin.xml</descriptor>
                                    </descriptors>
                                    <attach>false</attach>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>


</project>

A zip is created and am uploading the zip to and providng the s3 URL to Lambda function.

After that i have created a API gateway HTTP API and chosen the integration as the previously created Lambda. If i try to access the APi am getting, {"message":"Internal Server Error"}

In cloudwatch logs, i could see,

enter image description here

It is getting struck at establising DB connection. I have chosen execution rule as Admin for lambda and provided same VPC as RDS, but no luck, kndly let me know a solution for this.

Upvotes: 3

Views: 2900

Answers (1)

Jagadheeswaran Mohan
Jagadheeswaran Mohan

Reputation: 381

Increasing Lambda timeout fixed this issue. Added the below lines too,

LambdaContainerHandler.getContainerConfig().setInitializationTimeout(60_000); handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(Application.class);

Upvotes: 1

Related Questions