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

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

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:

AWS CLI Installation:, 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.


export AWS_PROFILE=terraform-aws

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

  • : 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 "", 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. This is where we let terraform know which cloud provider we are gonna use this with 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 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 with "/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 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,


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.

We could talk about many other ways and advanced options to deploy API along with Terraform in the upcoming posts.