├── .gitignore ├── README ├── pom.xml └── src └── main ├── java └── robbaily │ ├── Hello.java │ ├── RequestComponent.java │ ├── SpringBootInvocation.java │ └── TestConfiguration.java └── resources ├── application.properties └── log4j.properties /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | *.iml 3 | target -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | This project demonstrates how you can use Spring Boot correctly in an AWS Lambda handler 2 | if you are so inclined to do so. 3 | 4 | Came about while looking at http://stackoverflow.com/questions/36081908/using-spring-boot-cloud-with-amazon-aws-lambda-does-not-inject-values -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | robbaily 8 | aws-lambda-spring 9 | 1.0-SNAPSHOT 10 | jar 11 | 12 | 13 | UTF-8 14 | 15 | 1.2 16 | 1.5.2.RELEASE 17 | 18 | 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-dependencies 24 | pom 25 | ${springboot.version} 26 | import 27 | 28 | 29 | 30 | 31 | 32 | 33 | com.amazonaws 34 | aws-lambda-java-core 35 | 1.1.0 36 | 37 | 38 | com.amazonaws 39 | aws-lambda-java-log4j 40 | 1.0.0 41 | 42 | 43 | commons-logging 44 | commons-logging 45 | ${commons-logging.version} 46 | 47 | 48 | 49 | org.springframework.boot 50 | spring-boot-starter 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | org.springframework.boot 59 | spring-boot-maven-plugin 60 | ${springboot.version} 61 | 62 | 63 | 64 | 65 | 66 | org.springframework.boot 67 | spring-boot-maven-plugin 68 | 69 | 72 | MODULE 73 | 74 | 75 | 76 | 77 | repackage 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /src/main/java/robbaily/Hello.java: -------------------------------------------------------------------------------- 1 | package robbaily; 2 | 3 | import com.amazonaws.services.lambda.runtime.*; 4 | 5 | /** 6 | * Created by Rob on 3/22/2016. 7 | */ 8 | public class Hello implements RequestHandler { 9 | public String handleRequest(String name, Context context) { 10 | SpringBootInvocation springBootInvocation = new SpringBootInvocation(); 11 | springBootInvocation.run(); 12 | return springBootInvocation.getApplicationContext().getBean(RequestComponent.class).processRequest(name,context); 13 | } 14 | 15 | public static void main(String[] args) { 16 | SpringBootInvocation springBootInvocation = new SpringBootInvocation(); 17 | springBootInvocation.run(); 18 | springBootInvocation.getApplicationContext().getBean(RequestComponent.class).processRequest("Robert",null); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/robbaily/RequestComponent.java: -------------------------------------------------------------------------------- 1 | package robbaily; 2 | 3 | import com.amazonaws.services.lambda.runtime.Context; 4 | import org.apache.commons.logging.Log; 5 | import org.apache.commons.logging.LogFactory; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.beans.factory.annotation.Value; 8 | import org.springframework.context.ApplicationContext; 9 | import org.springframework.context.annotation.Scope; 10 | import org.springframework.core.env.Environment; 11 | import org.springframework.stereotype.Component; 12 | 13 | /** 14 | * Created by Rob on 3/22/2016. 15 | */ 16 | @Component 17 | @Scope("prototype") 18 | public class RequestComponent { 19 | static Log log = LogFactory.getLog(RequestComponent.class); 20 | 21 | @Autowired 22 | ApplicationContext applicationContext; 23 | 24 | @Autowired 25 | Environment environment; 26 | 27 | @Value(value = "${configtest.value}") 28 | private String configValue; 29 | 30 | public String processRequest(String name, Context context) { 31 | log.info("In the hello function"); 32 | log.info("Test value here = "+ configValue); 33 | log.info("env value = " + environment.getProperty("configtest.value")); 34 | TestConfiguration testConfiguration = applicationContext.getBean(TestConfiguration.class); 35 | log.info("Test configuration value = " + testConfiguration.getConfigValue()); 36 | return String.format("Hello %s.", name); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/robbaily/SpringBootInvocation.java: -------------------------------------------------------------------------------- 1 | package robbaily; 2 | 3 | import org.springframework.boot.Banner; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | import org.springframework.boot.builder.SpringApplicationBuilder; 8 | import org.springframework.context.ApplicationContext; 9 | import org.springframework.context.annotation.Bean; 10 | import org.springframework.context.annotation.ComponentScan; 11 | import org.springframework.context.annotation.Configuration; 12 | import org.springframework.context.annotation.Scope; 13 | 14 | /** 15 | * Created by Rob on 3/22/2016. 16 | */ 17 | @Configuration 18 | @EnableAutoConfiguration 19 | @ComponentScan() 20 | public class SpringBootInvocation { 21 | private ApplicationContext applicationContext; 22 | 23 | public SpringBootInvocation() { 24 | } 25 | 26 | public void run() { 27 | String[] args = new String[0]; 28 | applicationContext = SpringApplication.run(SpringBootInvocation.class, args); 29 | /* 30 | applicationContext = new SpringApplicationBuilder() 31 | .main(getClass()) 32 | .bannerMode(Banner.Mode.OFF) 33 | .web(false) 34 | .sources(getClass()) 35 | .addCommandLineProperties(false) 36 | .build() 37 | .run(); 38 | */ 39 | } 40 | 41 | public ApplicationContext getApplicationContext() { 42 | return applicationContext; 43 | } 44 | 45 | @Bean 46 | @Scope("prototype") 47 | public TestConfiguration testConfiguration() { 48 | return new TestConfiguration(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/robbaily/TestConfiguration.java: -------------------------------------------------------------------------------- 1 | package robbaily; 2 | 3 | import org.springframework.beans.factory.annotation.Value; 4 | 5 | /** 6 | * Created by Rob on 3/22/2016. 7 | */ 8 | public class TestConfiguration { 9 | @Value(value = "${configtest.value:default config value}") 10 | private String configValue; 11 | 12 | public String getConfigValue() { 13 | return configValue; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | logging.level.robbaily=DEBUG 2 | logging.level.org.springframework=DEBUG 3 | logging.level.org.springframework.boot.autoconfigure=TRACE 4 | logging.level.org.springframework.context.annotation.ConfigurationClassParser=TRACE 5 | logging.level.org.springframework.context.support.PostProcessorRegistrationDelegate=TRACE 6 | 7 | spring.application.name=Lamba Test 8 | 9 | configtest.value=My test value 10 | 11 | -------------------------------------------------------------------------------- /src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log = . 2 | log4j.rootLogger = DEBUG, LAMBDA 3 | 4 | #Define the LAMBDA appender 5 | log4j.appender.LAMBDA=com.amazonaws.services.lambda.runtime.log4j.LambdaAppender 6 | log4j.appender.LAMBDA.layout=org.apache.log4j.PatternLayout 7 | log4j.appender.LAMBDA.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss} <%X{AWSRequestId}> %-5p %c{1}:%L - %m%n --------------------------------------------------------------------------------