AWS CLI调用TestInvokeMethod报错:指定的方法标识符无效
问题:调用AWS CLI的apigateway test-invoke-method时提示NotFoundException
我参照AWS官方文档示例,用AWS CLI调用后端API:
# 文档示例: # aws apigateway test-invoke-method --rest-api-id 1234123412 --resource-id avl5sg8fw8 --http-method GET --path-with-query-string '/' # 我的调用命令 aws apigateway test-invoke-method --rest-api-id ${restId} --resource-id ${resID} --http-method GET --path-with-query-string '/health'
执行后报错:
An error occurred (NotFoundException) when calling the TestInvokeMethod operation: Invalid Method identifier specified
我甚至怀疑是不是GET拼写有误?我的AWS CLI版本如下:
$ aws --version aws-cli/2.15.15 Python/3.11.6 Darwin/23.3.0 exe/x86_64 prompt/off
可能的原因及解决方法
资源ID或API ID错误
确认${restId}和${resID}变量是否正确赋值,对应的REST API和资源确实存在。可通过以下命令验证:- 查看所有REST API的ID:
aws apigateway get-rest-apis - 查看指定API下的所有资源ID:
aws apigateway get-resources --rest-api-id YOUR_REST_ID
确保/health对应的资源ID与${resID}一致。
- 查看所有REST API的ID:
目标资源未配置GET方法
即使/health资源存在,也必须为该资源明确配置GET方法。可通过以下方式验证:- 登录AWS控制台,查看该资源的方法列表
- 使用CLI命令:
aws apigateway get-methods --rest-api-id YOUR_REST_ID --resource-id YOUR_RESOURCE_ID
如果返回结果中没有GET方法,需要先为该资源创建GET方法。
路径参数与资源路径不匹配
确保--path-with-query-string指定的/health是资源的完整路径。如果资源路径是嵌套结构(比如/v1/health),仅传入/health会导致无法匹配到对应方法。CLI变量引用问题
检查shell环境中${restId}和${resID}是否正确导出。比如在bash中,可直接将变量替换为实际ID测试,避免因变量未解析导致传入空值或错误值:aws apigateway test-invoke-method --rest-api-id 实际APIID --resource-id 实际资源ID --http-method GET --path-with-query-string '/health'
内容的提问来源于stack exchange,提问作者Denis
相关产品推荐
相关产品推荐

