Skip to content

Instantly share code, notes, and snippets.

@mendhak
Last active April 24, 2024 10:52
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mendhak/8303d60cbfe8c9bf1905def3ccdb2176 to your computer and use it in GitHub Desktop.
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

Exactly what I needed, thank you!

@sonoman
Copy link

sonoman commented Mar 17, 2020

Access Permissions are missing...."aws_lambda_permission"

@UtsavChokshiCNU
Copy link

You are a savior !
Thanks.

@yxycman
Copy link

yxycman commented Feb 25, 2021

kudos!

@mendhak
Copy link
Author

mendhak commented Apr 19, 2021

Updated for latest Terraform syntax

@DaveOps83
Copy link

Thanks!

@woremo
Copy link

woremo commented Jul 26, 2022

Thanks!!

@manuelbent
Copy link

🚀

@flarocca
Copy link

Thank you so much!

@flarocca
Copy link

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
Copy link
Author

mendhak commented Feb 23, 2023

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
Copy link
Author

mendhak commented Feb 23, 2023

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

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

Thank you!

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