Deploy a Rest API with a Proxy Method on AWS API Gateway with Terraform

Dilusha Gonagala
Geek Culture
Published in
4 min readJun 11, 2021

--

In this tutorial, we will see how to deploy a Rest API with a Proxy Method that’s hosted on AWS using Terraform.

Prerequisites Before we start: You will need to terraform, AWS-CLI and the configuration done in your local machine.

Terraform Installation Guide: https://learn.hashicorp.com/tutorials/terraform/install-cli?in=terraform/aws-get-started

AWS CLI Installation: https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html, Set the default account to be the one you would be using for terraform. If not you could also create a new account from AWS Console for the sole purpose of terraform (Recommended) and then export that account credentials to be used on your CLI along with Terraform by assigning them into ‘AWS_PROFILE’ variable in your local machine.

E.g:

export AWS_PROFILE=terraform-aws

After the prerequisites, you would need a base structure for your directory, as below,

  • main.tf
  • providers.tf
  • variables.tf

main.tf : Could be used to have all our code blocks regarding the resources we gonna create during this tutorial. [We will implement code inside this file during each step of the setup]

P.S: It’s not a must to call this file “main.tf”, but since our only goal in this tutorial is to set up the API gateway in this project, it could be more generally defined on this file, hence the file name.

providers.tf: This is where we let terraform know which cloud provider we are gonna use this with

variables.tf: This file is where we could define your prefered Region to use on AWS, which is referenced on the above providers file

Alright now that we have the basic directory setup done, we could run the first command to initialize all the necessary files that terraform requires.

Inside the root directory where you have all those three files mentioned above, you could run the below command.

terraform init

This code will download and initialize terraform resources inside your directory, you may see a new folder called .terraform is created after this command is run.

Now that we have everything we need for terraform to work with your AWS account and region, let’s move on to creating the real deal. The Rest API.

  1. Create the API with a path that forwards the requests to the PROXY HTTP call.

Let’s create an API using resource “aws_api_gateway_rest_api”, which will include a path called “posts” where it will PROXY the request back to a sample HTTP server. In this example, we are using the famous mock API https://jsonplaceholder.typicode.com with the “/posts” route.

Now that we have written the code definition to create a new Rest API with a Proxy method, let’s move to the next steps.

2. Defining the API Deployment: this is where we tell AWS which API we should deploy

Inside this code snippet, you will notice 3 segments,

  • rest_api_id: This is the Rest API Id which we defined above
  • triggers: Redeployment: This is a trigger that tells API Gateway to re-deploy in case if the body of the API Gateway defined above changes.
  • lifecycle: This tells the API gateway to create a new one before destroying the old one.

3. Define the API Stage

By defining a stage, we could keep different versions of the deployments, which we could use to test in different environments. E.g: test, dev, prod

4. Print the deployment URL

Finally, let’s get the deployed API URL, so we could test our endpoint‌

Alright, now that we have all the code blocks in place, it’s time to call some terraform commands to get this API deployed.

First, we should run,

terraform plan

This command will check to validate your code and then check the AWS resources definitions you have defined on your code, after which if no errors you should see something like this

Plan: 3 to add, 0 to change, 0 to destroy.

You could now skim read all the things mentioned in your terminal, and if all looks good, run the below command.

terraform apply

Again, it would be a similar output except for this time you now have to type ‘yes’ to confirm and then Terraform will create all the resources that are defined on the code.

After the apply command is completed, you should be seeing something like this along with the created endpoint,

If you visit this URL, you will get this message,

{“message”: “Missing Authentication Token”}

Because it’s not a known path, since we created the path as /posts, you could this right after the stage name “test”, like below,

https://{API-ID}.execute-api.eu-central-1.amazonaws.com/test/posts

If everything is set up properly, you will see sample posts response coming from the JSON Placeholder endpoint from the proxy path.

There you have it! A Simple way to create a Proxy Endpoint with AWS Gateway using Terraform — Infrastructure as code.

Hope you found this tutorial useful, I am hoping to write about many other ways and advanced options to deploy Rest APIs along with Terraform in the upcoming posts.

--

--