用于列出所有 api 网关部署的 boto 脚本

boto script to list all api gateway deployments

我使用网关部署了多个 API。如何以表格格式列出所有这些以及集成类型(如 lambda)和方法响应(如 200)等详细信息?


更新: 正如答案中所建议的,我可以使用 "get-rest-apis" 方法来获取所有 API ID 的列表。 json 数据可以像这样转换为 pandas 数据帧...

# aws apigateway get-rest-apis --region=us-east-1 > /tmp/to_file.json

import pandas as pd
import json
from pandas.io.json import json_normalize

with open("to_file.json") as f:
    data = json.load(f)

df = json_normalize(data, "items")

df["createdDate"] = pd.to_datetime(df["createdDate"], unit="s").dt.date
df = df.sort_values(["createdDate"])

df["endpointConfiguration.types"] = df["endpointConfiguration.types"].str[0]

但是如何查询得到每个ID的详细信息呢?


为了获得给定 API 的完整图片,我需要查询几种方法,例如 get-integration、get-method-response、get-resource。每一个都有不同数量的必需参数,这使得自动化过程非常困难。

Boto 是 Python 的亚马逊网络服务 (AWS) SDK。可以通过使用 Python 输出技术以及不同的 Python 数据结构来以表格格式显示输出。

在以下链接中,您可以找到 API 参考资料,您可以将这些参考资料用于您的 API 网关部署,以获取您使用 API 所需的所有信息,例如 get_deployments()get_integration()get_integration_response()、等等:

  1. Boto3 API Gateway APIs
  2. Boto3 API Gateway v2 APIs

如果您想使用 AWS CLI,您可以在相应的 CLI 命令中使用 --output table 参数。例如:aws apigateway get-deployments --rest-api-id 1234123412 --output table

您可以在此处找到 AWS API 网关服务的 AWS CLI 命令参考:AWS API Gateway CLI Reference

虽然它可能无法满足您的所有要求(或者它可能正是您所需要的),但实现您想要的最简单和最标准化的方法是将您的 REST API 导出到 OpenAPI 格式(以前的 "Swagger" format). Support for version 3.0 was recently added 到 API 网关。您可以 augment/extend 输出 JSON 以及您需要的其他信息。

官方文档:https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-export-api.html