【问题标题】:AWS API Gateway - How to add method to root resource via CLI or GO aws-sdk?AWS API Gateway - 如何通过 CLI 或 GO aws-sdk 向根资源添加方法?
【发布时间】:2017-02-14 14:47:41
【问题描述】:

AWS Console for API Gateway 上,可以将新的method 添加到api/ (/),而无需先添加新资源。

但我不知道如何使用 aws cliaws-sdk(用于 GO)来做到这一点。

这可能吗?

PS:最终我想通过terraform 来做这件事。

【问题讨论】:

  • 我不确定我是否听懂了你的意思。当您在 API 网关上创建新方法时,您同时将其链接到资源(即使在控制台中),因为该方法实际上是资源的一部分(定义方法位于 AWS 控制台中的资源下)。因此,使用 Terraform,在创建新方法时,您必须提供该方法所针对的资源。

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


【解决方案1】:

您可以按照以下步骤使用 aws-cli 来实现它。

您可以通过aws apigateway put-method 创建一个新方法,其中需要以下参数:--rest-api-id、--resource-id、--http-method、--authorization-type

首先,从 json-output 中找到您的 --rest-api-id,将方法添加到根资源。

aws apigateway get-rest-apis

--resource-id 是这里的关键部分。 rest-api 的 root-resource-id 可以通过以下命令获取,该命令返回一个 JSON 对象。

aws apigateway get-resources --rest-api-id <restApiId> | jq -r  '.items[0].id'

jq 是一个快速、轻量级、灵活的 CLI JSON 处理器。访问此处了解更多“http://blog.librato.com/posts/jq-json

替换值并从 CLI 调用函数。

例子:

aws apigateway put-method --rest-api-id hgwwri3w30 --http-method POST --authorization-type NONE --resource-id `aws apigateway get-resources --rest-api-id hgwwri3w30 | jq -r  '.items[0].id'`

成功后你会得到这样的回应,

{
    "httpMethod": "POST",
    "authorizationType": "NONE",
    "apiKeyRequired": false
}

您现在可以从控制台设置方法,也可以在函数调用期间使用其他参数设置方法。

aws apigateway put-method help

【讨论】:

    【解决方案2】:

    好的,我想我至少找到了terraform 的答案。

    要获取我们想要添加方法的root resource,我们可以使用

    aws_api_gateway_rest_api.ApiName.root_resource_id

    【讨论】:

      【解决方案3】:

      很高兴您在 terraform 中找到了这样做的答案。

      如果您想在 aws-cli 中执行此操作,请使用 put-method

      【讨论】:

        【解决方案4】:

        获取根路径的 id 的更好方法可能是:

        aws apigateway get-resources --rest-api-id <restApiId> --query 'items[?path==`/`]' --output text
        

        然后您可以将该 id 写入文件 &gt; output.txt 并将其加载到 kishor 提到的 put 方法中。

        即使使用 Terraform,aws_api_gateway_rest_api 的数据源的 root_resource_id 部分有时也会出现问题。

        【讨论】:

          猜你喜欢
          • 2021-05-29
          • 1970-01-01
          • 1970-01-01
          • 2020-08-21
          • 2022-11-30
          • 2019-06-09
          • 1970-01-01
          • 1970-01-01
          • 2019-10-20
          相关资源
          最近更新 更多