1

I want to invoke lambda with api gateway and hit spring boot application API. Can someone help me with good example of doing so without using cloud formation templates.

1 Answer 1

1

You will need to use Spring Cloud Function with aws dependencies and thin jar creation plugin.

Dependencies

 <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-function-adapter-aws</artifactId>
        <version>3.1.2</version>
    </dependency>

    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-lambda-java-core</artifactId>
        <version>1.2.1</version>
    </dependency>  

Build Plugin

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>

                <configuration>
                    <createDependencyReducedPom>false</createDependencyReducedPom>
                </configuration>

                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>

                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>

                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.codetinkering.example.LambdaApp</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.4.5</version>

                <dependencies>
                    <dependency>
                        <groupId>org.springframework.boot.experimental</groupId>
                        <artifactId>spring-boot-thin-layout</artifactId>
                        <version>1.0.26.RELEASE</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

Once this app is built, you need to follow the below steps in AWS console to run your application in Lambda

  1. Create a new Lambda Function in AWS -> Author from scratch. Give a name and runtime as Java 8 or 11(Corretto)

  2. Under Code section , Upload your jar

  3. In Runtime Settings, edit the handler as below

    org.springframework.cloud.function.adapter.aws.FunctionInvoker::handleRequest

  4. If you have defined more than 1 function in your jar, then Lambda needs to know, exactly which one should be invoked. To specify this, go to Configuration section. Add a new environment variable as below Name of variable : spring_cloud_function_definition, Value : <Name of your function>

Note : Name of your function should be the same as method that implemented the functional interface, beginning with small case letter.

With above steps done, you are good to test your lambda.

I have put up this with help of a blog on CodeTinkering. You can refer to the original article here CodeTinkering_Blog_AWSLambda_SpringCloudFunction

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

Comments

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.