top of page
download.png

CloudMates Blog

How to automate creating AWS API Gateway with OpenAPI3 standard using Terraform

As you might know APIs can be built using OpenAPI specification file aka swagger definition which allows defining a standardardized way and a language-agnostic interface to RESTful APIs to discover their capabilities in an easy and human readable format without the need to access the source code, documentation or intercepting the network traffic.

You can read more on that in this link:


Using this method to describe the functionality of a RESTful API, we can leverage that in automating the process of building APIs in general and in the cloud in particular in a very easy way. Generally speaking, any provisioning platform such as Terraform and the like can be used to provision any potential resource but combing the use of OpenAPI3 definition with these provisioning platforms makes the process super easy. For instance, if you were to create an API Gateway in AWS using Terraform, you would technically need to create at least four objects and a bunch of input variables to simply create a single method such as GET or PUT with a single integration. However, with OpenAPI3, we can completely abstract the creation of all related objects and resources and simply reference the OpenAPI definition file and everything will automatically be created and configured.


Let's take an example here, I've got an API Gateway that has two resources and two methods namely GET and PUT:


/id ---> GET ----> Lambda Function

/store ---> PUT ----> S3 Bucket



To get the OpenAPI definition file, we simply click on Stages ---> Export and choose Export as Swagger + API Gateway Extensions


Download the file and fire up your favorite file editor and use this sample Terraform code:


resource "aws_api_gateway_rest_api" "restful_api" {
 name = "restful-api"
 body = file("${path.module}/S3ProxyApi-dev-oas30-apigateway.json")
 endpoint_configuration {
  types = ["REGIONAL"]
  }
} 

In line 3, you would reference the json file that you downloaded in the previous step. Now you can run:


terraform init
terraform apply






0 comments

Recent Posts

See All

Comments


bottom of page