2016-08-19 76 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