Maciej Walkowiak

Creating Spring Cloud Function projects with AWS SAM

Russia has invaded Ukraine and already killed tens of thousands of civilians, with many more raped and tortured. Ukraine needs your help!

Help Ukraine Now!

One option to deploy serverless applications built with AWS Lambda is to use AWS SAM - a framework that comes with:

  • a template specification (think higher level abstraction over Cloud Formation)
  • a CLI that simplifies creating, deploying, testing and running application locally.

To create new SAM project, you can simply call sam init and then choose one of the available templates for variety of programming languages - including Java.

Java templates baked into SAM CLI are literally pure Java - no frameworks other than the standard AWS Lambda SDK are included.

To speed up creating Spring Cloud Function based projects, I created a template a custom SAM template:

Calling following command:

$ sam init --location gh:maciejwalkowiak/aws-sam-spring-cloud-function-template

.. generates:

  • basic Spring Cloud Function project with AWS Lambda adapter running on java11 runtime (no GraalVM native image yet)
  • SAM template containing function and API Gateway declaration
$ tree .
├── mvnw
├── mvnw.cmd
├── pom.xml
├── src
│   ├── main
│   │   └── java
│   │       └── com
│   │           └── example
│   │               └── demo
│   │                   ├──
│   │                   └──
│   └── test
│       └── java
│           └── com
│               └── example
│                   └── demo
│                       └──
└── template.yaml

Now you can build project with ./mvnw package and execute sam deploy to deploy function to AWS or sam local start-api to start function locally.

You might be wondering how is this project really different from regular SAM template for Java, except of course using Spring Cloud Function in the Java code. There are only few - but important things to notice:

  • function handler in template.yml has to be set to
  • project has to have SPRING_CLOUD_FUNCTION_DEFINITION environment variable to tell the framework which function to call (otherwise it will try to figure out based on the message but this requires more/different configuration)
  • Lambda has to be shaded, and following Maven Shade Plugin configuration has to be used
  • Since the dependencies are shaded, there is no need to include dependency JARs - you rather should exclude them from the JAR, otherwise it will be double the size. This is done by adding a dependency to spring-boot-thin-layout in spring-boot-maven-plugin configuration

If you believe this template can be improved, you are welcome to create an issue and even more welcome to submit a PR to

In the future, I plan to extend this template with a question about the target runtime and give users an option to choose between java11 and a custom runtime running GraalVM native image.