惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
月光博客
月光博客
大猫的无限游戏
大猫的无限游戏
GbyAI
GbyAI
博客园 - 叶小钗
小众软件
小众软件
WordPress大学
WordPress大学
I
InfoQ
Last Week in AI
Last Week in AI
Vercel News
Vercel News
博客园 - Franky
Stack Overflow Blog
Stack Overflow Blog
P
Proofpoint News Feed
A
About on SuperTechFans
Engineering at Meta
Engineering at Meta
腾讯CDC
D
DataBreaches.Net
有赞技术团队
有赞技术团队
宝玉的分享
宝玉的分享
Jina AI
Jina AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
G
Google Developers Blog
V
Visual Studio Blog
酷 壳 – CoolShell
酷 壳 – CoolShell

Maciej Walkowiak - Java & Spring

Blog Generating HTTP clients in Spring Boot application from OpenAPI spec PostgreSQL and UUID as primary key Dynamic Projections with Spring Data JPA Container logs with Spring Boot and Testcontainers Reified Generics in Java? Faster integration tests with reusable Testcontainers and Flyway Running one-time jobs with Quartz and Spring Boot The best way to use Testcontainers with Spring Boot Spring Boot & Flyway - clear database between integration tests What's new in Spring? Activate Maven Profile by Operating System Spring Boot with Thymeleaf and Tailwind CSS - Complete Guide How to publish a Java library to Maven Central - Complete Guide Docker Compose - waiting until containers are ready Single file Java applications with JBang Beautiful bash scripts with Gum Running Java on CRaC How to log PostgreSQL queries with Testcontainers Spring Boot 3.0 & GraalVM Native Image - not a free lunch Loading classpath resources to String with a custom JUnit extension Creating Project Templates with Cookiecutter Auto-Registering JUnit 5 extensions Spring Boot component scanning without annotations Listing Maven dependencies in Spring Boot Actuator Info endpoint Spring Cloud AWS 2.3 RC2 Released How I built vlad-cli - command line interface to Vlad Mihalcea The State of Java Relational Persistence On Choosing a Tech Stack
Creating Spring Cloud Function projects with AWS SAM
2022-08-03 · via Maciej Walkowiak - Java & Spring
Published on
  • Spring cloud function
  • Aws
  • Aws sam

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: https://github.com/maciejwalkowiak/aws-sam-spring-cloud-function-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
│   │                   ├── FunctionApplication.java
│   │                   └── HelloWorldFunction.java
│   └── test
│       └── java
│           └── com
│               └── example
│                   └── demo
│                       └── FunctionApplicationTests.java
└── 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 org.springframework.cloud.function.adapter.aws.FunctionInvoker::handleRequest
  • 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 https://github.com/maciejwalkowiak/aws-sam-spring-cloud-function-template.

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.

Let's stay in touch and follow me on Twitter: @maciejwalkowiak

Subscribe to RSS feed