Amazon API Gateway with Spring boot -Tricks and Hacks
Amazon API Gateway is an AWS service for creating, publishing, maintaining, monitoring, and securing REST , WebSocket APIs ,HTTP API and Rest private Api at any scale. API developers can create APIs that access AWS or other web services as well as data stored in the AWS Cloud.
When you start reading documentation everything looks easy-peasy .
Now think a scenario we have developed a monolithic app (Save Cloud cost to deploy the monolithic app on single EBS) and developed 30 to 50 rest API and want to expose on AWS API gateway.
We can use the following two possible approach
- Build: Create manually each API and integrate http or lambda endpoint etc
2. Import: Create Swagger or Open API 3 and import into the gateway
Both cases we need to spend lots of time either manually create API or create swagger and if you see above scenario with 30 -50 rest API in the monolithic app will increase the complexity as we need to add integration and then validate all API .
If you developed your application using Spring framework then the following hack will save lots of time, let’s start step by step
I am not going to cover how to create rest API , the assumption is that you already developed rest API .
- Add Swagger into your spring app ,need to update pom and add swagger config code.
Pom file:
<dependencies>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
<scope>compile</scope>
</dependency>
</dependencies>
Code :
2. Navigate to your Swagger docs endpoint http://localhost:5000/swagger-ui.html
3. In chrome browser press “ctrl+Shift+I “ and navigate to the network tab and filter by XHR requests
4. Right click on the XHR request that ends in ?format=openapi
Open into new browser.
5. Save as Json file or copy and paste that into a new JSON file
Now open saved Json into text editor
- Replace “host”: “localhost:5000” with your EBS url
2 .Search “produces” in json and replace all “*/*” with “application/json”
All set to deploy your swagger on AWS Api gateway
Create a New Stage
After the initial deployment, you can add more stages and associate them with existing deployments. You can use the API Gateway console to create and use a new stage or choose an existing stage while deploying an API. In general, you can add a new stage to an API deployment before redeploying the API. To do so using the API Gateway console, follow the instructions below.
- From the APIs navigation pane, choose Stages under an API.
- From the Stages navigation pane, choose Create.
- Under Create Stage, type a stage name, for example,
prod
, for Stage name.
Conclusion
This article gives a good starting point to deploy an AWS gateway with no time using existing Spring swagger documentation, I hope it will save your time.