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

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

在 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 文档和示例未涵盖此用例。

最佳答案

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

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

关于amazon-web-services - 在 Terraform 中,如何使用请求路径中的变量指定 API 网关端点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39040739/

相关文章:

dictionary - Terraform 多层 map

amazon-web-services - 具有通配符条件的 AWS S3 RoutingRule

python - 如何使用 boto3 部署 AWS 无服务器应用程序模型模板?

amazon-web-services - 使用 IAM 角色而不是凭证从使用 terraform 的 EC2 实例创建 aws 资源

wordpress - 有什么方法可以将 AWS Cloudfront 设置为指向静态 IP 地址(托管在 GoDaddy 上的 WP)?

amazon-web-services - 如何使用 Terraform 和 AWS API Gateway 创建 API 代理

amazon-web-services - AWS-使用 CFT 将 Lambda 与 API Gateway 集成

amazon-web-services - 无法在 AWS API Gateway 中创建阶段

azure - 如何引用 terraform 中的对象

terraform - 在 terraform 控制台中获取局部变量