Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in AWS by (19.1k points)

I am trying to deploy a Spring Cloud Function written in Kotlin as an AWS lambda, but getting this error when testing it out

{

  "errorMessage": "No bean named 'function' available",

  "errorType": "org.springframework.beans.factory.NoSuchBeanDefinitionException",

  "stackTrace": [

    "org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:686)",

    "org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1205)",

    "org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)",

    "org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:205)",

    "org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1091)",

    "org.springframework.cloud.function.adapter.aws.SpringFunctionInitializer.initialize(SpringFunctionInitializer.java:100)",

    "org.springframework.cloud.function.adapter.aws.SpringBootRequestHandler.handleRequest(SpringBootRequestHandler.java:44)"

  ]

}

I am able to run it fine on my local setup using spring-boot:run command. I am using spring boot 2 with spring 5. Here is what my code looks like.

Application.kts

@SpringBootApplication

@FunctionScan

class Application

fun main(args: Array<String>) {

    runApplication<Application>(*args)

}

Function.kt

package `in`.nafa.job.inventoryfulfillment

import org.springframework.stereotype.Component

import java.util.function.Function

@Component("function")

class Function : Function<Incoming, Outgoinig> {

    override fun apply(t: Incoming): Outgoinig {

        return Outgoinig()

    }

}

Incoming and Outgoing are empty classes and do not have any properties.

Handler.kt

package `in`.nafa.job.inventoryfulfillment

import org.springframework.cloud.function.adapter.aws.SpringBootRequestHandler

class Handler : SpringBootRequestHandler<Incoming, Outgoinig>()

pom.xml Most part of this pom is copied from spring cloud function samples

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>in.nafa.job</groupId>

    <artifactId>inventory-fulfillment</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <packaging>jar</packaging>

    <name>inventory-fulfillment</name>

    <description>Demo project for Spring Boot</description>

    <parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>2.0.0.RELEASE</version>

        <relativePath/> <!-- lookup parent from repository -->

    </parent>

    <properties>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

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

        <kotlin.version>1.2.20</kotlin.version>

        <aws-lambda-events.version>2.0.2</aws-lambda-events.version>

        <reactor.version>3.1.2.RELEASE</reactor.version>

        <wrapper.version>1.0.9.RELEASE</wrapper.version>

        <spring-cloud-function.version>1.0.0.BUILD-SNAPSHOT</spring-cloud-function.version>

        <spring-cloud-stream-servlet.version>1.0.0.BUILD-SNAPSHOT</spring-cloud-stream-servlet.version>

        <start-class>in.nafa.job.inventoryfulfillment.InventoryFulfillmentApplicationKt</start-class>

    </properties>

    <dependencies>

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-function-adapter-aws</artifactId>

        </dependency>

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-function-web</artifactId>

            <scope>provided</scope>

        </dependency>

        <dependency>

            <groupId>com.amazonaws</groupId>

            <artifactId>aws-lambda-java-events</artifactId>

            <version>${aws-lambda-events.version}</version>

            <scope>provided</scope>

        </dependency>

        <dependency>

            <groupId>com.amazonaws</groupId>

            <artifactId>aws-lambda-java-core</artifactId>

            <version>1.1.0</version>

            <scope>provided</scope>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-test</artifactId>

            <scope>test</scope>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-configuration-processor</artifactId>

            <optional>true</optional>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>

        <dependency>

            <groupId>com.fasterxml.jackson.module</groupId>

            <artifactId>jackson-module-kotlin</artifactId>

        </dependency>

        <dependency>

            <groupId>org.jetbrains.kotlin</groupId>

            <artifactId>kotlin-stdlib-jdk8</artifactId>

        </dependency>

        <dependency>

            <groupId>org.jetbrains.kotlin</groupId>

            <artifactId>kotlin-reflect</artifactId>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-test</artifactId>

            <scope>test</scope>

        </dependency>

    </dependencies>

    <dependencyManagement>

        <dependencies>

            <dependency>

                <groupId>org.springframework.cloud</groupId>

                <artifactId>spring-cloud-function-dependencies</artifactId>

                <version>${spring-cloud-function.version}</version>

                <type>pom</type>

                <scope>import</scope>

            </dependency>

            <dependency>

                <groupId>org.springframework.cloud</groupId>

                <artifactId>spring-cloud-stream-binder-servlet</artifactId>

                <version>${spring-cloud-stream-servlet.version}</version>

            </dependency>

        </dependencies>

    </dependencyManagement>

    <build>

        <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>

        <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>

        <plugins>

            <plugin>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-maven-plugin</artifactId>

                <dependencies>

                    <dependency>

                        <groupId>org.springframework.boot.experimental</groupId>

                        <artifactId>spring-boot-thin-layout</artifactId>

                        <version>${wrapper.version}</version>

                    </dependency>

                </dependencies>

            </plugin>

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-shade-plugin</artifactId>

                <configuration>

                    <createDependencyReducedPom>false</createDependencyReducedPom>

                    <shadedArtifactAttached>true</shadedArtifactAttached>

                    <shadedClassifierName>aws</shadedClassifierName>

                </configuration>

            </plugin>

            <plugin>

                <artifactId>kotlin-maven-plugin</artifactId>

                <groupId>org.jetbrains.kotlin</groupId>

                <configuration>

                    <args>

                        <arg>-Xjsr305=strict</arg>

                    </args>

                    <compilerPlugins>

                        <plugin>spring</plugin>

                    </compilerPlugins>

                </configuration>

                <dependencies>

                    <dependency>

                        <groupId>org.jetbrains.kotlin</groupId>

                        <artifactId>kotlin-maven-allopen</artifactId>

                        <version>${kotlin.version}</version>

                    </dependency>

                </dependencies>

            </plugin>

        </plugins>

    </build>

</project>

closed

1 Answer

0 votes
by (44.4k points)
edited by
 
Best answer

I was able to get it working by changing my start class in pom.xml from

<start-class>in.nafa.job.inventoryfulfillment.InventoryFulfillmentApplicationKt</start-class>

to

<start-class>in.nafa.job.inventoryfulfillment.InventoryFulfillmentApplication</start-class>

Need help mastering AWS Lambda and other AWS skills? Go through AWS Training Page and become an AWS expert! 

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer
0 votes
1 answer
asked Mar 27, 2020 in Java by Praveen_1998 (119k points)
0 votes
1 answer
asked Mar 27, 2020 in Java by Praveen_1998 (119k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...