55

In AWS API Gateway, I have a endpoint defined as /users/{userId}/someAction, and I'm trying to recreate this with terraform

I would start having some sort of linked gateway_resource chain like so...

resource "aws_api_gateway_resource" "Users" {
  rest_api_id = "${var.rest_api_id}" 
  parent_id = "${var.parent_id}" 
  path_part = "users"
}

//{userId} here?

resource "aws_api_gateway_resource" "SomeAction" {
  rest_api_id = "${var.rest_api_id}" 
  parent_id = "${aws_api_gateway_resource.UserIdReference.id}"
  path_part = "someAction"
}

In which I then define the aws_api_gateway_method and everything else.

How do I define this endpoint in terraform? The terraform documentation and examples don't cover this use case.

GabLeRoux
  • 16,715
  • 16
  • 63
  • 81
iquestionshard
  • 956
  • 1
  • 7
  • 17
  • adding to the answer, you can change the parent_id to point to the `aws_api_gateway_resource` that has the dynamic param. – naren Oct 02 '17 at 14:46

1 Answers1

82

You need to define a resource whose path_part is the parameter you want to use:

// List
resource "aws_api_gateway_resource" "accounts" {
    rest_api_id = var.gateway_id
    parent_id   = aws_api_gateway_resource.finance.id
    path_part   = "accounts"
}

// Unit
resource "aws_api_gateway_resource" "account" {
  rest_api_id = var.gateway_id
  parent_id   = aws_api_gateway_resource.accounts.id
  path_part   = "{accountId}"
}

Then you create the method and enable the path parameter:

resource "aws_api_gateway_method" "get-account" {
  rest_api_id   = var.gateway_id
  resource_id   = var.resource_id
  http_method   = "GET"
  authorization = "NONE"

  request_parameters = {
    "method.request.path.accountId" = true
  }
}

And finally you can successfully create the mapping within the integration:

resource "aws_api_gateway_integration" "get-account-integration" {
    rest_api_id             = var.gateway_id
    resource_id             = var.resource_id
    http_method             = aws_api_gateway_method.get-account.http_method
    type                    = "HTTP"
    integration_http_method = "GET"
    uri                     = "/integration/accounts/{id}"
    passthrough_behavior    = "WHEN_NO_MATCH"

    request_parameters = {
        "integration.request.path.id" = "method.request.path.accountId"
    }
}

The method needs to be there - and with the parameter enabled - in order for the integration mapping to work.

donnchadh
  • 63
  • 1
  • 6
pesama
  • 993
  • 8
  • 8
  • 1
    Isn't this only for the `users/{userId}` portion of the OP's question? Currently facing same issue as the original question: managed to get `resource/{resourceId}` working, but not `resource/{resourceId}/someAction`. Tried creating another resource under the `{pathId}` resource, with it's own method and integration, but I get a "Missing Authentication Token" error – which leads me to suspect it didn't really create anything. – cintron Jul 04 '17 at 06:00
  • Where in the docs did you find this solution? Was it just diving into AWS docs? – Ryan Smith Apr 21 '18 at 16:24
  • 1
    Equals operator `=` required for the assignment to `request_parameters`in method and integration, as per docs e.g. https://www.terraform.io/docs/providers/aws/r/api_gateway_integration.html#request_parameters – Tom Bunting Dec 24 '19 at 10:05
  • I see that `aws_api_gateway_resource.account` is defined, but it doesn't appear to be referenced anywhere. Is this an oversight? I would have expected it might be used in `aws_api_gateway_method.get-account`. Perhaps in its `resource_id` field? – Daniel Aug 14 '20 at 17:44