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

时间:2016-08-19 13:49:52

标签: amazon-web-services aws-api-gateway terraform

在AWS API Gateway中,我将端点定义为D:\my_workspace\my_project\my_bundle\node_modules\bufferutil>if not defined npm_config_node_gyp (node "C:\Program Files\nodejs\node_modules\npm\bin\node-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.js" rebuild ) else (node "" rebuild ) Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch. C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.CppCommon.targets(356,5): error MSB6006: "CL.exe" exited with code -1073741515. [D:\my_workspace\my_project\my_bundle\node_modules\bufferutil\build\bufferutil.vcxproj] gyp ERR! build error gyp ERR! stack Error: `C:\Program Files (x86)\MSBuild\14.0\bin\msbuild.exe` failed with exit code: 1 gyp ERR! stack at ChildProcess.onExit (C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\build.js:276:23) gyp ERR! stack at emitTwo (events.js:87:13) gyp ERR! stack at ChildProcess.emit (events.js:172:7) gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:200:12) gyp ERR! System Windows_NT 6.3.9600 gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" gyp ERR! cwd D:\my_workspace\my_project\my_bundle\node_modules\bufferutil gyp ERR! node -v v4.5.0 gyp ERR! node-gyp -v v3.4.0 gyp ERR! not ok gyp ERR! Windows_NT 6.3.9600 gyp ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "--msvs_version=2015" gyp ERR! node v4.5.0 gyp ERR! npm v2.15.9 gyp ERR! code ELIFECYCLE gyp ERR! bufferutil@1.2.1 install: `node-gyp rebuild` gyp ERR! Exit status 1 gyp ERR! gyp ERR! Failed at the bufferutil@1.2.1 install script 'node-gyp rebuild'. gyp ERR! This is most likely a problem with the bufferutil package, gyp ERR! not with npm itself. gyp ERR! Tell the author that this fails on your system: gyp ERR! node-gyp rebuild gyp ERR! You can get information on how to open an issue for this project with: gyp ERR! npm bugs bufferutil gyp ERR! Or if that isn't available, you can get their info via: gyp ERR! gyp ERR! npm owner ls bufferutil gyp ERR! There is likely additional logging output above. gyp ERR! Please include the following file with any support request: gyp ERR! D:\my_workspace\my_project\my_bundle\npm-debug.log ,我尝试使用terraform重新创建此端点

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

/users/{userId}/someAction

然后我在其中定义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" } 和其他所有内容。

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

1 个答案:

答案 0 :(得分:30)

您需要定义resource,其中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}"
}

然后您创建method启用路径参数:

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

最后,您可以在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"
    }
}

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