Lambda Function for AWS API Gateway ReImport Operations
Intro
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?
>> This article was originally published on my blog:
https://habil.dev/lambda-function-for-aws-api-gateway-re-import-operations/
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=3.0.4 \
-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
💡 If you only need a re-import operation, you can safely remove other methods in the service class.
💡Don’t use provided CORS configuration in your Production setup for your safety.
Time for the Function class.
Here is the Request Object
Change SpringApplication to FunctionalSpringApplication
Local Test
You can test in the local environment by creating a “controller” for testing and connecting to the service.
💡Don’t forget to comment or remove your test controller class after local testing. Controller classes don’t work with functional spring boot applications. (At least for now)
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 521MB 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.
org.springframework.cloud.function.adapter.aws.FunctionInvoker::handleRequest
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.
Result
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:
habil-dev-blog/spring-boot-samples/lambda-apigw-sync at main · habil/habil-dev-blog