教學:使用 AWS CLI開始進行排定擴展 - Application Auto Scaling

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

教學:使用 AWS CLI開始進行排定擴展

下列教學課程說明如何透過協助您建立可擴展名 AWS CLI 為的範例 DynamoDB 表格的排程動作來開始進行排程調整。TestTable如果您在 DynamoDB 還沒有用來測試的 TestTable 資料表,您可以立即遵循《Amazon DynamoDB 開發人員指南》中的步驟 1:建立 DynamoDB 資料表操作,執行 create-table 指令建立一個。

使用時 AWS CLI,請記住您的命令在為您的設定檔設定的 AWS 區域中執行。如果您想在不同區域中執行命令,則可變更設定檔的預設區域,或搭配 --region 參數使用命令。

注意

作為本教程的一部分,您可能會產生 AWS 費用。請監控您的免費方案使用量,並確定您瞭解 DynamoDB 資料庫使用的讀寫容量單位數所引起的成本。

步驟 1:註冊可擴展的目標

首先向 Application Auto Scaling 將 DynamoDB 資料表註冊為可擴展的目標。

向 Application Auto Scaling 註冊可擴展的目標
  1. 首先,使用命describe-scalable-targets令檢查是否已註冊任何 DynamoDB 資源。這一步是為了讓您驗證 TestTable 資料表是否未註冊 (如果其不是新表)。

    Linux、macOS 或 Unix

    aws application-autoscaling describe-scalable-targets \ --service-namespace dynamodb

    Windows

    aws application-autoscaling describe-scalable-targets --service-namespace dynamodb

    如果沒有現有可擴展的目標,這就是回應。

    { "ScalableTargets": [] }
  2. 使用下列register-scalable-target命令來註冊所呼叫的 DynamoDB 表格的寫入容量。TestTable設定最低所需容量為 5 個寫入容量單位,而最高所需容量為 10 個寫入容量單位。

    Linux、macOS 或 Unix

    aws application-autoscaling register-scalable-target \ --service-namespace dynamodb \ --scalable-dimension dynamodb:table:WriteCapacityUnits \ --resource-id table/TestTable \ --min-capacity 5 --max-capacity 10

    Windows

    aws application-autoscaling register-scalable-target --service-namespace dynamodb --scalable-dimension dynamodb:table:WriteCapacityUnits --resource-id table/TestTable --min-capacity 5 --max-capacity 10

    如果成功,此命令會傳回可擴展目標的 ARN。

    { "ScalableTargetARN": "arn:aws:application-autoscaling:region:account-id:scalable-target/1234abcd56ab78cd901ef1234567890ab123" }

步驟 2:建立兩個排定的動作

Application Auto Scaling 可讓您排定應該發生擴展動作的時間。您可以指定可擴展目標、排程以及最低和最高容量。在指定的時間,Application Auto Scaling 會更新可擴展目標的最小值和最大值。如果目前的容量超出這個範圍,結果就會是擴展活動。

如果您決定建立擴展政策,最低和最高容量的排程更新也很實用。擴展政策可讓您根據目前的資源使用率動態擴展資源。擴展政策常見的護欄是擁有適當的最低及最高的容量。

在本練習中,我們將為向外擴展和向內擴展建立兩個一次性動作。

建立和檢視排程動作
  1. 若要建立第一個排程動作,請使用下列put-scheduled-action命令。

    --schedule 中的 at 命令會將動作排定在未來的指定日期和時間執行一次。小時為 UTC 時區 24 小時格式。請將動作排程距現在約 5 分鐘。

    在指定的日期和時間,Application Auto Scaling 會更新 MinCapacityMaxCapacity 值。假設資料表目前有 5 個寫入容量單位,Application Auto Scaling 會水平擴展至 MinCapacity,將資料表放入新的所需範圍內,即 15 到 20 個寫入容量單位。

    Linux、macOS 或 Unix

    aws application-autoscaling put-scheduled-action \ --service-namespace dynamodb \ --scalable-dimension dynamodb:table:WriteCapacityUnits \ --resource-id table/TestTable \ --scheduled-action-name my-first-scheduled-action \ --schedule "at(2019-05-20T17:05:00)" \ --scalable-target-action MinCapacity=15,MaxCapacity=20

    Windows

    aws application-autoscaling put-scheduled-action --service-namespace dynamodb --scalable-dimension dynamodb:table:WriteCapacityUnits --resource-id table/TestTable --scheduled-action-name my-first-scheduled-action --schedule "at(2019-05-20T17:05:00)" --scalable-target-action MinCapacity=15,MaxCapacity=20

    如果成功,此命令不會傳回任何輸出。

  2. 若要建立應用 Application Auto Scaling 用來縮放的第二個排程動作,請使用下列put-scheduled-action命令。

    請將動作排程距現在約 10 分鐘。

    在指定的日期和時間,Application Auto Scaling 會更新資料表的 MinCapacityMaxCapacity,並縮減至 MaxCapacity,讓資料表回到原始的所需範圍內,即 5 到 10 個寫入容量單位。

    Linux、macOS 或 Unix

    aws application-autoscaling put-scheduled-action \ --service-namespace dynamodb \ --scalable-dimension dynamodb:table:WriteCapacityUnits \ --resource-id table/TestTable \ --scheduled-action-name my-second-scheduled-action \ --schedule "at(2019-05-20T17:10:00)" \ --scalable-target-action MinCapacity=5,MaxCapacity=10

    Windows

    aws application-autoscaling put-scheduled-action --service-namespace dynamodb --scalable-dimension dynamodb:table:WriteCapacityUnits --resource-id table/TestTable --scheduled-action-name my-second-scheduled-action --schedule "at(2019-05-20T17:10:00)" --scalable-target-action MinCapacity=5,MaxCapacity=10
  3. (選擇性) 使用下列命令取得指定服務describe-scheduled-actions命名空間的排程動作清單。

    Linux、macOS 或 Unix

    aws application-autoscaling describe-scheduled-actions \ --service-namespace dynamodb

    Windows

    aws application-autoscaling describe-scheduled-actions --service-namespace dynamodb

    下列為範例輸出。

    { "ScheduledActions": [ { "ScalableDimension": "dynamodb:table:WriteCapacityUnits", "Schedule": "at(2019-05-20T18:35:00)", "ResourceId": "table/TestTable", "CreationTime": 1561571888.361, "ScheduledActionARN": "arn:aws:autoscaling:us-east-1:123456789012:scheduledAction:2d36aa3b-cdf9-4565-b290-81db519b227d:resource/dynamodb/table/TestTable:scheduledActionName/my-first-scheduled-action", "ScalableTargetAction": { "MinCapacity": 15, "MaxCapacity": 20 }, "ScheduledActionName": "my-first-scheduled-action", "ServiceNamespace": "dynamodb" }, { "ScalableDimension": "dynamodb:table:WriteCapacityUnits", "Schedule": "at(2019-05-20T18:40:00)", "ResourceId": "table/TestTable", "CreationTime": 1561571946.021, "ScheduledActionARN": "arn:aws:autoscaling:us-east-1:123456789012:scheduledAction:2d36aa3b-cdf9-4565-b290-81db519b227d:resource/dynamodb/table/TestTable:scheduledActionName/my-second-scheduled-action", "ScalableTargetAction": { "MinCapacity": 5, "MaxCapacity": 10 }, "ScheduledActionName": "my-second-scheduled-action", "ServiceNamespace": "dynamodb" } ] }

步驟 3:檢視擴展活動

在此步驟中,您會檢視排定的動作所觸發的擴展活動,然後驗證 DynamoDB 是否變更資料表的寫入容量。

檢視擴展活動
  1. 等待您選擇的時間,然後使用下列describe-scaling-activities命令確認排程的動作是否正常運作。

    Linux、macOS 或 Unix

    aws application-autoscaling describe-scaling-activities \ --service-namespace dynamodb

    Windows

    aws application-autoscaling describe-scaling-activities --service-namespace dynamodb

    下列是範例輸出排程動作進行中的第一個排程動作。

    擴展活動是依建立日期排序,並會先傳回最新的擴展活動。

    { "ScalingActivities": [ { "ScalableDimension": "dynamodb:table:WriteCapacityUnits", "Description": "Setting write capacity units to 15.", "ResourceId": "table/TestTable", "ActivityId": "d8ea4de6-9eaa-499f-b466-2cc5e681ba8b", "StartTime": 1561574108.904, "ServiceNamespace": "dynamodb", "Cause": "minimum capacity was set to 15", "StatusMessage": "Successfully set write capacity units to 15. Waiting for change to be fulfilled by dynamodb.", "StatusCode": "InProgress" }, { "ScalableDimension": "dynamodb:table:WriteCapacityUnits", "Description": "Setting min capacity to 15 and max capacity to 20", "ResourceId": "table/TestTable", "ActivityId": "3250fd06-6940-4e8e-bb1f-d494db7554d2", "StartTime": 1561574108.512, "ServiceNamespace": "dynamodb", "Cause": "scheduled action name my-first-scheduled-action was triggered", "StatusMessage": "Successfully set min capacity to 15 and max capacity to 20", "StatusCode": "Successful" } ] }

    下列是兩個排程動作皆已執行後的範例輸出。

    { "ScalingActivities": [ { "ScalableDimension": "dynamodb:table:WriteCapacityUnits", "Description": "Setting write capacity units to 10.", "ResourceId": "table/TestTable", "ActivityId": "4d1308c0-bbcf-4514-a673-b0220ae38547", "StartTime": 1561574415.086, "ServiceNamespace": "dynamodb", "EndTime": 1561574449.51, "Cause": "maximum capacity was set to 10", "StatusMessage": "Successfully set write capacity units to 10. Change successfully fulfilled by dynamodb.", "StatusCode": "Successful" }, { "ScalableDimension": "dynamodb:table:WriteCapacityUnits", "Description": "Setting min capacity to 5 and max capacity to 10", "ResourceId": "table/TestTable", "ActivityId": "f2b7847b-721d-4e01-8ef0-0c8d3bacc1c7", "StartTime": 1561574414.644, "ServiceNamespace": "dynamodb", "Cause": "scheduled action name my-second-scheduled-action was triggered", "StatusMessage": "Successfully set min capacity to 5 and max capacity to 10", "StatusCode": "Successful" }, { "ScalableDimension": "dynamodb:table:WriteCapacityUnits", "Description": "Setting write capacity units to 15.", "ResourceId": "table/TestTable", "ActivityId": "d8ea4de6-9eaa-499f-b466-2cc5e681ba8b", "StartTime": 1561574108.904, "ServiceNamespace": "dynamodb", "EndTime": 1561574140.255, "Cause": "minimum capacity was set to 15", "StatusMessage": "Successfully set write capacity units to 15. Change successfully fulfilled by dynamodb.", "StatusCode": "Successful" }, { "ScalableDimension": "dynamodb:table:WriteCapacityUnits", "Description": "Setting min capacity to 15 and max capacity to 20", "ResourceId": "table/TestTable", "ActivityId": "3250fd06-6940-4e8e-bb1f-d494db7554d2", "StartTime": 1561574108.512, "ServiceNamespace": "dynamodb", "Cause": "scheduled action name my-first-scheduled-action was triggered", "StatusMessage": "Successfully set min capacity to 15 and max capacity to 20", "StatusCode": "Successful" } ] }
  2. 成功執行排定的動作後,請開啟 DynamoDB 主控台,並選擇您要使用的資料表。在 Capacity (容量) 索引標籤下方,檢視 Write capacity units (寫入容量單位)。在第二個擴展動作執行後,寫入容量單位應會從 15 縮減為 10。

    您也可以使用以下的 describe-table 命令來驗證資料表目前的寫入容量。加上 --query 選項來篩選輸出。若要取得有關的輸出篩選功能的更多資訊 AWS CLI,請參閱《AWS Command Line Interface 使用指南》AWS CLI中的〈控制指令輸出

    Linux、macOS 或 Unix

    aws dynamodb describe-table --table-name TestTable \ --query 'Table.[TableName,TableStatus,ProvisionedThroughput]'

    Windows

    aws dynamodb describe-table --table-name TestTable --query "Table.[TableName,TableStatus,ProvisionedThroughput]"

    下列為範例輸出。

    [ "TestTable", "ACTIVE", { "NumberOfDecreasesToday": 1, "WriteCapacityUnits": 10, "LastIncreaseDateTime": 1561574133.264, "ReadCapacityUnits": 5, "LastDecreaseDateTime": 1561574435.607 } ]

步驟 4:後續步驟

如果您想試著同時使用排定擴展和擴展政策進行擴展,請按照 教學課程:設定自動擴展以處理繁重的工作負載 的步驟操作。

步驟 5:清除

當您完成入門練習之後,您可以清除關聯的資源,如下所示。

刪除排程動作

下列delete-scheduled-action命令會刪除指定的排程動作。如果您想要保留排程動作以供日後使用,您可以略過此步驟。

Linux、macOS 或 Unix

aws application-autoscaling delete-scheduled-action \ --service-namespace dynamodb \ --scalable-dimension dynamodb:table:WriteCapacityUnits \ --resource-id table/TestTable \ --scheduled-action-name my-second-scheduled-action

Windows

aws application-autoscaling delete-scheduled-action --service-namespace dynamodb --scalable-dimension dynamodb:table:WriteCapacityUnits --resource-id table/TestTable --scheduled-action-name my-second-scheduled-action
解除登錄可擴展的目標

使用下面的deregister-scalable-target命令來取消註冊可擴展的目標。如果您有任何由您建立的擴展政策或任何排程動作尚未刪除,此命令會將其全部刪除。如果您想要保留可擴展的目標以供日後使用,您可以略過此步驟。

Linux、macOS 或 Unix

aws application-autoscaling deregister-scalable-target \ --service-namespace dynamodb \ --scalable-dimension dynamodb:table:WriteCapacityUnits \ --resource-id table/TestTable

Windows

aws application-autoscaling deregister-scalable-target --service-namespace dynamodb --scalable-dimension dynamodb:table:WriteCapacityUnits --resource-id table/TestTable
若要刪除 DynamoDB 資料表

使用以下 delete-table 命令刪除您在本教學課程中用過的資料表。如果您想要保留資料表以供日後使用,您可以略過此步驟。

Linux、macOS 或 Unix

aws dynamodb delete-table --table-name TestTable

Windows

aws dynamodb delete-table --table-name TestTable