AWS API Gateway CLI更新集成请求时$参数丢失问题求助
The issue here is that your shell is interpreting the $context prefixes as shell variables and expanding (or stripping) them before the command even reaches AWS API Gateway. On top of that, the quoting in your original command is misconfigured, which makes the problem worse. Here's how to resolve it:
Solution 1: Escape $ Signs and Fix Shell Quoting
In bash (and most POSIX shells), you need to escape the $ character with a backslash (\$) to prevent the shell from treating it as a variable. You also need to properly escape inner double quotes in your JSON string so the shell doesn't split the command incorrectly.
Use this corrected CLI command:
aws apigateway update-integration --rest-api-id aoi84daah3 --resource-id jh5tps --http-method POST --patch-operations "op='add',path='/requestTemplates/application~1json',value='{\"api_name\" : \"\$context.resourcePath\",\"http-method\" :\"\$context.httpMethod\"}'"
Key changes made:
- Each
$is escaped with\$to preserve it in the final template - Inner double quotes in the JSON are escaped with
\"to keep them part of the value string - The entire
--patch-operationsargument is wrapped in double quotes to maintain the correct structure
Solution 2: Use a File for Patch Operations (Cleaner Approach)
If shell escaping feels too error-prone, define your patch operations in a JSON file and reference it directly. This avoids all shell quoting headaches.
- Create a file named
patch-operations.jsonwith this content:
[ { "op": "add", "path": "/requestTemplates/application~1json", "value": "{\"api_name\" : \"$context.resourcePath\",\"http-method\" :\"$context.httpMethod\"}" } ]
- Run the CLI command using the file:
aws apigateway update-integration --rest-api-id aoi84daah3 --resource-id jh5tps --http-method POST --patch-operations file://patch-operations.json
After running either solution, check your integration's request template—you’ll see the $context variables preserved exactly as you intended.
内容的提问来源于stack exchange,提问作者Ludo

