使用AWS CLI为API Gateway特定阶段创建使用计划时遇错误
问题:创建API网关使用计划时触发BadRequestException错误
我想为关联的API阶段创建使用计划,执行的AWS CLI命令如下:
aws apigateway create-usage-plan --name "New Usage Plan1" --api-stages '[{ "apiId": "abcde12345", "stage":"test-stage", "throttle": {"Test from CLI": { "burstLimit": 200, "rateLimit": 100}}}]' --throttle burstLimit=200,rateLimit=100 --quota limit=500,offset=0,period=MONTH
其中abcde12345是我的Rest API ID,test-stage是要关联的阶段,"Test from CLI"是已创建好的API密钥。执行后报错:
An error occurred (BadRequestException) when calling the CreateUsagePlan operation: Invalid method {resourcePath: Test from CLI,method: } specified
换成API密钥IDabc123后,错误依旧:
An error occurred (BadRequestException) when calling the CreateUsagePlan operation: Invalid method {resourcePath: abc123,method: } specified
我是参考AWS官方文档写的命令,请问哪里错了?
解决方案
你混淆了参数的用途:--api-stages里的throttle字段是用来给API的具体方法(比如GET /users)设置单独节流规则的,不是用来关联API密钥的。你把API密钥/密钥ID放在这个位置,AWS会误以为这是某个API方法的路径,所以才报"Invalid method"错误。
正确操作分两步:
第一步:创建使用计划(去掉
--api-stages里错误的throttle内容,如果不需要给特定方法单独节流,这部分可以完全删掉):aws apigateway create-usage-plan --name "New Usage Plan1" --api-stages '[{ "apiId": "abcde12345", "stage":"test-stage" }]' --throttle burstLimit=200,rateLimit=100 --quota limit=500,offset=0,period=MONTH执行后会返回使用计划ID(例如
up-xxxxxx),请记录下来。第二步:关联API密钥到使用计划:
aws apigateway create-usage-plan-key --usage-plan-id up-xxxxxx --key-id abc123 --key-type API_KEY其中
--key-id填你的API密钥ID(abc123),--usage-plan-id填第一步得到的使用计划ID。
这样就能完成使用计划的创建和API密钥的关联了。
内容的提问来源于stack exchange,提问作者Navidk
相关产品推荐
相关产品推荐

