summary: "How to create API Gateway endpoints with Terraform."
---
Right when we first started to use Terraform, we had a lot of problems with the API Gateway. We had to create a lot of resources, manage 1,000s of lines of `.tf` files, and it was a lot of work that required more attention and time, things that are critical in software development as we all know.
So we decided to create a module to help us with this. Big thanks to [Stephano](https://www.linkedin.com/in/stephano-macedo/) who helped me with this!
## Before
Basically, when we are developing a new API, we need to create a lot of resources in the API Gateway. We need to create a new resource, a new method and a new integration, and therefore connecting all of them using their respectives IDs.
Let's suppose a endpoint called `/users/all`. This is a snippet of the code we had before:
Obviously there is more code to that, but this is the main part of it and that's what we will be using in the module.
## After (Creating a module)
Now, we can create a module to help us with this. We can start by summarizing what we had into a separate folder. We will call this folder `terraform/modules/api`, inside it there will be a couple of files:
### variables.tf
Here we will define the variables that we will use in the module, what will come from the outside.
```terraform
# This is the parent resource ID in case we have something like /users/all/prune
variable "parent_id" {
description = "Resource Parent ID"
type = string
}
# This is the last part of the path, we can infer it from the endpoint URI
variable "path_part" {
description = "Path Part"
type = string
}
# Here we will put all the HTTP methods that the endpoint will accept
variable "http_methods" {
description = "HTTP Methods"
type = list(string)
default = []
}
# The complete endpoint URI
variable "uri" {
description = "URI"
type = string
default = ""
}
# The API Gateway ID
variable "gateway_id" {
description = "API Gateway ID"
type = string
}
# If we have a URI that won't accept any HTTP method, we set this to true
variable "only_resource" {
description = "Only create the resource"
type = bool
default = false
}
# Authorization as an example so that we can pass the headers
variable "authorization" {
description = "Required authorization"
type = bool
default = false
}
```
### outputs.tf
This file is needed for at least one important variable, which is the `resource_id`. That's needed if we have some endpoint like `/users/all/prune`.
```terraform
output "resource_id" {
value = local.resource_id
}
```
### locals.tf
As we referenced the `resource_id` in the `outputs.tf`, we need to define it in the `locals.tf`.
Since we need one `aws_api_gateway_method` for each HTTP Method, we use the `count` to iterate over the list of HTTP Methods and create one api_gateway_method for each http method we defined in the `var.http_methods` list.
Now that we have the module, we can use it in our `main.tf` file. We will use the same example as before, but now we will use the module and we will create some other endpoints as example as well.
```terraform
# this is our main API endpoint, we don't want to receive any request here, so we will only create the resource
For one endpoint, we went from having to manage 15 lines splitted in 3 files to just 5 lines inside of one file. If you have to manage hundreds of endpoints, that will be a great help.
We can also add the `WWW-Authenticate` header to the request for example. We tried to do that by adding it to the files properly, but it didn't work. The reason was that the API Gateway was not passing the `WWW-Authenticate` to our API, and that's because of the name of the header. You may call it `WWW-Authenticate-Header` for example and it will work.
This code has not been tested "as is", though it has been tested as part of a bigger project. There is always room for improvements and more possibilities depending on the context, but it's a good start.