Terraform: AWS API Gateway not possible to set HTTP status method response as Proxy

Created on 16 Nov 2016  路  14Comments  路  Source: hashicorp/terraform

Hi there,

I麓m trying to create a method for a resource which uses a AWS Lambda function as a proxy but is not created like via AWS website.

I haven麓t found any way to do this. I know this a new feature in AWS API Gateway and that is the reason why is not supported yet in Terraform.

Terraform Version

Terraform v0.7.10

Affected Resource(s)

  • aws_api_gateway_integration

Terraform Configuration Files

##############################
# Variables
variable "access_key" {}
variable "secret_key" {}
variable "account_id" {}
variable "region" {}
variable "rest-api-name" {}
variable "rest-api-description" {}
variable "rest-api-resource-name" {}
variable "aws-lambda-function-name" {}

##############################
# Providers
provider "aws" {
    access_key = "${var.access_key}"
    secret_key = "${var.secret_key}"
    region = "${var.region}"
}

##############################
# Create API
resource "aws_api_gateway_rest_api" "rest-api" {
  name = "${var.rest-api-name}"
  description = "${var.rest-api-description}"
}

##############################
# Create parent resource 
resource "aws_api_gateway_resource" "parent-resource" {
  rest_api_id = "${aws_api_gateway_rest_api.rest-api.id}"
  parent_id = "${aws_api_gateway_rest_api.rest-api.root_resource_id}"
  path_part = "${var.rest-api-resource-name}"
}

##############################
# /*/GET/parent-resource
resource "aws_api_gateway_method" "parent-resource-method-get" {
  rest_api_id = "${aws_api_gateway_rest_api.rest-api.id}"
  resource_id = "${aws_api_gateway_resource.parent-resource.id}"
  http_method = "GET"
  authorization = "NONE"
}

resource "aws_api_gateway_integration" "parent-resource-integration-method-get" {
  rest_api_id = "${aws_api_gateway_rest_api.rest-api.id}"
  resource_id = "${aws_api_gateway_resource.parent-resource.id}"
  http_method = "${aws_api_gateway_method.parent-resource-method-get.http_method}"
  type = "AWS_PROXY"
  uri = "arn:aws:apigateway:${var.region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${var.region}:${var.account_id}:function:${var.aws-lambda-function-name}/invocations"
  integration_http_method = "POST"
}

resource "aws_lambda_permission" "allow-api-gateway-parent-resource-get" {
    function_name = "${var.aws-lambda-function-name}"
    statement_id = "allow-api-gateway-parent-resource-get"
    action = "lambda:InvokeFunction"
    principal = "apigateway.amazonaws.com"
    source_arn = "arn:aws:execute-api:${var.region}:${var.account_id}:${aws_api_gateway_rest_api.rest-api.id}/*/${aws_api_gateway_method.parent-resource-method-get.http_method}${aws_api_gateway_resource.parent-resource.path}"
}

Expected Behavior

The same than create this method via AWS website where the _Method Response HTTP status_ is set as Proxy, like:

mainviewexpected

Actual Behavior

The _Method Response HTTP status_ is not set:

mainview

Steps to Reproduce

  1. terraform apply -var 'access_key=XXXX' -var 'secret_key=XXXX' -var 'account_id=XXXX' -var 'region=XXXX' -var 'rest-api-name=XXXX' -var 'rest-api-description=XXXX' -var 'rest-api-resource-name=XXXX' -var 'aws-lambda-function-name=XXXX'
bug provideaws

Most helpful comment

oh man this needs to be documented... i was experiencing same issue... will try this method to see if it works

edit: works great!

All 14 comments

Hi, i think you need to add aws_api_gateway_method_response and aws_api_gateway_integration_response resources to your configuration. But i do not know that status_code can be set "PROXY".

So your config file will look something like this:

resource "aws_api_gateway_method_response" "200" {
    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}"
    status_code = "200"

    response_models = {
         "application/json" = "Empty"
    }
}

resource "aws_api_gateway_integration_response" "MyDemoIntegrationResponse" {
   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}"
   status_code = "${aws_api_gateway_method_response.200.status_code}"

   response_templates = {
       "application/json" = ""
   } 
}

Hi @pasali

Yes, I tried to aws_api_gateway_method_response and aws_api_gateway_integration_response but that add as _Method Response HTTP status_ a 200 instead _Proxy_.

This _Proxy_ value is set magically (any specific action done) when is created via AWS website.

@BorjaLopezAltarriba, i don't know if it will work but you can change status_code = "200" to status_code = "PROXY" and see what will happen. Btw i also use my API Gateway to act like proxy to my lambda function and i use above configuration to achieve it.

@pasali I tried but status_code field at aws_api_gateway_method_response must be a number.

If you say this works to you, I will try it again. Thank you in advance!

@BorjaLopezAltarriba it doesn't work for me too.

@pasali yes, it doesn't work.

Hi @pasali

I tried again what you suggested and now it works! As you said using:

resource "aws_api_gateway_method_response" "200" {
    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}"
    status_code = "200"

    response_models = {
         "application/json" = "Empty"
    }
}

resource "aws_api_gateway_integration_response" "MyDemoIntegrationResponse" {
   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}"
   status_code = "${aws_api_gateway_method_response.200.status_code}"

   response_templates = {
       "application/json" = ""
   } 
}

I get what I expected:

apigateway proxy

I haven't upgrade Terraform version, I'm still using the same (v0.7.10).

Thank you for your help @pasali

You're welcome @blaltarriba. Nice to see you get things work.

oh man this needs to be documented... i was experiencing same issue... will try this method to see if it works

edit: works great!

This worked for me as well in #10494. I've renamed that to indicate that it's a documentation issue.

I spent so long trying to figure this out before finding this solution. thank you @pasali 馃檹

@wkentdag Out of interest, would you mind confirming my comment here... that the documentation for this isn't yet sufficient? If so I'll see about opening a new pull request (although I haven't touched this for some time so I forget the exact implementation!)

The API Gateway - Lambda example in the docs doesn't work out of the box so agreed, it could use some fleshing out @tdmalone

Hi all,

Any new issues with the terraform aws provider should be opened in the aws provider repository.

Because this closed issue is generating notifications for subscribers, I am going to lock it and encourage anyone experiencing issues with the aws provider to open tickets there.

Please continue to open issues here for any other terraform issues you encounter, and thanks!

Was this page helpful?
0 / 5 - 0 ratings