In Terraform, how do you specify an API Gateway endpoint with a variable in the request path?

后端 未结 1 1640
滥情空心
滥情空心 2020-12-25 11:11

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 s

相关标签:
1条回答
  • 2020-12-25 11:37

    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.

    0 讨论(0)
提交回复
热议问题