mindas mindas - 1 year ago 172
Java Question

Using Spring boot/cloud with Amazon AWS lambda does not inject values

I have an AWS lambda

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.

public class MyClass implements com.amazonaws.services.lambda.runtime.RequestHandler<I, O> {
public O handleRequest(I input, Context context) {
ApplicationContext applicationContext = new SpringApplicationBuilder()

// 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

public class SomeConfigClass {
public String foo;

name: my_service
uri: http://my.server
failFast: true
profile: localdev

What have I tried:

  • using regular Spring MVC, but this doesn't have integration with
    injection/Spring cloud

  • using
    - 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
    to ensure the value is brought back

  • verified to ensure that .jar actually contains
    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
(neither as environment variable nor as
parameter). Nor I can control the underlying classes which actually launch
- 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?

Answer Source

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:


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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download