【问题标题】:In Terraform, how do you specify an API Gateway endpoint with a variable in the request path?在 Terraform 中,如何在请求路径中使用变量指定 API 网关端点?
【发布时间】:2016-12-26 17:09:37
【问题描述】:

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

然后我在其中定义 aws_api_gateway_method 和其他所有内容。

如何在 terraform 中定义此端点? terraform 文档和示例未涵盖此用例。

【问题讨论】:

  • 添加到答案中,您可以将 parent_id 更改为指向具有动态参数的aws_api_gateway_resource

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


【解决方案1】:

您需要定义一个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"
    }
}

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

【讨论】:

  • 这不只是针对 OP 问题的 users/{userId} 部分吗?目前面临与原始问题相同的问题:设法让resource/{resourceId} 工作,但不是resource/{resourceId}/someAction。尝试使用自己的方法和集成在 {pathId} 资源下创建另一个资源,但我收到“缺少身份验证令牌”错误 - 这让我怀疑它并没有真正创建任何东西。
  • 您在文档中的什么地方找到了这个解决方案?只是深入研究 AWS 文档吗?
  • Equals operator = 需要分配给request_parametersin 方法和集成,根据文档,例如terraform.io/docs/providers/aws/r/…
  • 我看到 aws_api_gateway_resource.account 已定义,但似乎没有在任何地方引用。这是疏忽吗?我原以为它可能会在aws_api_gateway_method.get-account 中使用。也许在其resource_id 字段中?
猜你喜欢
  • 2021-07-14
  • 2018-07-24
  • 1970-01-01
  • 2023-01-04
  • 2020-12-12
  • 2016-02-11
  • 1970-01-01
  • 2018-07-24
  • 1970-01-01
相关资源
最近更新 更多