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

Time:02-12

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

CodePudding user response:

Terraform interpolates variables in strings: documentation.

In your case:

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

CodePudding user response:

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