Go Serverless - Run Spring Cloud Function App on AWS Lambda with Serverless Framework and Gitlab Deploy
Akash Tyagi
Associate Director at Bank Julius Baer | AWS Community Builder | DevOps | Architecture | M Tech @NUS | CI/CD | Test Automation |
Source code kept here:
Objective:
Who this is for:
Background:
Four things involved here:
Spring Cloud Function
Let me give a quick summary.
Read through this to understand more on it.
AWS lambda:
As stated earlier it is an AWS offering to support FaaS i.e. Function as a Service.
Serverless Framework
Read through this to learn more on the topic:
Gitlab CI CD:
Steps to take:
We will do the below in the series of steps:
Let's start.
First Step: Create the Spring Cloud Function App
Add dependencies
Spring Version: 3.0.4
Java version: 17
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-adapter-aws</artifactId>
</dependency>
Notice there is a third dependency i.e. spring-cloud-function-adapter-aws
@Bean
public Supplier<String> hello(){
return ()-> "Hello World";
}
@Bean
public Function<String, String> helloName() {
return (name)-> "Hello '" + name + "'";
}
@Bean
public Consumer<String> helloJustLog() {
return (name)-> log.info("Hello '" + name + "'");
}
curl -X GET https://localhost:8084/hello
Hello World
curl -X GET https://localhost:8084/helloName/Akash
Hello 'Akash'
curl -X GET https://localhost:8084/helloJustLog/Akash
Next Step: Create a Shaded jar
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<configuration>
<finalName>app-aws-lambda</finalName>
<createDependencyReducedPom>false</createDependencyReducedPom>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>aws</shadedClassifierName>
</configuration>
</plugin>
领英推荐
Next Step: Use Serverless Framework to deploy the app in AWS Lambda
npm install -g serverless
serverless create --help
serverless create --template aws-java-maven
service: my-first-serverless-fw-project
frameworkVersion: '3'
provider:
name: aws # since we are on AWS
runtime: java11 # we want the run time to be java 11
package:
artifact: target/app-aws-lambda.jar # place name of the shaded jar here after mvn clean install
functions:
cloud-app:
url: true # a Public URL will be automatically created
handler: org.springframework.cloud.function.adapter.aws.FunctionInvoker::handleRequest
# Handler is coming from spring-cloud-function-adapter-aws maven pom dependency
# This always remains the same and acts as main method/starting point of the Lambda function
serverless deploy
Since in "serverless.yml" we marked url: true and hence serverless fw created a public url for our function.
# invoke the hello GET end point
curl --header 'spring.cloud.function.definition: hello'? https://vkj52pn2crl2plojx75wnnj4zy0mkyqt.lambda-url.us-east-1.on.aws/
# invoke the helloName POST end point
curl --request POST --header 'spring.cloud.function.definition: helloName' --data 'Akash' https://vkj52pn2crl2plojx75wnnj4zy0mkyqt.lambda-url.us-east-1.on.aws/
Next Step: Use Gitlab to automatically deploy the app via CI CD pipeline
image: node:latest
stages:
- build
- deploy
mvn-build:
image: maven:3.8.6-jdk-11-slim
stage: build
script:
- mvn clean install -DskipTests
artifacts:
paths:
- target/app-aws-lambda.jar
expire_in: 1 week
deploy-aws:
stage: deploy
dependencies:
- mvn-build
before_script:
- npm config set prefix /usr/local
- npm install -g serverless
script:
- serverless deploy --stage production --verbose
environment: production
Full link here of the source code here:
Last thing for this to work, you need to have AWS_ACCESS_KEY and AWS_SECRET_KEY set up in your Gitlab CI variable.
Next Steps: Clean Up!
To clean up Use below:
> serverless remove
or
> serverless remove --stage production ==> if you hvae used stage parameter to deploy, I have used it in my Gitlab CI CD
Thats all for now! try to play around with this. There is a lot you can build using Spring Cloud Function and a lot you can configure with Serverless framework. Keep trying and let me know your experiences.
Senior Java software engineer and mentor
1 年Very good who explains all step by step