2016-08-19 81 views
11

在AWS API網關,我有一個端點定義爲「/用戶/ {} userId的/ someAction」,我試圖用terraform重新創建此在Terraform中,如何在請求路徑中指定具有變量的API網關端點?

我就開始有某種聯繫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" 
} 

然後在其中我定義了aws_api_gateway_method和其他一切。

如何在terraform中定義此端點?該terraform文檔和示例不包括此用例。

回答

14

您需要定義一種資源,其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}" 
} 

然後創建方法和使路徑參數:

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

最後你可成功創建集成內的映射:

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

該方法需要在那裏 - 並且啓用參數 - 以便集成映射工作。

+1

很好。謝謝!! – knaak

+1

這不僅適用於OP問題的'users/{userId}'部分嗎? 目前面臨與原始問題相同的問題:設法讓'resource/{resourceId}工作,但不是'resource/{resourceId}/someAction'。 嘗試在'{pathId}'資源下創建另一個資源,並使用它自己的方法和集成,但我得到一個「Missing Authentication Token」錯誤 - 這讓我懷疑它並沒有真正創建任何東西。 – yoaquim

0

我無法評論,因爲名譽較低,但添加到上面的答案中,您可以更改parent_id以指向具有動態參數的aws_api_gateway_resource