在Terraform中,如何在请求路径中使用变量指定API网关端点?

iqu*_*ard 25 amazon-web-services terraform aws-api-gateway

在AWS API Gateway中,我将端点定义为/users/{userId}/someAction,并且我尝试使用terraform重新创建此端点

我会开始像某样链接的gateway_resource链......

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"
}
Run Code Online (Sandbox Code Playgroud)

然后我在其中定义了aws_api_gateway_method其他所有内容.

如何在terraform中定义此端点?terraform文档和示例不包括此用例.

pes*_*ama 39

您需要定义一个资源,该资源path_part是您要使用的参数:

// 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}"
}
Run Code Online (Sandbox Code Playgroud)

然后创建方法启用 path参数:

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
  }
}
Run Code Online (Sandbox Code Playgroud)

最后,您可以在集成中成功创建映射:

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"
    }
}
Run Code Online (Sandbox Code Playgroud)

该方法需要在那里 - 并且启用参数 - 以使集成映射起作用.