

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 教學課程︰使用標籤指定要停止哪些資料庫執行個體
<a name="Tagging.RDS.Autostop"></a>

本教學課程假設您在開發或測試環境中有多個資料庫執行個體。您需要保留這些資料庫執行個體數天。某些資料庫執行個體可以在夜間執行測試，而其他資料庫執行個體可以在夜間停止，並在隔天再次啟動。

下列教學課程顯示如何將標籤指派給適合在夜間停止的資料庫執行個體。該教學課程會顯示指令碼如何偵測哪些資料庫執行個體具有該標籤，然後停止標記的資料庫執行個體。在這個範例中，鍵值對的值部分並不重要。`stoppable` 標籤的存在表示資料庫執行個體具有此使用者定義的屬性。

在下列教學課程中，標記 的命令和 APIs 可與 ARNs 搭配使用，這可讓 RDS 順暢地跨 AWS 區域、 AWS 帳戶和不同類型的資源，而這些資源可能有相同的短名。您可以在於資料庫執行個體上操作的 CLI 命令中指定 ARN 而不是資料庫執行個體 ID。

**若要指定要停止哪些資料庫執行個體**

1. 確定您要指定為可停止的資料庫執行個體 ARN。

   在下列範例中，將您自己的資料庫執行個體名稱替換為 *dev-test-db-instance*。在使用 ARN 參數的後續命令中，替換您自己的資料庫執行個體 ARN。ARN 包含您自己的 AWS 帳戶 ID 和資料庫執行個體所在的 AWS 區域名稱。

   ```
   $ aws rds describe-db-instances --db-instance-identifier dev-test-db-instance \
     --query "*[].{DBInstance:DBInstanceArn}" --output text
   arn:aws:rds:us-east-1:123456789102:db:dev-test-db-instance
   ```

1. 將標籤 `stoppable` 新增至此資料庫執行個體。

   您會選擇此標籤的名稱。由於此範例會將標籤視為存在或不存在的屬性，因此會省略 `Value=` 參數的 `--tags` 部分。這種方法表示您可以避免設計命名慣例，對名稱中的所有相關資訊進行編碼。在這類慣例中，您可能會對資料庫執行個體名稱或其他資源名稱中的資訊進行編碼。

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

1. 確認標籤存在於資料庫執行個體中。

   以下命令以 JSON 格式和純定位鍵分隔文字擷取資料庫執行個體的標籤資訊。

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

1. 停止所有指定為 `stoppable` 的資料庫執行個體。

   下列範例會建立可列出所有資料庫執行個體的文字檔案。shell 命令會循環瀏覽清單，並檢查每個資料庫執行個體是否標記了相關屬性，並為每個資料庫執行個體執行 `aws rds stop-db-instance` 命令。

   ```
   $ aws rds describe-db-instances --query "*[].[DBInstanceArn]" --output text >/tmp/db_instance_arns.lst
   $ for arn in $(cat /tmp/db_instance_arns.lst)
   do
     match="$(aws rds list-tags-for-resource --resource-name $arn --output text | grep stoppable)"
     if [[ ! -z "$match" ]]
     then
         echo "DB instance $arn is tagged as stoppable. Stopping it now."
   # Note that you need to get the DB instance identifier from the ARN.
         dbid=$(echo $arn | sed -e 's/.*://')
         aws rds stop-db-instance --db-instance-identifier $dbid
     fi
   done
   
   DB instance arn:arn:aws:rds:us-east-1:123456789102:db:dev-test-db-instance is tagged as stoppable. Stopping it now.
   {
       "DBInstance": {
           "DBInstanceIdentifier": "dev-test-db-instance",
           "DBInstanceClass": "db.t3.medium",
           ...
   ```

您可以在每天結束時執行如上的指令碼，以確保非必要的資料庫執行個體已停止。您也可以使用 `cron` 之類的公用程式來排程要執行的任務，例如每天晚上檢查。例如，如果某些資料庫執行個體錯誤地保持執行中狀態，您可以執行此操作。在這裡，您可以微調準備要檢查的資料庫執行個體清單的命令。

下列命令會產生資料庫執行個體的清單，但只會產生 `available` 狀態的執行個體清單。指令碼可以忽略已停止的資料庫執行個體，因為它們會有不同的狀態值，例如 `stopped` 或 `stopping`。

```
$ aws rds describe-db-instances \
  --query '*[].{DBInstanceArn:DBInstanceArn,DBInstanceStatus:DBInstanceStatus}|[?DBInstanceStatus == `available`]|[].{DBInstanceArn:DBInstanceArn}' \
  --output text
arn:aws:rds:us-east-1:123456789102:db:db-instance-2447
arn:aws:rds:us-east-1:123456789102:db:db-instance-3395
arn:aws:rds:us-east-1:123456789102:db:dev-test-db-instance
arn:aws:rds:us-east-1:123456789102:db:pg2-db-instance
```

**提示**  
您可以使用指派標籤，並尋找具有這些標籤的資料庫執行個體，以其他方式降低成本。例如，採取此案例搭配用於開發和測試的資料庫執行個體。在此情況下，您可以指定一些要在每天結束時刪除的資料庫執行個體。或者，您可以指定它們，在預期的低使用量期間將其資料庫執行個體變更為小型資料庫執行個體類別。