title: AWS Lambda with Java Spring
body: <p>I'm trying to implement an AWS Lambda service using Java Spring.  I've gone through a whole bunch of tutorials which have told me a whole bunch of ways to do it, but I can't get it to work.</p>
  
  <p>The main problem I'm having is that the AWS Lambda library requires classes to have zero-arg constructors, which makes it difficult to use the Spring IOC container.  I found <a href="https://www.profit4cloud.nl/blog/just-spring-enabled-aws-lambdas/" rel="nofollow noreferrer">a tutorial</a> which has a workaround for that and is able to inject my Spring beans into the class after creation, but that approach doesn't seem to work for bean instantiations of anonymous classes created from defined interfaces (e.g. Spring Data Repositories), which is something my application uses.</p>
  
  <p>I found <a href="https://cloud.spring.io/spring-cloud-function/reference/html/spring-cloud-function.html#_serverless_platform_adapters" rel="nofollow noreferrer">a tutorial</a> on the Spring wrapper for the AWS lambda library, but that also doesn't seem to work; I think I've copied their code more or less properly from <a href="https://github.com/spring-cloud/spring-cloud-function/tree/master/spring-cloud-function-samples/function-sample-aws" rel="nofollow noreferrer">their sample code</a>, but when I run it on Lambda I get the following error:</p>
  
  <pre><code>START RequestId: d9e62756-e72b-45f0-8df4-2164f3979dd7 Version: $LATEST
  Failed to discover main class. An attempt was made to discover main class as 'MAIN_CLASS' environment variable, system property as well as entry in META-INF/MANIFEST.MF (in that order).: java.lang.IllegalStateException
  java.lang.IllegalStateException: Failed to discover main class. An attempt was made to discover main class as 'MAIN_CLASS' environment variable, system property as well as entry in META-INF/MANIFEST.MF (in that order).
      at org.springframework.cloud.function.utils.FunctionClassUtils.getStartClass(FunctionClassUtils.java:83)
      at org.springframework.cloud.function.utils.FunctionClassUtils.getStartClass(FunctionClassUtils.java:60)
      at org.springframework.cloud.function.adapter.aws.FunctionInvoker.start(FunctionInvoker.java:104)
      at org.springframework.cloud.function.adapter.aws.FunctionInvoker.&lt;init&gt;(FunctionInvoker.java:70)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  </code></pre>
  
  <p>Here's my code:</p>
  
  <p>pom.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
      &lt;parent&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;version&gt;2.2.4.RELEASE&lt;/version&gt;
          &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
      &lt;/parent&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;artifactId&gt;JobsTest&lt;/artifactId&gt;
      &lt;packaging&gt;jar&lt;/packaging&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
              &lt;artifactId&gt;spring-cloud-function-adapter-aws&lt;/artifactId&gt;
              &lt;version&gt;3.0.2.RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.amazonaws&lt;/groupId&gt;
              &lt;artifactId&gt;aws-lambda-java-events&lt;/artifactId&gt;
              &lt;version&gt;2.0.2&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.amazonaws&lt;/groupId&gt;
              &lt;artifactId&gt;aws-lambda-java-core&lt;/artifactId&gt;
              &lt;version&gt;1.1.0&lt;/version&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-configuration-processor&lt;/artifactId&gt;
              &lt;optional&gt;true&lt;/optional&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-deploy-plugin&lt;/artifactId&gt;
                  &lt;configuration&gt;
                      &lt;skip&gt;true&lt;/skip&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-shade-plugin&lt;/artifactId&gt;
                  &lt;configuration&gt;
                      &lt;createDependencyReducedPom&gt;false&lt;/createDependencyReducedPom&gt;
                      &lt;shadedArtifactAttached&gt;true&lt;/shadedArtifactAttached&gt;
                      &lt;shadedClassifierName&gt;aws&lt;/shadedClassifierName&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>JobTestApplication.java (a sample PoC application I'm using to try to get this dang thing working, then I'm going to add more advanced features to it one by one):</p>
  
  <pre><code>package jobtest;
  
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.context.annotation.Bean;
  
  import java.util.function.Function;
  
  @SpringBootApplication
  public class JobTestApplication {
      public static void main(String[] args) {
          SpringApplication.run(JobTestApplication.class, args);
      }
  
      @Bean
      public Function&lt;Object, String&gt; uppercase() {
          return input -&gt; "Hello World!";
      }
  }
  </code></pre>
  
  <p>Can someone explain to me what I'm doing wrong?  I've spent 2 and a half days on this and I can't figure it out.  Thanks.</p>