教程:通过 Amazon CLI开始使用计划扩缩 - Application Auto Scaling
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

教程:通过 Amazon CLI开始使用计划扩缩

以下教程向您展示了如何通过帮助您创建计划操作 Amazon CLI 来扩展名为的 DynamoDB 表的示例,从而开始计划扩展。TestTable如果您的 DynamoDB 中没有用于测试的 TestTable 表,则可以通过运行 Amazon DynamoDB 开发人员指南中的步骤 1:创建 DynamoDB 表中所示的 create-table 命令来立即创建 一个。

使用时 Amazon CLI,请记住您的命令在为您的个人资料配置的 Amazon 区域中运行。如果您想要在不同的区域中运行命令,可以为配置文件更改默认区域,或者与命令一起使用 --region 参数。

注意

作为本教程的一部分,您可能会产生 Amazon 费用。请监控免费套餐使用情况,并确保您了解与 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 命令计划在将来的指定日期和时间要运行一次的操作。小时采用世界标准时间 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 选项以筛选输出。有关输出筛选功能的更多信息 Amazon CLI,请参阅《Amazon Command Line Interface 用户指南》 Amazon 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