

# 教程：使用标签指定要停止哪些 Aurora 数据库集群
<a name="Tagging.Aurora.Autostop"></a>

 假设您正在开发或测试环境中创建大量 Aurora 数据库集群。您需要将所有这些集群保留几天。一些集群会在夜间运行测试。另一些集群可以在夜间停止运行并在第二天重新开始运行。以下示例说明如何为适合在夜间停止运行的集群分配标签。之后，该示例显示脚本如何检测哪些集群具有该标签并随后停止这些集群。在此示例中，键值对的值部分无关紧要。存在`stoppable`标签表示集群具有此用户定义的属性。

**指定要停止的 Aurora 数据库集群**

1. 确定您要指定为“可停止”的集群的 ARN。

   用于标记的命令和 API 使用 ARN。这样一来，它们就可以在 AWS 区域、AWS 账户和可能具有相同短名称的不同类型资源之间无缝运作。您可以在集群上运行的 CLI 命令中指定 ARN 代替集群 ID。将自己集群的名称替换为 *dev-test-cluster*。在使用 ARN 参数的后续命令中，替换自己集群的 ARN。ARN 包含您自己的 AWS 账户 ID 和集群所在的 AWS 区域的名称。

   ```
   $ aws rds describe-db-clusters --db-cluster-identifier dev-test-cluster \
     --query "*[].{DBClusterArn:DBClusterArn}" --output text
   arn:aws:rds:us-east-1:123456789:cluster:dev-test-cluster
   ```

1. 将标签 `stoppable` 添加至此集群。

   由您选择此标签的名称。这种方法意味着，您可以避免设计一种在名称中对所有相关信息进行编码的命名约定。在此类约定中，您可以在数据库实例名称或其他资源的名称中对信息进行编码。由于此示例将标签视为存在或不存在的属性，因此它忽略了 `Value=` 参数的 `--tags` 部分。

   ```
   $ aws rds add-tags-to-resource \
     --resource-name arn:aws:rds:us-east-1:123456789:cluster:dev-test-cluster \
     --tags Key=stoppable
   ```

1. 确认集群中存在该标签。

   以下命令以 JSON 格式和以制表符分隔的纯文本检索集群的标签信息。

   ```
   $ aws rds list-tags-for-resource \
     --resource-name arn:aws:rds:us-east-1:123456789:cluster:dev-test-cluster 
   {
       "TagList": [
           {
               "Key": "stoppable",
               "Value": ""
   
           }
       ]
   }
   $ aws rds list-tags-for-resource \
     --resource-name arn:aws:rds:us-east-1:123456789:cluster:dev-test-cluster --output text
   TAGLIST stoppable
   ```

1. 要停止所有指定为`stoppable`的集群，请准备所有集群的列表。循环浏览列表，并检查每个集群是否都具有相关属性标签。

   此 Linux 示例使用 shell 脚本将群集 ARN 列表保存到临时文件中，然后为每个集群执行 CLI 命令。

   ```
   $ aws rds describe-db-clusters --query "*[].[DBClusterArn]" --output text >/tmp/cluster_arns.lst
   $ for arn in $(cat /tmp/cluster_arns.lst)
   do
     match="$(aws rds list-tags-for-resource --resource-name $arn --output text | grep 'TAGLIST\tstoppable')"
     if [[ ! -z "$match" ]]
     then
         echo "Cluster $arn is tagged as stoppable. Stopping it now."
   # Note that you can specify the full ARN value as the parameter instead of the short ID 'dev-test-cluster'.
         aws rds stop-db-cluster --db-cluster-identifier $arn
     fi
   done
   
   Cluster arn:aws:rds:us-east-1:123456789:cluster:dev-test-cluster is tagged as stoppable. Stopping it now.
   {
       "DBCluster": {
           "AllocatedStorage": 1,
           "AvailabilityZones": [
               "us-east-1e",
               "us-east-1c",
               "us-east-1d"
           ],
           "BackupRetentionPeriod": 1,
           "DBClusterIdentifier": "dev-test-cluster",
           ...
   ```

 您可以在每天结束时运行此类脚本，以确保停止非必要的集群。还可以使用实用程序（例如 `cron`）安排任务，如每晚执行此类检查。例如，您可以这样做，以防某些集群被误运行。在此，您可以对准备待检查集群列表的命令进行微调。

以下命令生成集群列表，但只生成处于`available`状态的集群的列表。该脚本可以忽略已停止的集群，因为它们将具有不同的状态值，如`stopped`或`stopping`。

```
$ aws rds describe-db-clusters \
  --query '*[].{DBClusterArn:DBClusterArn,Status:Status}|[?Status == `available`]|[].{DBClusterArn:DBClusterArn}' \
  --output text
arn:aws:rds:us-east-1:123456789:cluster:cluster-2447
arn:aws:rds:us-east-1:123456789:cluster:cluster-3395
arn:aws:rds:us-east-1:123456789:cluster:dev-test-cluster
arn:aws:rds:us-east-1:123456789:cluster:pg2-cluster
```

**提示**  
您可以将分配标签的过程与查找具有这些标签的集群的过程结合使用，以其他方式降低成本。我们以用于开发和测试的 Aurora 数据库集群为例。在此，您可以指定在每天结束时删除一些集群，或者仅删除其读取器数据库实例。或者，您可以指定某些集群在预期使用率较低的时期，将其数据库实例更改为小型数据库实例类。