Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform: Create url path parameter for AWS API Gateway that invokes Lambda?

I'm writing Terraform to deploy an AWS API Gateway with AWS Lambda integration. I would like to specify an optional path parameter in the url that I can reference from the lambda. I can't figure out how to specify this in the AWS API Gateway terraform.

The only information I can find for path variables is this SO post: In Terraform, how do you specify an API Gateway endpoint with a variable in the request path?

In it, the answer specifies the path variable in the uri field of the aws_api_gateway_integration function:

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"
    }
}

Unfortunately, with AWS Lambda integration uses that uri field for the ARN of the lambda. Here is how I reference the lambda in my integration:

resource "aws_api_gateway_integration" "books_lambda" {
  rest_api_id             = "${var.gateway.id}"
  resource_id             = "${var.resource_id}"
  http_method             = "${aws_api_gateway_method.books.http_method}"
  type                    = "AWS_PROXY"
  integration_http_method = "POST"
  uri                     = "${var.books_invoke_arn}" # <--
  credentials             = "${aws_iam_role.books_gateway.arn}"

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

Because the arn is in the place of the uri field, I don't know where to define the placement of the path parameter.

I've tried appending the path variable to the uri field (${var.books_invoke_arn}/{bookId}), but it just creates an error. Where can I specify the path variable when the uri field is occupied by the lambda arn?

Second, is it possible to make that variable optional, or would I have to have a second set of terraform (one with the variable, one without)?

Thanks!

like image 429
user2233394 Avatar asked Jun 13 '19 21:06

user2233394


People also ask

Can API gateway trigger Lambda?

that can call to an HTTP or Websocket endpoint sends that request. these requests are intercepted by API gateway which will take care of managing the traffic of the request. It will send the request to the right service like AWS Lambda, Dynamo DB, EC2 etc. It can be used in mobile or web back ends.


1 Answers

This is an old question, but it came up in one of my searches. I think that things may have changed in the interim but a way to do this currently is by specifying your variable in curly braces in the route_key as in this example from our code:

resource "aws_apigatewayv2_route" "room-recommendations-ng" {
  api_id             = aws_apigatewayv2_api.this.id
  route_key          = "GET /rooms/{room}/recommendations"
  target             = "integrations/${aws_apigatewayv2_integration.room-recommendations.id}"
}
like image 123
Cargo23 Avatar answered Sep 27 '22 20:09

Cargo23