在 Terraform 中,如何在请求路径中使用变量指定 API 网关端点? [英] In Terraform, how do you specify an API Gateway endpoint with a variable in the request path?

查看:20
本文介绍了在 Terraform 中,如何在请求路径中使用变量指定 API 网关端点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 AWS API Gateway 中,我有一个定义为 /users/{userId}/someAction 的端点,我正在尝试使用 terraform 重新创建它

In AWS API Gateway, I have a endpoint defined as /users/{userId}/someAction, and I'm trying to recreate this with terraform

我会开始有某种链接的 gateway_resource 链,像这样......

I would start having some sort of linked gateway_resource chain like so...

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 和其他所有内容.

In which I then define the aws_api_gateway_method and everything else.

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

How do I define this endpoint in terraform? The terraform documentation and examples don't cover this use case.

推荐答案

你需要定义一个 resource 其中 path_part 是您要使用的参数:

You need to define a resource whose path_part is the parameter you want to use:

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

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

Then you create the method and enable the path parameter:

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

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

The method needs to be there - and with the parameter enabled - in order for the integration mapping to work.

这篇关于在 Terraform 中,如何在请求路径中使用变量指定 API 网关端点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆