

# 为 API Gateway 中的 API 阶段设置标签
<a name="set-up-tags"></a>

在 API Gateway 中，您可以将标签添加到 API 阶段、从阶段中删除标签或者查看标签。为此，您可以使用 API Gateway 控制台、AWS CLI/开发工具包或 API Gateway REST API。

阶段还可以从其父 REST API 继承标签。有关更多信息，请参阅 [Amazon API Gateway V1 API 中的标签继承](apigateway-tagging-supported-resources.md#apigateway-tagging-inheritance)。

有关标记 API Gateway 资源的更多信息，请参阅 [为 API Gateway 资源添加标签](apigateway-tagging.md)。

**Topics**
+ [使用 API Gateway 控制台为 API 阶段设置标签](#set-up-tags-using-console)
+ [使用 AWS CLI 为 API 阶段设置标签](#set-up-tags-using-cli)
+ [使用 API Gateway REST API 为 API 阶段设置标签](#set-up-tags-using-api)

## 使用 API Gateway 控制台为 API 阶段设置标签
<a name="set-up-tags-using-console"></a>

以下过程将介绍如何为 API 阶段设置标签。

**使用 API Gateway 控制台为 API 阶段设置标签**

1. 登录 API Gateway 控制台。

1. 选择一个现有 API，或者创建包括资源、方法和对应集成的新 API。

1. 选择阶段或者将 API 部署到新阶段。

1. 在主导航窗格中，选择**阶段**。

1. 选择**标签**选项卡。您可能需要选择右箭头按钮以显示该选项卡。

1. 选择**管理标签**。

1. 在**标签编辑器**中，选择**添加标签**。在**键**字段中输入标签键（例如 `Department`），在**值**字段中输入标签值（例如 `Sales`）。选择**保存**以保存标签。

1.  如果需要，请重复第 5 步，将更多标签添加到 API 阶段。每个阶段的最大标签数是 50。

1.  要从阶段中移除现有标签，请选择**移除**。

1. 如果先前已在 API Gateway 控制台中部署了 API，则需要重新进行部署，以使更改生效。

## 使用 AWS CLI 为 API 阶段设置标签
<a name="set-up-tags-using-cli"></a>

您可以使用 AWS CLI，通过 [create-stage](https://docs.aws.amazon.com/cli/latest/reference/apigateway/create-stage.html) 命令或 [tag-resource](https://docs.aws.amazon.com/cli/latest/reference/apigateway/tag-resource.html) 命令为 API 阶段设置标签。您可以使用 [untag-resource](https://docs.aws.amazon.com/cli/latest/reference/apigateway/untag-resource.html) 命令从 API 阶段中删除一个或多个标签。

使用以下 [create-stage](https://docs.aws.amazon.com/cli/latest/reference/apigateway/create-stage.html) 命令在创建 `test` 阶段时添加一个标签：

```
aws apigateway create-stage --rest-api-id {{abc1234}} --stage-name test --description 'Testing stage' --deployment-id {{efg456}} --tag Department=Sales
```

使用以下 [tag-resource](https://docs.aws.amazon.com/cli/latest/reference/apigateway/tag-resource.html) 命令将标签添加到 `prod` 阶段中：

```
aws apigateway tag-resource --resource-arn arn:aws:apigateway:{{us-east-2}}::/restapis/{{abc123}}/stages/prod --tags Department=Sales
```

使用以下 [untag-resource](https://docs.aws.amazon.com/cli/latest/reference/apigateway/untag-resource.html) 命令从 `test` 阶段中删除 `Department=Sales` 标签：

```
aws apigateway untag-resource --resource-arn arn:aws:apigateway:{{us-east-2}}::/restapis/{{abc123}}/stages/test --tag-keys Department 
```

## 使用 API Gateway REST API 为 API 阶段设置标签
<a name="set-up-tags-using-api"></a>

您可以执行以下操作之一，使用 API Gateway REST API 为 API 阶段设置标签：
+ 调用 [https://docs.aws.amazon.com/apigateway/latest/api/API_TagResource.html](https://docs.aws.amazon.com/apigateway/latest/api/API_TagResource.html) 为 API 阶段添加标签。
+  调用 [https://docs.aws.amazon.com/apigateway/latest/api/API_UntagResource.html](https://docs.aws.amazon.com/apigateway/latest/api/API_UntagResource.html) 以从 API 阶段中删除一个或多个标签。
+ 调用 [https://docs.aws.amazon.com/apigateway/latest/api/API_CreateStage.html](https://docs.aws.amazon.com/apigateway/latest/api/API_CreateStage.html) 以将一个或多个标签添加到您正创建的 API 阶段。

您还可以调用 [https://docs.aws.amazon.com/apigateway/latest/api/API_GetTags.html](https://docs.aws.amazon.com/apigateway/latest/api/API_GetTags.html) 以描述 API 阶段中的标签。

### 为 API 阶段添加标签
<a name="tag-a-stage-using-api"></a>

将 API (`m5zr3vnks7`) 部署到阶段 (`test`) 之后，通过调用 [https://docs.aws.amazon.com/apigateway/latest/api/API_TagResource.html](https://docs.aws.amazon.com/apigateway/latest/api/API_TagResource.html) 为阶段添加标签。所需的阶段 Amazon Resource Name (ARN) (`arn:aws:apigateway:us-east-1::/restapis/m5zr3vnks7/stages/test`) 必须为 URL 编码 (`arn%3Aaws%3Aapigateway%3Aus-east-1%3A%3A%2Frestapis%2Fm5zr3vnks7%2Fstages%2Ftest`)。

```
PUT /tags/arn%3Aaws%3Aapigateway%3Aus-east-1%3A%3A%2Frestapis%2Fm5zr3vnks7%2Fstages%2Ftest

{
  "tags" : {
    "Department" : "Sales"
  }
}
```

 您还可以使用之前的请求，将现有标签更新为新值。

在调用 [https://docs.aws.amazon.com/apigateway/latest/api/API_CreateStage.html](https://docs.aws.amazon.com/apigateway/latest/api/API_CreateStage.html) 创建阶段时，您可以将标签添加到阶段。

```
POST /restapis/<restapi_id>/stages

{
  "stageName" : "test",
  "deploymentId" : "adr134",
  "description" : "test deployment",
  "cacheClusterEnabled" : "true",
  "cacheClusterSize" : "500",
  "variables" : {
    "sv1" : "val1"
  },
  "documentationVersion" : "test",

  "tags" : {
    "Department" : "Sales",
    "Division" : "Retail"
  }
}
```

### 取消 API 阶段的标签
<a name="untag-a-stage-using-api"></a>

 要从阶段中删除 `Department` 标签，请调用 [https://docs.aws.amazon.com/apigateway/latest/api/API_UntagResource.html](https://docs.aws.amazon.com/apigateway/latest/api/API_UntagResource.html)：

```
DELETE /tags/arn%3Aaws%3Aapigateway%3Aus-east-1%3A%3A%2Frestapis%2Fm5zr3vnks7%2Fstages%2Ftest?tagKeys=Department
Host: apigateway.us-east-1.amazonaws.com
Authorization: ...
```

 要删除多个标签，请在查询表达式中使用逗号分隔的标签键列表，例如 `?tagKeys=Department,Division,…`。

### 描述 API 阶段的标签
<a name="get-tags-using-api"></a>

要描述指定阶段上的现有标签，请调用 [https://docs.aws.amazon.com/apigateway/latest/api/API_GetTags.html](https://docs.aws.amazon.com/apigateway/latest/api/API_GetTags.html)：

```
GET /tags/arn%3Aaws%3Aapigateway%3Aus-east-1%3A%3A%2Frestapis%2Fm5zr3vnks7%2Fstages%2Ftags
Host: apigateway.us-east-1.amazonaws.com
Authorization: ...
```

成功响应的形式与下方类似：

```
200 OK

{
    "_links": {
        "curies": {
            "href": "http://docs.aws.amazon.com/apigateway/latest/developerguide/restapi-tags-{rel}.html",
            "name": "tags",
            "templated": true
        },
        "tags:tag": {
            "href": "/tags/arn%3Aaws%3Aapigateway%3Aus-east-1%3A%3A%2Frestapis%2Fm5zr3vnks7%2Fstages%2Ftags"
        },
        "tags:untag": {
            "href": "/tags/arn%3Aaws%3Aapigateway%3Aus-east-1%3A%3A%2Frestapis%2Fm5zr3vnks7%2Fstages%2Ftags{?tagKeys}",
            "templated": true
        }
    },
    "tags": {
        "Department": "Sales"
    }
}
```