Skip to content

codeputer/spring-cloud-function-adapters

 
 

Repository files navigation

This project provides an adapter layer for a Spring Cloud Function application onto AWS Lambda. You can write an app with a single @Bean of type Function, Consumer or Supplier and it will be deployable in AWS if you get the JAR file layed out right. The best way to make it work is to include spring-cloud-function-context as a dependency, but not the higher level adapters (e.g. spring-cloud-function-web or spring-cloud-function-stream).

The adapter has a couple of generic request handlers that you can use. The most generic is SpringBootStreamHandler, which uses a Jackson ObjectMapper provided by Spring Boot to serialize and deserialize the objects in the function. There is also a SpringBootRequestHandler which you can extend, and provide the input and output types as type parameters (enabling AWS to inspect the class and do the JSON conversions itself).

If your app has more than one @Bean of type Function etc. then you can choose the one to use by configuring function.name (e.g. as FUNCTION_NAME environment variable in AWS). The functions are extracted from the Spring Cloud FunctionCatalog (searching first for Function then Consumer and finally Supplier).

Notes on JAR Layout

You don’t need the Spring Cloud Function Web or Stream adapter at runtime in Lambda, so you might need to exlcude those before you create the JAR you send to AWS. A Lambda application has to be shaded, but a Spring Boot standalone application does not, so you can run the same app using 2 separate jars (as per the sample here). The sample app creates 2 jar files, one with an aws classifier for deploying in Lambda, and one executable (thin) jar that includes spring-cloud-function-web at runtime.

Build

./mvnw -U clean package

Upload

aws lambda create-function --function-name Uppercase --role arn:aws:iam::[USERID]:role/service-role/[ROLE] --zip-file fileb://spring-cloud-function-adapter-sample/target/spring-cloud-function-adapter-sample-1.0.0.BUILD-SNAPSHOT-aws.jar --handler org.springframework.cloud.function.adapter.aws.SpringBootStreamHandler --description "Spring Cloud Function Adapter Example" --runtime java8 --region us-east-1 --timeout 30 --memory-size 1024 --publish

The sample should work with --handler example.Handler as well (this is an empty subclass of SpringBootRequestHandler). If you don’t specify the handler that way then the Handler class is not used (so you don’t need it if you go with the generic SpringBootStreamHandler like above).

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 60.2%
  • Shell 39.8%