Patch operations for updating api resources with the AWS CLI

When updating API Gateway resources using the AWS CLI, some commands include a --patch-operations argument.

For example, the update-base-path-mapping allows an optional --patch-operations. For more information about which resources are supported via patch operations, the AWS documentation is here.

How to use the --patch-operations argument

In the case for updating a base path mapping, a user may need to supply a restapiId and stage value.

The JSON array:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
[
  {
    "op": "replace",
    "path": "/restapiId",
    "value": "id"
  },
  {
    "op": "replace",
    "path": "/stage",
    "value": "prod"
  }
]

The aws apigateway update-base-path-mapping allows for a stringified json array value for the --patch-operations argument:

1
2
3
4
5
6
7
8
domain_name="example.com"
base_path="some-path"
api_id="id"
stage="prod"
aws apigateway update-base-path-mapping \
	--domain-name $domain_name \
	--base-path $base_path \
	--patch-operations "[{\"op\":\"replace\",\"path\":\"/restapiId\",\"value\":\"$api_id\"},{\"op\":\"replace\",\"path\":\"/stage\",\"value\":\"$stage\"}]"

The same --patch-operations argument format applies to the other API Gateway commands as well.