

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 为 Amazon Cognito 资源贴标签
<a name="tagging"></a>

*标签*是您或 AWS 分配给 AWS 资源的元数据标签。每个标签均包含一个*键* 和一个*值*。对于您分配的标签，需要定义键和值。例如，您可以将键定义为 `stage`，将一个资源的值定义为 `test`。

标签可帮助您：
+ 识别和整理您的 AWS 资源。许多 AWS 服务都支持标记，因此您可以为来自不同服务的资源分配相同的标签。这有助于您指示哪些资源是相关的。例如，您可以将相同的标签分配给您分配至 Amazon DynamoDB 表的 Amazon Cognito 用户池。
+ 追踪您的 AWS 成本。您可以在 AWS 账单与成本管理 控制面板上激活这些标签。 AWS 使用成本分配标签对您的成本进行分类，并向您提供每月成本分配报告。有关更多信息，请参阅《AWS Billing 用户指南》**中的[使用成本分配标签](https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/cost-alloc-tags.html)。
+ 根据分配给资源的标签控制对资源的访问。您可以通过在 AWS Identity and Access Management （IAM）策略的条件中指定标签键和值来控制访问权限。例如，您可以允许用户更新用户池，但前提是该用户池具有 `owner` 标签且值为该用户的名称。有关更多信息，请参阅《IAM 用户指南》**中的[使用标签控制访问](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_tags.html)。

您可以使用 AWS Command Line Interface 或 Amazon Cognito API 为用户和身份池添加、编辑或删除标签。您还可以使用 Amazon Cognito 控制台来管理用户池的标签。

有关使用标签的提示，请参阅 *AWS Answers* 博客上的 [AWS tagging strategies](https://aws.amazon.com/answers/account-management/aws-tagging-strategies/) 文章。

以下各部分提供有关 Amazon Cognito 标签的更多信息。

## Amazon Cognito 中支持的资源
<a name="tagging-supported-resources"></a>

Amazon Cognito 中的以下资源支持贴标签：
+ 用户池
+ 身份池

## 标签限制
<a name="tagging-restrictions"></a>

以下限制适用于 Amazon Cognito 资源上的标签：
+ 您可以分配给资源的最大标签数量 - 50 
+ 最大密钥长度 – 128 个 Unicode 字符 
+ 最大值长度 – 256 个 Unicode 字符 
+ 键和值的有效字符 – a-z、A-Z、0-9、空格和以下字符：\$1 . : / = \$1 - 和 @
+ 键和值区分大小写
+ 请不要使用 `aws:` 作为键的前缀；它保留为供 AWS 使用

## 使用 Amazon Cognito 控制台管理标签
<a name="tagging-console"></a>

您可以使用 Amazon Cognito 控制台来管理分配给您的用户池的标签。

**将标签添加到用户池**

1. 导航到 [Amazon Cognito 控制台](https://console.aws.amazon.com/cognito/home)。如果出现提示，请输入您的 AWS 凭据。

1. 选择**用户池**。

1. 从列表中选择一个现有用户池，或[创建一个用户池](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pool-as-user-directory.html)。

1. 选择**设置**菜单并找到**标签**选项卡。

1. 选择 **Add tags**（添加标签）即可添加您的第一个标签。如果您之前已将标签分配给此用户池，请在 **Manage tags**（管理标签）中选择 **Add another**（添加其它）。

1. 为 **Tag key**（标签键）和 **Tag value**（标签值）指定值。

1. 对于每个要添加的其它标签，请选择 **Add another**（添加其它）。

1. 添加完标签后，选择 **Save changes**（保存更改）。

要为身份池贴标签，请导航到**身份池**菜单，然后选择或创建身份池。在**身份池属性**选项卡中，找到**标签**。选择 **Add tag（添加标签）**。

## AWS CLI 例子
<a name="tagging-cli-examples"></a>

 AWS CLI 提供的命令可帮助您管理分配给 Amazon Cognito 用户池和身份池的标签。

### 分配标签
<a name="tagging-cli-examples-assigning"></a>

使用以下命令可将标签分配给现有的用户池和身份池。

**Example 适用于用户池的 `tag-resource` 命令**  
使用 `cognito-idp` 命令集中的 [https://docs.aws.amazon.com/cli/latest/reference/cognito-idp/tag-resource.html](https://docs.aws.amazon.com/cli/latest/reference/cognito-idp/tag-resource.html)将标签分配给用户池：  

```
$ aws cognito-idp tag-resource \
> --resource-arn user-pool-arn \
> --tags Stage=Test
```
此命令包含以下参数：  
+ `resource-arn` - 您向其应用标签的用户池的 Amazon Resource Name（ARN）。要查找 ARN，请在 Amazon Cognito 控制台中选择该用户池，并查看 **General settings**（常规设置）选项卡上的 **Pool ARN**（池 ARN）值。
+ `tags` – 以格式 `key=value` 表示的标签的键值对。
要一次分配多个标签，请以逗号分隔的列表形式指定它们：  

```
$ aws cognito-idp tag-resource \
> --resource-arn user-pool-arn \
> --tags Stage=Test,CostCenter=80432,Owner=SysEng
```

**Example 适用于身份池的 `tag-resource` 命令**  
使用 `cognito-identity` 命令集中的 [https://docs.aws.amazon.com/cli/latest/reference/cognito-identity/tag-resource.html](https://docs.aws.amazon.com/cli/latest/reference/cognito-identity/tag-resource.html) 将标签分配给身份池：  

```
$ aws cognito-identity tag-resource \
> --resource-arn identity-pool-arn \
> --tags Stage=Test
```
此命令包含以下参数：  
+ `resource-arn` - 您向其应用标签的身份池的 Amazon Resource Name（ARN）。要查找 ARN，请在 Amazon Cognito 控制台中选择身份池，并选择 **Edit identity pool (编辑身份池)**。然后，在 **Identity pool ID**（身份池 ID）中，选择 **Show ARN**（显示 ARN）。
+ `tags` – 以格式 `key=value` 表示的标签的键值对。
要一次分配多个标签，请以逗号分隔的列表形式指定它们：  

```
$ aws cognito-identity tag-resource \
> --resource-arn identity-pool-arn \
> --tags Stage=Test,CostCenter=80432,Owner=SysEng
```

### 查看标签
<a name="tagging-cli-examples-viewing"></a>

使用以下命令可查看您已分配给用户池和身份池的标签。

**Example 适用于用户池的 `list-tags-for-resource` 命令**  
使用 `cognito-idp` 命令集中的 [https://docs.aws.amazon.com/cli/latest/reference/cognito-idp/list-tags-for-resource.html](https://docs.aws.amazon.com/cli/latest/reference/cognito-idp/list-tags-for-resource.html) 查看分配给用户池的标签：  

```
$ aws cognito-idp list-tags-for-resource --resource-arn user-pool-arn
```

**Example 适用于身份池的 `list-tags-for-resource` 命令**  
使用 `cognito-identity` 命令集中的 [https://docs.aws.amazon.com/cli/latest/reference/cognito-identity/list-tags-for-resource.html](https://docs.aws.amazon.com/cli/latest/reference/cognito-identity/list-tags-for-resource.html) 查看分配给身份池的标签：  

```
$ aws cognito-identity list-tags-for-resource --resource-arn identity-pool-arn
```

### 移除标签
<a name="tagging-cli-examples-removing"></a>

使用以下命令从用户池和身份池中删除标签。

**Example 适用于用户池的 `untag-resource` 命令**  
使用 `cognito-idp` 命令集中的 [https://docs.aws.amazon.com/cli/latest/reference/cognito-idp/untag-resource.html](https://docs.aws.amazon.com/cli/latest/reference/cognito-idp/untag-resource.html) 删除用户池中的标签：  

```
$ aws cognito-idp untag-resource \
> --resource-arn user-pool-arn \
> --tag-keys Stage CostCenter Owner
```
对于 `--tag-keys` 参数，请指定一个或多个标签键。请勿包含标签值。用空格分隔键。

**Example 适用于身份池的 `untag-resource` 命令**  
使用 `cognito-identity` 命令集中的 [https://docs.aws.amazon.com/cli/latest/reference/cognito-identity/untag-resource.html](https://docs.aws.amazon.com/cli/latest/reference/cognito-identity/untag-resource.html) 删除身份池中的标签：  

```
$ aws cognito-identity untag-resource \
> --resource-arn identity-pool-arn \
> --tag-keys Stage CostCenter Owner
```
对于 `--tag-keys` 参数，请指定一个或多个标签键。请勿包含标签值。

**重要**  
删除用户或身份池后，与已删除池相关的标签在删除后的 30天内仍然可以在控制台或 API 调用中显示。

### 在创建资源时应用标签
<a name="tagging-cli-examples-applying"></a>

使用以下命令可在创建用户池或身份池时分配标签。

**Example `create-user-pool` 命令以及标签**  
当您使用 [https://docs.aws.amazon.com/cli/latest/reference/cognito-idp/create-user-pool.html](https://docs.aws.amazon.com/cli/latest/reference/cognito-idp/create-user-pool.html) 命令创建用户时，您可以使用 `--user-pool-tags` 参数指定标签：  

```
$ aws cognito-idp create-user-pool \
> --pool-name user-pool-name \
> --user-pool-tags Stage=Test,CostCenter=80432,Owner=SysEng
```
标签的键值对必须采用格式 `key=value`。如果要添加多个标签，请以逗号分隔的列表形式指定它们。

**Example `create-identity-pool` 命令以及标签**  
当您使用 [https://docs.aws.amazon.com/cli/latest/reference/cognito-identity/create-identity-pool.html](https://docs.aws.amazon.com/cli/latest/reference/cognito-identity/create-identity-pool.html) 命令创建用户池时，您可以使用 `--identity-pool-tags` 参数指定标签：  

```
$ aws cognito-identity create-identity-pool \
> --identity-pool-name identity-pool-name \
> --allow-unauthenticated-identities \
> --identity-pool-tags Stage=Test,CostCenter=80432,Owner=SysEng
```
标签的键值对必须采用格式 `key=value`。如果要添加多个标签，请以逗号分隔的列表形式指定它们。

## 使用 Amazon Cognito API 管理标签
<a name="tagging-api"></a>

您可以在 Amazon Cognito API 中使用以下操作来管理用户池和身份池的标签。

### 适用于用户池标签的 API 操作
<a name="tagging-api-user-pools"></a>

使用以下 API 操作来分配、查看和删除用户池的标签。
+ [TagResource](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_TagResource.html)
+ [ListTagsForResource](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_ListTagsForResource.html)
+ [UntagResource](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_UntagResource.html)
+ [CreateUserPool](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_CreateUserPool.html)

### 适用于身份池标签的 API 操作
<a name="tagging-api-identity-pools"></a>

使用以下 API 操作来分配、查看和删除身份池的标签。
+ [TagResource](https://docs.aws.amazon.com/cognitoidentity/latest/APIReference/API_TagResource.html)
+ [ListTagsForResource](https://docs.aws.amazon.com/cognitoidentity/latest/APIReference/API_ListTagsForResource.html)
+ [UntagResource](https://docs.aws.amazon.com/cognitoidentity/latest/APIReference/API_UntagResource.html)
+ [CreateIdentityPool](https://docs.aws.amazon.com/cognitoidentity/latest/APIReference/API_CreateIdentityPool.html)