通过 CLI 更新 AWS API Gateway 资源策略的正确语法?

tra*_*bal 5 api json command-line-interface amazon-web-services

我正在尝试通过 CLI 更新我的 API Gateway 实例上的资源策略,但我似乎找不到 JSON 的正确语法。在文档中它说要使用“补丁操作”,据我所知,它需要一串 JSON 来表示策略。我尝试过缩小 JSON、转义 JSON、单引号、无引号,但似乎没有任何效果。该文档在补丁操作的值字段中没有实际 JSON 的示例,所以我觉得有点迷茫。

我一直在尝试这个命令的变体:

aws apigateway update-rest-api --rest-api-id abcde123 --patch-operations op=replace,path=/policy,value='{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":"*","Action":"execute-api:Invoke","Resource":"arn:aws:execute-api:region:000000000000:*"},{"Effect":"Deny","Principal":"*","Action":"execute-api:Invoke","Resource":"arn:aws:execute-api:region:000000000000:*","Condition":{"StringNotEquals":{"aws:SourceVpce":["vpce-123456789","vpce-987654321"]}}}]}'

每次说时我都会收到错误消息:

Error parsing parameter '--patch-operations': Expected: '=', received: '{' for input:

相关文档在这里

Ami*_*nes 6

以下命令已针对我的环境进行了测试 - (使用 bash)

aws apigateway update-rest-api --rest-api-id %REST_API_ID% --patch-operations op=replace,path=/policy,value='"{\"Version
\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":\"*\",\"Action\":\"execute-api:Invoke\",\"Resource
\":\"arn:aws:execute-api:region:000000000000:*\"},{\"Effect\":\"Deny\",\"Principal\":\"*\",\"Action\":\"execute-api:Inv
oke\",\"Resource\":\"arn:aws:execute-api:region:000000000000:*\",\"Condition\":{\"StringNotEquals\":{\"aws:SourceVpce\"
:[\"vpce-123456789\",\"vpce-987654321\"]}}}]}"' --region %REGION%
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

关键是将 JSON 对象转换为字符串化的文本,我使用过这个站点。基本上,将您的 JSON 粘贴到输入文本框中,并将字符串化的文本复制到 AWS CLI 命令中。

更多信息在这里

  • 惊人的。谁会想到您必须添加单引号和双引号?太感谢了! (3认同)