Abhishek Sharma
Abhishek Sharma

Reputation: 193

Terraform Include parent uri with dynamic parameter in child uri for AWS API Gateway

I want to create a endpoint like user/<user_id>/info through aws_api_gateway using terraform

I am able to create endpoint till /user/<user_id>

But how to include <user_id> in the resource for "info"

Here is the code for "info" part:

resource "aws_api_gateway_resource" "info" {
  rest_api_id = aws_api_gateway_rest_api.app.id
  parent_id   = aws_api_gateway_resource.user.id
  path_part   = "info"
}

resource "aws_api_gateway_method" "info" {
  rest_api_id   = aws_api_gateway_rest_api.app.id
  resource_id   = aws_api_gateway_resource.info.id
  http_method   = "GET"
  authorization = "NONE"
}

resource "aws_api_gateway_integration" "info" {
  rest_api_id          = aws_api_gateway_rest_api.app.id
  resource_id          = aws_api_gateway_resource.info.id
  http_method          = aws_api_gateway_method.info.http_method
  type                 = "HTTP_PROXY"
  uri                  = "http://root_api/user/<user_id>/info"

When I try this, the url for info resolves to http://root_api/user/<user_id>/info. Any help on how to inject <user_id> in this url ??

Thanks

Upvotes: 1

Views: 1180

Answers (3)

Rahul Manas
Rahul Manas

Reputation: 95

I have API in the YAML file. The routes are

/events/{filename}:
/historical-data/{ticker}
/events

events API are working fine. But path_part for /events/{filename}, /historical-data/{ticker} are throwing error.

Says invalid character. How can I handle it?

The current implementation looks like:

resource "aws_api_gateway_resource" "resource" {
  for_each = { for path, details in yamldecode(file("./open-spec.yaml")).paths : path => details }

  rest_api_id = aws_api_gateway_rest_api.explore_rest.id
  parent_id   = aws_api_gateway_resource.root.id
  path_part = replace(each.key, "/[^a-zA-Z0-9._-]+/", "")
}

Please suggest.

Upvotes: 0

Abhishek Sharma
Abhishek Sharma

Reputation: 193

Figured Out the answer. This makes you accept any dynamic parameter into your integation uri no matter from where they appear in the ancestor chain

resource "aws_api_gateway_resource" "info" {
  rest_api_id = aws_api_gateway_rest_api.app.id
  parent_id   = aws_api_gateway_resource.user.id
  path_part   = "info"
}

resource "aws_api_gateway_method" "info" {
  rest_api_id   = aws_api_gateway_rest_api.app.id
  resource_id   = aws_api_gateway_resource.info.id
  http_method   = "GET"
  authorization = "NONE"
  request_parameters   = 
  {
   "method.request.path.user_id" = true
  }
}

resource "aws_api_gateway_integration" "info" {
  rest_api_id          = aws_api_gateway_rest_api.app.id
  resource_id          = aws_api_gateway_resource.info.id
  http_method          = aws_api_gateway_method.info.http_method
  type                 = "HTTP_PROXY"
  uri                  = "http://root_api/user/<user_id>/info"
  request_parameters   = 
  {
   "integration.request.path.user_id" = "method.request.path.user_id"
  }
}

Upvotes: 1

Kombajn zbożowy
Kombajn zbożowy

Reputation: 10693

Terraform interpolates variables in strings: documentation.

In your case:

uri = "http://root_api/user/${aws_api_gateway_resource.user.id}/info"

Upvotes: 2

Related Questions