Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AWS API Gateway not possible to set HTTP status method response as Proxy #10157

Closed
blaltarriba opened this issue Nov 16, 2016 · 14 comments
Closed

AWS API Gateway not possible to set HTTP status method response as Proxy #10157

blaltarriba opened this issue Nov 16, 2016 · 14 comments
Labels

Comments

@blaltarriba
Copy link

@blaltarriba blaltarriba commented Nov 16, 2016

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'
@pasali
Copy link

@pasali pasali commented Nov 16, 2016

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" = ""
   } 
}
@blaltarriba
Copy link
Author

@blaltarriba blaltarriba commented Nov 16, 2016

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.

@pasali
Copy link

@pasali pasali commented Nov 16, 2016

@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.

@blaltarriba
Copy link
Author

@blaltarriba blaltarriba commented Nov 16, 2016

@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!

@pasali
Copy link

@pasali pasali commented Nov 16, 2016

@BorjaLopezAltarriba it doesn't work for me too.

@blaltarriba
Copy link
Author

@blaltarriba blaltarriba commented Nov 17, 2016

@pasali yes, it doesn't work.

@blaltarriba
Copy link
Author

@blaltarriba blaltarriba commented Nov 29, 2016

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

@pasali
Copy link

@pasali pasali commented Nov 30, 2016

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

@highbass
Copy link

@highbass highbass commented Dec 1, 2016

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

edit: works great!

@FransUrbo
Copy link

@FransUrbo FransUrbo commented Dec 5, 2016

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

@wkentdag
Copy link
Member

@wkentdag wkentdag commented Aug 2, 2018

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

@tdmalone
Copy link

@tdmalone tdmalone commented Aug 2, 2018

@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!)

@wkentdag
Copy link
Member

@wkentdag wkentdag commented Aug 3, 2018

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

@mildwonkey
Copy link
Member

@mildwonkey mildwonkey commented Aug 3, 2018

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!

@hashicorp hashicorp locked and limited conversation to collaborators Aug 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
8 participants
You can’t perform that action at this time.