Skip to content

Instantly share code, notes, and snippets.

@JefStat
Created May 3, 2016 18:22
Show Gist options
  • Save JefStat/7db907e52566ce59c575881a7c7c3467 to your computer and use it in GitHub Desktop.
Save JefStat/7db907e52566ce59c575881a7c7c3467 to your computer and use it in GitHub Desktop.
Terraform api gateway bug sample
variable "function_hello" {}
resource "aws_api_gateway_rest_api" "Hello" {
name = "Hello World API"
description = "Terraform integration"
}
resource "aws_api_gateway_resource" "Hello" {
rest_api_id = "${aws_api_gateway_rest_api.Hello.id}"
parent_id = "${aws_api_gateway_rest_api.Hello.root_resource_id}"
path_part = "hello"
}
resource "aws_api_gateway_method" "HelloGet" {
rest_api_id = "${aws_api_gateway_rest_api.Hello.id}"
resource_id = "${aws_api_gateway_resource.Hello.id}"
http_method = "GET"
authorization = "NONE"
#TODO HTTP Request Headers cannot be added at this time
}
resource "aws_api_gateway_method_response" "200" {
rest_api_id = "${aws_api_gateway_rest_api.Hello.id}"
resource_id = "${aws_api_gateway_resource.Hello.id}"
http_method = "${aws_api_gateway_method.HelloGet.http_method}"
#TODO add header "Access-Control-Allow-Origin"
status_code = "200"
response_models = {
"application/json" = "Empty"
}
}
resource "aws_api_gateway_method_response" "500" {
rest_api_id = "${aws_api_gateway_rest_api.Hello.id}"
resource_id = "${aws_api_gateway_resource.Hello.id}"
http_method = "${aws_api_gateway_method.HelloGet.http_method}"
status_code = "500"
}
resource "aws_api_gateway_method_response" "400" {
rest_api_id = "${aws_api_gateway_rest_api.Hello.id}"
resource_id = "${aws_api_gateway_resource.Hello.id}"
http_method = "${aws_api_gateway_method.HelloGet.http_method}"
status_code = "400"
}
resource "aws_api_gateway_method_response" "401" {
rest_api_id = "${aws_api_gateway_rest_api.Hello.id}"
resource_id = "${aws_api_gateway_resource.Hello.id}"
http_method = "${aws_api_gateway_method.HelloGet.http_method}"
status_code = "401"
}
resource "aws_api_gateway_method_response" "403" {
rest_api_id = "${aws_api_gateway_rest_api.Hello.id}"
resource_id = "${aws_api_gateway_resource.Hello.id}"
http_method = "${aws_api_gateway_method.HelloGet.http_method}"
status_code = "403"
}
resource "aws_api_gateway_integration_response" "Hello_500" {
rest_api_id = "${aws_api_gateway_rest_api.Hello.id}"
resource_id = "${aws_api_gateway_resource.Hello.id}"
http_method = "${aws_api_gateway_method.HelloGet.http_method}"
status_code = "${aws_api_gateway_method_response.500.status_code}"
selection_pattern = "500.*"
}
resource "aws_api_gateway_integration_response" "Hello_400" {
rest_api_id = "${aws_api_gateway_rest_api.Hello.id}"
resource_id = "${aws_api_gateway_resource.Hello.id}"
http_method = "${aws_api_gateway_method.HelloGet.http_method}"
status_code = "${aws_api_gateway_method_response.400.status_code}"
selection_pattern = "400.*"
}
resource "aws_api_gateway_integration_response" "Hello_401" {
rest_api_id = "${aws_api_gateway_rest_api.Hello.id}"
resource_id = "${aws_api_gateway_resource.Hello.id}"
http_method = "${aws_api_gateway_method.HelloGet.http_method}"
status_code = "${aws_api_gateway_method_response.401.status_code}"
selection_pattern = "401.*"
}
resource "aws_api_gateway_integration_response" "Hello_403" {
rest_api_id = "${aws_api_gateway_rest_api.Hello.id}"
resource_id = "${aws_api_gateway_resource.Hello.id}"
http_method = "${aws_api_gateway_method.HelloGet.http_method}"
status_code = "${aws_api_gateway_method_response.403.status_code}"
selection_pattern = "403.*"
}
resource "aws_api_gateway_integration_response" "Hello_200" {
rest_api_id = "${aws_api_gateway_rest_api.Hello.id}"
resource_id = "${aws_api_gateway_resource.Hello.id}"
http_method = "${aws_api_gateway_method.HelloGet.http_method}"
status_code = "${aws_api_gateway_method_response.200.status_code}"
}
resource "aws_api_gateway_integration" "HelloGet" {
rest_api_id = "${aws_api_gateway_rest_api.Hello.id}"
resource_id = "${aws_api_gateway_resource.Hello.id}"
http_method = "${aws_api_gateway_method.HelloGet.http_method}"
type = "AWS"
integration_http_method = "POST" # Must be POST for invoking Lambda function
credentials = "${module.iam.api_gateway_role_arn}"
# http://docs.aws.amazon.com/apigateway/api-reference/resource/integration/#uri
uri = "arn:aws:apigateway:${var.aws_region}:lambda:path/2015-03-31/functions/${var.function_hello}/invocations"
# TODO uri to should be function name ${stageVariables.lambdaFunction}
# TODO request templates
request_templates = {
"application/json" = <<EOF
{
"authorization": "$input.params('Authorization')",
"body": $input.json('$')
}
EOF
}
}
resource "aws_api_gateway_deployment" "dev" {
depends_on = ["aws_api_gateway_integration.HelloGet"]
rest_api_id = "${aws_api_gateway_rest_api.Hello.id}"
stage_name = "dev"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment