16

I have an AWS lambda RequestHandler class which is invoked directly by AWS. Eventually I need to get it working with Spring Boot because I need it to be able to retrieve data from Spring Cloud configuration server.

The problem is that the code works if I run it locally from my own dev environment but fails to inject config values when deployed on AWS.

@Configuration
@EnableAutoConfiguration
@ComponentScan("my.package")
public class MyClass implements com.amazonaws.services.lambda.runtime.RequestHandler<I, O> {
   public O handleRequest(I input, Context context) {
        ApplicationContext applicationContext = new SpringApplicationBuilder()
                .main(getClass())
                .showBanner(false)
                .web(false)
                .sources(getClass())
                .addCommandLineProperties(false)
                .build()
                .run();

        log.info(applicationContext.getBean(SomeConfigClass.class).foo);
        // prints cloud-injected value when running from local dev env
        //
        // prints "${path.to.value}" literal when running from AWS 
        //    even though Spring Boot starts successfully without errors
   }
}

@Configuration
public class SomeConfigClass {
   @Value("${path.to.value}")
   public String foo;
}

src/main/resources/bootstrap.yml:
spring:
  application:
    name: my_service
cloud:
  config:
    uri: http://my.server
    failFast: true
    profile: localdev

What have I tried:

  • using regular Spring MVC, but this doesn't have integration with @Value injection/Spring cloud.
  • using @PropertySource - but found out it doesn't support .yml files
  • verified to ensure the config server is serving requests to any IP address (there's no IP address filtering)
  • running curl to ensure the value is brought back
  • verified to ensure that .jar actually contains bootstrap.yml at jar root
  • verified to ensure that .jar actually contains Spring Boot classes. FWIW I'm using Maven shade plugin which packages the project into a fat .jar with all dependencies.

Note: AWS Lambda does not support environment variables and therefore I can not set anything like spring.application.name (neither as environment variable nor as -D parameter). Nor I can control the underlying classes which actually launch MyClass - this is completely transparent to the end user. I just package the jar and provide the entry point (class name), rest is taken care of.

Is there anything I could have missed? Any way I could debug this better?

11
  • is your config server available from 'outside'? I mean, are you sure its not hidden behind ip protection or something similar? Commented Mar 18, 2016 at 11:11
  • @freakman good point, but no - just checked, the config request is served from any host. Commented Mar 18, 2016 at 11:23
  • 1
    I am assuming you have looked at this: cloud.spring.io/spring-cloud-aws/… and it is not working for you? I don't really know much about AWS Lambda but saw this post: github.com/cagataygurturk/aws-lambda-java-boilerplate. Maybe you already checked these but wanted to confirm. Commented Mar 21, 2016 at 14:05
  • @RobBaily thanks, aws-java-lambda-boilerplate is using classic Spring .xml app context approach (see AbstractHandler.java:58) which works, but as I mentioned in my post, it doesn't have integration with with @Value injection/Spring cloud (whereas Spring Boot does). Re: documentation - yes, I'm using spring-cloud-config artifactId in my Maven cfg, unfortunately there's no mention of lambda in the documentation so not much I can pick up from there. Commented Mar 21, 2016 at 14:33
  • @mindas Have you tried setting a log level locally for Spring Cloud classes where you can see what it is doing? If so then you may be able to set the same logging levels for your Lambda deployment to see what is different. Not sure how much logging is available but you could also clone it and add your own log messages. Commented Mar 21, 2016 at 21:35

2 Answers 2

24
+200

After a bit of debugging I have determined that the issue is with using the Maven Shade plugin. Spring Boot looks in its autoconfigure jar for a META-INF/spring.factories jar see here for some information on this. In order to package a Spring Boot jar correctly you need to use the Spring Boot Maven Plugin and set it up to run during the maven repackage phase. The reason it works in your local IDE is because you are not running the Shade packaged jar. They do some special magic in their plugin to get things in the right spot that the Shade plugin is unaware of.

I was able to create some sample code that initially was not injecting values but works now that I used the correct plugin. See this GitHub repo to check out what I have done.

I did not connect it with Spring Cloud but now that the rest of the Spring Boot injection is working I think it should be straightforward.

As I mentioned in the comments you may want to consider just a simple REST call to get the cloud configuration and inject it yourself to save on the overhead of loading a Spring application with every request.

UPDATE: For Spring Boot 1.4.x you must provide this configuration in the Spring Boot plugin:

            <configuration>
                <layout>MODULE</layout>
            </configuration>

If you do not then by default the new behavior of the plugin is to put all of your jars under BOOT-INF as the intent is for the jar to be executable and have the bootstrap process load it. I found this out while addressing adding a warning for the situation that was encountered here. See https://github.com/spring-projects/spring-boot/issues/5465 for reference.

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

6 Comments

Spring Boot Maven Plugin was the missing element. I had to tweak the settings a bit but the whole combination worked. Interesting fact, the bootstrap took 10 seconds (!) according to logs - this is definitely a factor which discourages using Spring Boot in AWS environment. Nevertheless, thanks for your hard work, much appreciated!
@mindas Yes I think Spring Boot may make more sense in one of their container environments rather than Lambda. I am also going to see about putting in a request for some type of warning when this happens as it was definitely not obvious and could happen in other situation.
The good news is that Amazon is keeping the Lambda instance alive for some (undefined?) time before discarding it, so the next request can be served using the same instance. This means that code should check if application context has already been initialized and skip it if so. That said, for frequent requests the problem of overhead is irrelevant.
I got this warning for 1.5.7.RELEASE: Module layout is deprecated. Please use a custom LayoutFactory instead.
I don't much about this but it looks like it is reference here docs.spring.io/spring-boot/docs/1.5.x-SNAPSHOT/reference/… You may want to post a separate question if you are having trouble with a new Spring Boot version.
|
1

For those of you using Spring Boot 3, some of these same issues exit. I was not implementing any kind of actual web service with controllers, etc. I merely wanted to take advantage of Spring Boot autoconfigurations and dependency injection. (I've heard the arguments about why this is terrible, but so far so good.) Every example regarding Spring Cloud ended up requiring a web context and a servlet container and yielded lots of exceptions in my logs. I knew I had to get the context initialized with my @Configuration class and get the beans back out when I went to process the incoming request.

That's when I found the comment that mentions https://github.com/cagataygurturk/aws-lambda-java-boilerplate. The accepted answer for this question is basically the same: https://github.com/rob-baily/aws-lambda-spring.git. I tried this and my lambda actually runs. I have other issues, but it is no longer Spring Boot that is the problem.

In order to reduce start up time, in my @Configuration class, I do not do component scan. I use @Import to get what I need (mostly) and a few @Bean definitions. I am using AutoConfiguration for the database initialization/setup.

I am using the shade plugin to create a jar that can be deployed via the console. I have yet to try SAM.

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.