Skip to content

Instantly share code, notes, and snippets.

@mendhak
Last active March 15, 2025 20:09
Show Gist options
  • Select an option

  • Save mendhak/8303d60cbfe8c9bf1905def3ccdb2176 to your computer and use it in GitHub Desktop.

Select an option

Save mendhak/8303d60cbfe8c9bf1905def3ccdb2176 to your computer and use it in GitHub Desktop.
Terraform - API Gateway with greedy path (proxy+) calling httpbin. Also includes deployment
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
}
}
provider "aws" {
region = "eu-west-1"
}
resource "aws_api_gateway_rest_api" "MyDemoAPI" {
name = "MyDemoAPI"
description = "This is my API for demonstration purposes"
}
resource "aws_api_gateway_resource" "MyDemoResource" {
rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id
parent_id = aws_api_gateway_rest_api.MyDemoAPI.root_resource_id
path_part = "{proxy+}"
}
resource "aws_api_gateway_method" "MyDemoMethod" {
rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id
resource_id = aws_api_gateway_resource.MyDemoResource.id
http_method = "GET"
authorization = "NONE"
request_parameters = {
"method.request.path.proxy" = true
}
}
resource "aws_api_gateway_integration" "MyDemoIntegration" {
rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id
resource_id = aws_api_gateway_resource.MyDemoResource.id
http_method = aws_api_gateway_method.MyDemoMethod.http_method
type = "HTTP_PROXY"
uri = "https://httpbin.org/anything/{proxy}"
integration_http_method = "GET"
cache_key_parameters = ["method.request.path.proxy"]
timeout_milliseconds = 29000
request_parameters = {
"integration.request.path.proxy" = "method.request.path.proxy"
}
}
resource "aws_api_gateway_deployment" "teststage" {
depends_on = [
aws_api_gateway_integration.MyDemoIntegration
]
rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id
stage_name = "test"
}
output "api_gateway_test_url" {
value = "${aws_api_gateway_deployment.teststage.invoke_url}/hello/world"
}
@WhatIsHeDoing

Copy link
Copy Markdown

Exactly what I needed, thank you!

@sonoman

sonoman commented Mar 17, 2020

Copy link
Copy Markdown

Access Permissions are missing...."aws_lambda_permission"

@UtsavChokshiCNU

Copy link
Copy Markdown

You are a savior !
Thanks.

@yxycman

yxycman commented Feb 25, 2021

Copy link
Copy Markdown

kudos!

@mendhak

mendhak commented Apr 19, 2021

Copy link
Copy Markdown
Author

Updated for latest Terraform syntax

@DaveOps83

Copy link
Copy Markdown

Thanks!

@woremo

woremo commented Jul 26, 2022

Copy link
Copy Markdown

Thanks!!

@manuelbent

Copy link
Copy Markdown

🚀

@flarocca

Copy link
Copy Markdown

Thank you so much!

@flarocca

Copy link
Copy Markdown

This code works perfectly fine, except wgen I test it via its public URL, in other words, when I use Postman.
It keeps returning an 500 - Internal Server Error.
Seems as though it is missing either an integration_response or a method_response (or both).
I've trying different approaches but nothing worked.

Do you have any clue? (My code is a copy-paste from this code)

@mendhak

mendhak commented Feb 23, 2023

Copy link
Copy Markdown
Author

I can't speak for Postman but does it work when you try with curl?

I ran the TF just now and was able to:

curl "https://xxxxxxxxxxxxxxxxxxxxxxxx.execute-api.eu-west-1.amazonaws.com/test/hello/world?a=1"

And I got an output from httpbin.

I did have to add some missing Terraform to the top regarding the provider (I'll edit the gist now):

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
}

provider "aws" {
  region = "eu-west-1"
}

@mendhak

mendhak commented Feb 23, 2023

Copy link
Copy Markdown
Author

I've added an output block to the gist too which shows what URL to invoke to test it

output "api_gateway_test_url" {
  value = "${aws_api_gateway_deployment.teststage.invoke_url}/hello/world"
}

@flarocca

Copy link
Copy Markdown

I just realized that my script was technically correct, but since I use as a deployment trigger the ids of resources and methods, it was not being deployed at all!!

Thank you too much for the help and support!

@nachomezzadra

Copy link
Copy Markdown

Thank you!

@luanjpb

luanjpb commented Nov 4, 2024

Copy link
Copy Markdown

@flarocca Thank you for your contribution. I spent nearly 3 days trying to deploy my function without realizing the problem.

@mingfang

Copy link
Copy Markdown

Is it possible to proxy the root, without any stages?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment