Let's say you have backend services in your development environment and sometimes your service endpoints are changed. You have to manually re-import your OPEN-API or Swagger specs into your API Gateway definition. You have to re-arrange all routes, authorizations, integrations and cors.
What if I told you, you could automate this process with a simple Lambda function?
Writing the Lambda Function with Java
Create a basic spring boot project with the following dependencies, extract the tar file and open it via your favourite IDE.
curl -G https://start.spring.io/starter.tgz \
-d dependencies=devtools,lombok,web \
-d type=maven-project \
-d language=java \
-d platformVersion=2.7.9 \
-d jvmVersion=17 \
-d groupId=dev.habil \
-d packaging=jar \
-d artifactId=apigw-sync \
-d name=apigw-sync \
-d description=Demo%20project%20for%20Spring%20Boot \
-d packageName=dev.habil.apigw \
-d baseDir=lambda-apigw-sync | tar -xzvf -
Add required 3party dependencies
Add required maven plugins & configurations
Let's create a configuration class for AWS API Gateway.
Now, we need to create a service class
Time for the Function class.
Here is the Request Object
Change SpringApplication to FunctionalSpringApplication
You can test in the local environment by creating a "controller" for testing and connecting to the service.
Package The Function & Deploy
Let's package our function. Open a terminal while in our function root directory and execute the following maven command.
mvn clean package
Now we packaged our function, at this point, you have to choose your deployment strategy. You have
- "jar" deploy with AWS Console
- Create and upload with aws-cli
- Create a container, push it to the ECR and create lambda deploy options.
For the sake of simplicity, I'll deploy my function via AWS Console.
Open AWS Console and follow the steps
- Create Function
- Choose "Author from scratch"
- Give the function a name
- Choose Runtime "java11"
- Create Function.
Now you need to upload your jar file. To get started click the "Upload From" button, select zip or jar file and start uploading.
After setup your function General Configuration settings (I chose 512MB Ram and 30s timeout), you have one last configuration to do.
When you are "Code" tab, edit runtime settings and update handler input with this line.
Thats all! You may trigger your function with the Test tab.
Once you have ok with your function, you can trigger your function with CI/CD job, Jira or manual.
In this article, we created a function using the simplicity of AWS Lambda. We can run our function with the given parameters and make updates to the relevant endpoints whenever we want.
See you in the next article.
The source code can be found here: