Amazon API Gateway
开发人员指南
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 Amazon AWS 入门

保护金丝雀版本

提升金丝雀版本,使其在正测试的 API 版本的生产阶段中可用。操作涉及到以下任务:

  • 使用金丝雀版本的部署 ID 设置重置阶段的 部署 ID。这会使用金丝雀版本的快照更新阶段的 API 快照,使得测试版本也成为生产版本。

  • 使用金丝雀版本阶段变量更新阶段变量 (如有)。这会使用金丝雀版本的内容更新阶段的 API 执行上下文。没有此更新时,如果测试版本使用不同的阶段变量或者现有阶段变量的不同值,新 API 版本可能会造成意外的结果。

  • 将金丝雀版本流量的百分比设置为 0.0%。

提升金丝雀版本不会在阶段上禁用金丝雀版本。要禁用金丝雀版本,您必须在阶段上删除金丝雀版本设置。

使用 API Gateway 控制台提升金丝雀版本

要使用 API Gateway 控制台提升金丝雀版本部署,请执行以下步骤:

  1. 登录 API Gateway 控制台并在主导航窗格中选择现有 API。

  2. 在 API 下选择 Stages,然后在 Stages 列表下选择现有阶段以打开 Stage Editor

  3. Stage Editor 中选择 Canary 选项卡。

  4. 选择 Promote Canary

  5. 确认所做更改,然后选择 Update

提升之后,生产版本引用相同的 API 版本 (deploymentId) 作为金丝雀版本。您可以使用 AWS CLI 或 API Gateway REST API 验证此项。例如,请参阅 使用 AWS CLI 提升金丝雀版本使用 API Gateway REST API 提升金丝雀版本

使用 AWS CLI 提升金丝雀版本

要使用 AWS CLI 命令将金丝雀版本提升到生产版本,请调用 update-stage 命令以将与金丝雀版本关联的 deploymentId 复制到与阶段关联的 deploymentId,将金丝雀版本流量百分比重置为零 (0.0),并且将任意金丝雀版本绑定阶段变量复制到对应的阶段绑定变量。

Suppose we have a canary release deployment, described by a stage similar to the following:

{ "_links": { ... }, "accessLogSettings": { ... }, "cacheClusterEnabled": false, "cacheClusterStatus": "NOT_AVAILABLE", "canarySettings": { "deploymentId": "eh1sby", "useStageCache": false, "stageVariableOverrides": { "sv2": "val3", "sv1": "val2" }, "percentTraffic": 10.5 }, "createdDate": "2017-11-20T04:42:19Z", "deploymentId": "nfcn0x", "lastUpdatedDate": "2017-11-22T00:54:28Z", "methodSettings": { ... }, "stageName": "prod", "variables": { "sv1": "val1" } }

我们调用以下 update-stage 请求来提升它:

aws apigateway update-stage \ --rest-api-id {rest-api-id} \ --stage-name '{stage-name}' \ --patch-operations '[{ \ "op": "replace", \ "value": "0.0" \ "path": "/canarySettings/percentTraffic", \ }, { \ "op": "copy", \ "from": "/canary/overriddenStageVariables" \ "path": "/variables", \ }, { \ "op": "copy", \ "from": "/canary/deploymentId", \ "path": "/deploymentId" \ }]'

After the promotion, the stage now looks like this:

{ "_links": { ... }, "accessLogSettings": { ... }, "cacheClusterEnabled": false, "cacheClusterStatus": "NOT_AVAILABLE", "canarySettings": { "deploymentId": "eh1sby", "useStageCache": false, "stageVariableOverrides": { "sv2": "val3", "sv1": "val2" }, "percentTraffic": 0 }, "createdDate": "2017-11-20T04:42:19Z", "deploymentId": "eh1sby", "lastUpdatedDate": "2017-11-22T05:29:47Z", "methodSettings": { ... }, "stageName": "prod", "variables": { "sv2": "val3", "sv1": "val2" } }

As you can see, promoting a canary release to the stage does not disable the canary and the deployment remains to be a canary release deployment. To make it a regular production release deployment, you must disable the canary settings. For more information about how to disable a canary release deployment, see 禁用金丝雀版本.

使用 API Gateway REST API 提升金丝雀版本

要使用 API Gateway REST API 将金丝雀版本提升到生产版本,请调用 stage:update 请求以将与金丝雀版本关联的 deploymentId 复制到与阶段关联的 deploymentId,将金丝雀版本流量百分比重置为零 (0.0),并且将任意金丝雀版本绑定阶段变量复制到对应的阶段绑定变量。

Suppose we have a canary release deployment, described by a stage similar to the following:

{ "_links": { ... }, "accessLogSettings": { ... }, "cacheClusterEnabled": false, "cacheClusterStatus": "NOT_AVAILABLE", "canarySettings": { "deploymentId": "eh1sby", "useStageCache": false, "stageVariableOverrides": { "sv2": "val3", "sv1": "val2" }, "percentTraffic": 10.5 }, "createdDate": "2017-11-20T04:42:19Z", "deploymentId": "nfcn0x", "lastUpdatedDate": "2017-11-22T00:54:28Z", "methodSettings": { ... }, "stageName": "prod", "variables": { "sv1": "val1" } }

我们调用以下 stage:update 请求来提升它:

PATCH /restapis/4wk1k4onj3/stages/prod HTTP/1.1 Host: apigateway.us-east-1.amazonaws.com Content-Type: application/json X-Amz-Date: 20171121T232431Z Authorization: AWS4-HMAC-SHA256 Credential={SECRET_ACCESS_KEY}/20171121/us-east-1/apigateway/aws4_request, SignedHeaders=content-length;content-type;host;x-amz-date, Signature={SIGV4_SIGNATURE} { "patchOperations": [ { "op": "copy", "path": "/deploymentId", "from": "/canarySettings/deploymentId" }, { "op": "replace", "path": "/canarySettings/percentTraffic", "value": "0.0" }, { "op": "copy", "path": "/variables", "from": "/canarySettings/stageVariableOverrides" } ] }

After the promotion, the stage now looks like this:

{ "_links": { ... }, "accessLogSettings": { ... }, "cacheClusterEnabled": false, "cacheClusterStatus": "NOT_AVAILABLE", "canarySettings": { "deploymentId": "eh1sby", "useStageCache": false, "stageVariableOverrides": { "sv2": "val3", "sv1": "val2" }, "percentTraffic": 0 }, "createdDate": "2017-11-20T04:42:19Z", "deploymentId": "eh1sby", "lastUpdatedDate": "2017-11-22T05:29:47Z", "methodSettings": { ... }, "stageName": "prod", "variables": { "sv2": "val3", "sv1": "val2" } }

As you can see, promoting a canary release to the stage does not disable the canary and the deployment remains to be a canary release deployment. To make it a regular production release deployment, you must disable the canary settings. For more information about how to disable a canary release deployment, see 禁用金丝雀版本.