aws apigateway get-method 详解:用 AWS CLI 查看 API Gateway 方法资源配置
2026/9/14 2:03:16 网站建设 项目流程

aws apigateway get-method 详解:用 AWS CLI 查看 API Gateway 方法资源配置

【免费下载链接】aws-cliUniversal Command Line Interface for Amazon Web Services项目地址: https://gitcode.com/GitHub_Trending/aw/aws-cli

导读

aws apigateway get-method是 AWS CLI 中用于查询 REST API 下某个资源(Resource)上已定义 HTTP 方法(Method)完整配置的核心命令。本文基于 awscli/examples/apigateway/get-method.rst 中的官方示例,结合仓库内 apigateway 服务模型 的源码级定义,完整讲解该命令的参数用法、返回字段含义、关联命令链路与常见排错方法。读完本文,你将能够熟练使用get-method快速审查 API 的鉴权方式、API Key 要求、请求参数声明、后端集成(Integration)与响应(MethodResponse)配置,并理解其与put-methodput-integration等命令的协作关系。


一、命令概览:get-method 解决什么问题

在 API Gateway 中,一个 REST API 由三级结构构成:

  1. RestApi(API 实例)——由create-rest-api创建,用rest-api-id唯一标识;
  2. Resource(资源路径,如/pets)——挂在 API 根资源下,由create-resource创建,用resource-id唯一标识;
  3. Method(HTTP 方法,如GETPOSTPUT)——定义在某个 Resource 之上,通过put-method创建。

get-method的作用正是读取第三级"方法"的完整配置快照:包括鉴权类型、是否要求 API Key、请求参数声明、请求模型、方法级响应(MethodResponse)以及后端集成(Integration)等。

从仓库中的服务模型定义可以看到该命令对应的底层 REST 调用:

"GetMethod":{ "name":"GetMethod", "http":{ "method":"GET", "requestUri":"/restapis/{restapi_id}/resources/{resource_id}/methods/{http_method}" }, ... "documentation":"<p>Describe an existing Method resource.</p>" }

(来源:service-2.json 第 1117-1131 行)

也就是说,aws apigateway get-method最终发出的是对GET /restapis/{restapi_id}/resources/{resource_id}/methods/{http_method}的 HTTP 请求,三个位置参数都会映射到 URI 路径中,而非请求体。


二、命令语法与参数说明

2.1 标准命令

官方示例(get-method.rst)给出的命令形式为:

aws apigateway get-method --rest-api-id 1234123412 --resource-id y9h6rt --http-method GET

该命令有三个必选参数,全部以--前缀的 CLI 参数形式传入:

参数类型必填说明
--rest-api-idString目标 RestApi 的字符串标识符,即创建 API 时返回的id
--resource-idString目标 Resource 的标识符,即该方法所属资源的id
--http-methodString方法请求的 HTTP 方法类型,如GETPOSTPUTDELETEPATCHOPTIONSHEADANY

在服务模型中,这三个参数的形状定义(shapeGetMethodRequest)明确标记了它们全部为必填,且都带"location":"uri"属性,与上述 URI 模板一一对应(service-2.json 第 4704-4732 行):

"GetMethodRequest":{ "type":"structure", "required":["restApiId","resourceId","httpMethod"], "members":{ "restApiId": { "shape":"String", "location":"uri", "locationName":"restapi_id" }, "resourceId": { "shape":"String", "location":"uri", "locationName":"resource_id" }, "httpMethod": { "shape":"String", "location":"uri", "locationName":"http_method" } }, "documentation":"<p>Request to describe an existing Method resource.</p>" }

2.2 如何获取 rest-api-id 和 resource-id

这两个 ID 需要从上游命令的返回结果中获得:

  • aws apigateway get-rest-apis列出所有 API,从返回的items[].id字段拿到rest-api-id(示例见 get-rest-apis.rst):
aws apigateway get-rest-apis
{ "items": [ { "createdDate": 1438884790, "id": "12s44z21rb", "name": "My First API" } ] }
  • aws apigateway get-resources --rest-api-id <rest-api-id>列出该 API 下的所有资源,从items[].id拿到resource-id(示例见 get-resources.rst):
aws apigateway get-resources --rest-api-id 1234123412
{ "items": [ { "path": "/resource/subresource", "resourceMethods": { "POST": {} }, "id": "024ace", "pathPart": "subresource", "parentId": "ai5b02" } ] }

注意get-resources返回的resourceMethods是一个键为 HTTP 方法名、值为空对象的 Map——它只提示"该资源上定义了哪些方法",而不含方法的详细配置;方法的详细配置正是get-method要补齐的信息。


三、返回结果逐字段解析

官方示例的完整返回如下(原样继承自 get-method.rst):

{ "apiKeyRequired": false, "httpMethod": "GET", "methodIntegration": { "integrationResponses": { "200": { "responseTemplates": { "application/json": null }, "statusCode": "200" } }, "cacheKeyParameters": [], "uri": "arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:123412341234:function:My_Function/invocations", "httpMethod": "POST", "cacheNamespace": "y9h6rt", "type": "AWS" }, "requestParameters": {}, "methodResponses": { "200": { "responseModels": { "application/json": "Empty" }, "statusCode": "200" } }, "authorizationType": "NONE" }

下面结合服务模型中Methodshape 的定义(service-2.json 第 5667-5716 行)逐字段解读。

3.1 方法级顶层字段

字段类型含义
httpMethodString方法请求的 HTTP 动词,此处为GET
authorizationTypeString方法的鉴权类型。有效值:NONE(开放访问)、AWS_IAM(使用 IAM 权限)、CUSTOM(使用自定义 Authorizer)、COGNITO_USER_POOLS(使用 Cognito 用户池)。示例返回NONE,说明该方法允许匿名调用
authorizerIdStringauthorizationTypeCUSTOM时,指向所用 Authorizer 的标识符(未配置时不返回该字段)
apiKeyRequiredBoolean是否必须携带有效 API Key 才能调用该方法。示例为false,表示无需 API Key
requestParametersMap方法请求参数的声明,键形如method.request.{location}.{name}locationquerystringpathheader),值为布尔型,true表示必填、false表示可选。示例为空对象{},说明该方法未声明任何请求参数
requestModelsMap以内容类型为键、以 Model 名称为值的数据模型映射,用于描述请求负载的结构
methodResponsesMap方法响应集合,以 HTTP 状态码为键(详见下文 3.3)
methodIntegrationIntegration方法的后端集成配置(详见下文 3.2)
operationNameString方法的可读操作标识,例如可为GET /pets分配ListPets
requestValidatorIdString方法请求校验器(RequestValidator)的标识符
authorizationScopesList配合COGNITO_USER_POOLSAuthorizer 使用的授权范围列表

3.2 methodIntegration:后端集成配置

methodIntegration对应 shapeIntegration,描述"负责把客户端请求转给后端、并执行必要转换"的集成。示例中的关键子字段:

字段含义
type集成类型,示例为AWS,表示 AWS 服务集成(如 Lambda、DynamoDB)
uri集成端点 URI。示例中为 Lambda 代理 URI:arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:123412341234:function:My_Function/invocations,其中us-west-2为区域,My_Function为 Lambda 函数名
httpMethod集成请求发给后端的 HTTP 方法。示例为POST(注意:与前端方法的GET不同,这是 Lambda 集成典型的"前端 GET、后端 POST"组合)
cacheNamespace缓存命名空间,默认取资源 ID(示例y9h6rtresource-id一致),用于区分不同资源的缓存
cacheKeyParameters参与缓存键计算的参数列表,示例为空
integrationResponses集成响应集合,示例中只定义了200状态码,其responseTemplatesapplication/json模板为null(即透传后端响应体),statusCode200

3.3 methodResponses:方法级响应配置

methodResponses对应 shapeMethodResponse(service-2.json 第 5717-5734 行),示例中:

"methodResponses": { "200": { "responseModels": { "application/json": "Empty" }, "statusCode": "200" } }
  • statusCode:方法响应的 HTTP 状态码(示例200);
  • responseModels:各内容类型使用的数据模型,示例application/json对应内置模型Empty(表示"空响应体");
  • responseParameters:方法响应头声明,键形如method.response.header.{name},值为该头是否必填(示例中未出现,说明没有声明任何响应头映射)。

四、与关联命令串成完整链路

get-method是"查询"操作,其输出对象由一系列"写"操作组装而成。完整链路如下(全部示例均取自 awscli/examples/apigateway 目录):

4.1 创建 API

aws apigateway create-rest-api --name 'My First API' --description 'This is my first API'

返回结果中的id即后续所有命令使用的rest-api-id(参考 create-rest-api.rst)。

4.2 创建资源

aws apigateway create-resource --rest-api-id 1234123412 --parent-id a1b2c3 --path-part 'new-resource'

parent-id一般为根资源 ID,返回结果中的idresource-id(参考 create-resource.rst)。

4.3 创建方法(put-method)

aws apigateway put-method --rest-api-id 1234123412 --resource-id a1b2c3 --http-method PUT \ --authorization-type "NONE" --no-api-key-required \ --request-parameters "method.request.header.custom-header=false"

该命令创建了一个"无鉴权、无需 API Key、带一个可选自定义请求头custom-header"的PUT方法(参考 put-method.rst)。注意这里声明的请求参数,稍后就会出现在get-method返回的requestParameters中。

4.4 配置后端集成(put-integration)

# MOCK 集成(用于模拟返回) aws apigateway put-integration --rest-api-id 1234123412 --resource-id a1b2c3 --http-method GET \ --type MOCK --request-templates '{ "application/json": "{\"statusCode\": 200}" }' # HTTP 集成 aws apigateway put-integration --rest-api-id 1234123412 --resource-id a1b2c3 --http-method GET \ --type HTTP --integration-http-method GET --uri 'https://domain.tld/path' # AWS 集成(Lambda 端点) aws apigateway put-integration --rest-api-id 1234123412 --resource-id a1b2c3 --http-method GET \ --type AWS --integration-http-method POST \ --uri 'arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:123412341234:function:function_name/invocations'

参考 put-integration.rst。示例文档中的get-method输出所展示的正是第三种(type: AWS、Lambda 集成)的查询结果。

4.5 查询并校验(get-method)

aws apigateway get-method --rest-api-id 1234123412 --resource-id y9h6rt --http-method GET

执行后即可核对上述各环节是否按预期生效。


五、细节洞察:返回结果中的两个关键对应关系

从示例输出中可以提炼出两条排错与审计时非常有用的对应关系:

  1. cacheNamespaceresource-id一致:API Gateway 默认以资源 ID 作为集成缓存的命名空间。当同一资源下多个方法共享缓存时,这一字段决定了缓存隔离的粒度。若发现缓存命中异常,应优先检查cacheNamespace是否因手动修改而与其他方法冲突。

  2. 前端httpMethod与集成httpMethod可以不同:示例中前端方法是GET,而集成发往后端(Lambda)的方法是POST。这是 API Gateway 的常规设计——前端 HTTP 动词和后端集成动词是解耦的两个独立配置,分别由put-method--http-methodput-integration--integration-http-method控制。

  3. responseTemplatesnull的含义integrationResponses.200.responseTemplates.application/jsonnull表示该内容类型没有自定义映射模板,即后端响应体按原样透传给客户端。如果需要在此处做响应体改写,应使用put-integration-response配置映射模板。


六、边界情况与错误处理

get-method的服务模型(service-2.json 第 1125-1129 行)声明了三种错误返回类型,理解它们有助于快速定位问题:

异常类型触发场景常见 CLI 报错表现
UnauthorizedException调用方缺少执行apigateway:GET的 IAM 权限An error occurred (UnauthorizedException) ...
NotFoundExceptionrest-api-idresource-id不存在,或该资源上未定义指定的http-methodAn error occurred (NotFoundException) when calling the GetMethod operation: ...
TooManyRequestsException超出 API Gateway 账户级节流配额429 状态码与节流提示

排查建议:

  • ID 拼写错误:先跑aws apigateway get-rest-apisaws apigateway get-resources --rest-api-id <id>核对两个 ID 的真实值(见 get-rest-apis.rst、get-resources.rst);
  • 方法未定义get-resources返回的resourceMethods键集合就是该资源上已定义的方法列表,get-method要求目标方法必须存在;
  • HTTP 方法大小写与写法--http-method需使用标准大写动词;对泛化路由(catch-all)可使用ANY
  • 确认目标方法本身能否正常响应:可配合aws apigateway test-invoke-method --rest-api-id <id> --resource-id <id> --http-method GET --path-with-query-string '/pets/1'做一次真实调用验证(参考 test-invoke-method.rst)。

七、进一步阅读

  • 本命令官方示例原文:get-method.rst
  • 服务模型中的完整定义(URI、请求/响应 shape、错误列表):awscli/botocore/data/apigateway/2015-07-09/service-2.json
  • 关联的写操作示例:put-method.rst、put-integration.rst、put-method-response.rst
  • 关联的读取操作示例:get-integration.rst、get-method-response.rst、get-resource.rst
  • 上游资源创建示例:create-rest-api.rst、create-resource.rst

【免费下载链接】aws-cliUniversal Command Line Interface for Amazon Web Services项目地址: https://gitcode.com/GitHub_Trending/aw/aws-cli

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询