Provisioning AWS API Gateway using Terraform

AWS provides two types of API Gateways, one specifically to support REST APIs and other to support all type of HTTP APIs. Visit the documentation to compare and choose one that suits your needs.

In this story we will try to provision an HTTP API Gateway using terraform. A complete example repository is hosted in github.

Let’s start defining API Gateway in the terraform file as follows

You can either define the variables like api_gateway_name, used in your terrafrom configuration, in configuration files or pass them to terrafrom while you run terraform commands.

You can create a stage and link to API Gateway as follows. A stage is a named reference to a deployment of the API. See documentation for more details

You can create a custom domain if you like and link it to your api gateway stage as follows. This assumes that you already have an SSL certificate managed by AWS certificate manager and you might have created it using terraform itself.

Now to route the requests to your api (let’s say for example a lambda: “sample_lambda_resource”), use “aws_apigatewayv2_integration” and “aws_apigatewayv2_route” as follows

If you need a custom authorizer, use “aws_apigatewayv2_authorizer” and in the route you need to link to the custom authorizer if you want to use it with that route. See below for an example. Here we assume that you have a custom lambda function that is used for authorization (again may be created via terraform)

And finally if you need to create a route53 entry and link it to your api gateway, see below example

Visit Terraform AWS API Gateway documentation to see all the supported options

Happy terraforming.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

A side project that went too far — Part 1: pythonping + matplotlib

How To Spend Thanksgiving ALONE

Asp.NET Core 2.0 WebApi JWT Role Based Authentication/Authorization with Custom Tables and Identity

Work-from-Home hack — How to run a Windows PC from an iPhone

Phone, tablet and pc on a table

How to Install Apache on CentOS7

How To Set Up Django with Postgres, Nginx, and Gunicorn on Ubuntu 14.04

Mio named Gartner Cool Vendor for Connecting Digital Workplace Applications

Gartner Cool Vendor for Connecting Digital Workplace Applications

Microsoft Teams vs Skype for Business — get to know Teams

Microsoft Teams vs Skype for Business

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sarath Kallatt Sivasankaran

Sarath Kallatt Sivasankaran

More from Medium

Cloud state management with Terraform(Project)

Manage on-premise instances using AWS System Manager and Terraform

Hostin a Static Website on AWS S3 using Terraform

Automating AWS infrastructure using Terraform and GitHub Actions — Introduction to GitOps — Part 2