搭RegisterScalableTarget配 AWS SDK或使用 CLI - AWS SDK 程式碼範例

AWS 文檔 AWS SDK示例 GitHub 回購中有更多SDK示例

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

RegisterScalableTarget配 AWS SDK或使用 CLI

下列程式碼範例會示範如何使用RegisterScalableTarget

CLI
AWS CLI

範例 1:將ECS服務註冊為可擴充的目標

下列register-scalable-target範例會使 Application Auto Scaling 註冊 Amazon ECS 服務。它還會將包含索引鍵名稱environment和值的標籤新增production至可擴充目標。

aws application-autoscaling register-scalable-target \ --service-namespace ecs \ --scalable-dimension ecs:service:DesiredCount \ --resource-id service/default/web-app \ --min-capacity 1 --max-capacity 10 \ --tags environment=production

輸出:

{ "ScalableTargetARN": "arn:aws:application-autoscaling:us-west-2:123456789012:scalable-target/1234abcd56ab78cd901ef1234567890ab123" }

如需其他 AWS 服務和自訂資源的範例,請參閱《應用程式 Auto Scaling 使用者指南》中可與 Application Auto Scaling 搭配使用的AWS 服務中的主題。

範例 2:暫停可擴展目標的擴展活動

下列register-scalable-target範例會暫停現有可調整目標的擴展活動。

aws application-autoscaling register-scalable-target \ --service-namespace dynamodb \ --scalable-dimension dynamodb:table:ReadCapacityUnits \ --resource-id table/my-table \ --suspended-state DynamicScalingInSuspended=true,DynamicScalingOutSuspended=true,ScheduledScalingSuspended=true

輸出:

{ "ScalableTargetARN": "arn:aws:application-autoscaling:us-west-2:123456789012:scalable-target/1234abcd56ab78cd901ef1234567890ab123" }

如需詳細資訊,請參閱《應用 Application Auto Scaling 整規模使用指南》中的暫停和繼續應用程式自動調整比例

範例 3:繼續調整可擴展目標的調整活動

下列register-scalable-target範例會繼續現有可調整目標的擴展活動。

aws application-autoscaling register-scalable-target \ --service-namespace dynamodb \ --scalable-dimension dynamodb:table:ReadCapacityUnits \ --resource-id table/my-table \ --suspended-state DynamicScalingInSuspended=false,DynamicScalingOutSuspended=false,ScheduledScalingSuspended=false

輸出:

{ "ScalableTargetARN": "arn:aws:application-autoscaling:us-west-2:123456789012:scalable-target/1234abcd56ab78cd901ef1234567890ab123" }

如需詳細資訊,請參閱《應用 Application Auto Scaling 整規模使用指南》中的暫停和繼續應用程式自動調整比例

Java
SDK對於爪哇 2.x
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.applicationautoscaling.ApplicationAutoScalingClient; import software.amazon.awssdk.services.applicationautoscaling.model.ApplicationAutoScalingException; import software.amazon.awssdk.services.applicationautoscaling.model.DescribeScalableTargetsRequest; import software.amazon.awssdk.services.applicationautoscaling.model.DescribeScalableTargetsResponse; import software.amazon.awssdk.services.applicationautoscaling.model.DescribeScalingPoliciesRequest; import software.amazon.awssdk.services.applicationautoscaling.model.DescribeScalingPoliciesResponse; import software.amazon.awssdk.services.applicationautoscaling.model.PolicyType; import software.amazon.awssdk.services.applicationautoscaling.model.PredefinedMetricSpecification; import software.amazon.awssdk.services.applicationautoscaling.model.PutScalingPolicyRequest; import software.amazon.awssdk.services.applicationautoscaling.model.RegisterScalableTargetRequest; import software.amazon.awssdk.services.applicationautoscaling.model.ScalingPolicy; import software.amazon.awssdk.services.applicationautoscaling.model.ServiceNamespace; import software.amazon.awssdk.services.applicationautoscaling.model.ScalableDimension; import software.amazon.awssdk.services.applicationautoscaling.model.MetricType; import software.amazon.awssdk.services.applicationautoscaling.model.TargetTrackingScalingPolicyConfiguration; import java.util.List; /** * Before running this Java V2 code example, set up your development environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class EnableDynamoDBAutoscaling { public static void main(String[] args) { final String usage = """ Usage: <tableId> <roleARN> <policyName>\s Where: tableId - The table Id value (for example, table/Music). roleARN - The ARN of the role that has ApplicationAutoScaling permissions. policyName - The name of the policy to create. """; if (args.length != 3) { System.out.println(usage); System.exit(1); } System.out.println("This example registers an Amazon DynamoDB table, which is the resource to scale."); String tableId = args[0]; String roleARN = args[1]; String policyName = args[2]; ServiceNamespace ns = ServiceNamespace.DYNAMODB; ScalableDimension tableWCUs = ScalableDimension.DYNAMODB_TABLE_WRITE_CAPACITY_UNITS; ApplicationAutoScalingClient appAutoScalingClient = ApplicationAutoScalingClient.builder() .region(Region.US_EAST_1) .build(); registerScalableTarget(appAutoScalingClient, tableId, roleARN, ns, tableWCUs); verifyTarget(appAutoScalingClient, tableId, ns, tableWCUs); configureScalingPolicy(appAutoScalingClient, tableId, ns, tableWCUs, policyName); } public static void registerScalableTarget(ApplicationAutoScalingClient appAutoScalingClient, String tableId, String roleARN, ServiceNamespace ns, ScalableDimension tableWCUs) { try { RegisterScalableTargetRequest targetRequest = RegisterScalableTargetRequest.builder() .serviceNamespace(ns) .scalableDimension(tableWCUs) .resourceId(tableId) .roleARN(roleARN) .minCapacity(5) .maxCapacity(10) .build(); appAutoScalingClient.registerScalableTarget(targetRequest); System.out.println("You have registered " + tableId); } catch (ApplicationAutoScalingException e) { System.err.println(e.awsErrorDetails().errorMessage()); } } // Verify that the target was created. public static void verifyTarget(ApplicationAutoScalingClient appAutoScalingClient, String tableId, ServiceNamespace ns, ScalableDimension tableWCUs) { DescribeScalableTargetsRequest dscRequest = DescribeScalableTargetsRequest.builder() .scalableDimension(tableWCUs) .serviceNamespace(ns) .resourceIds(tableId) .build(); DescribeScalableTargetsResponse response = appAutoScalingClient.describeScalableTargets(dscRequest); System.out.println("DescribeScalableTargets result: "); System.out.println(response); } // Configure a scaling policy. public static void configureScalingPolicy(ApplicationAutoScalingClient appAutoScalingClient, String tableId, ServiceNamespace ns, ScalableDimension tableWCUs, String policyName) { // Check if the policy exists before creating a new one. DescribeScalingPoliciesResponse describeScalingPoliciesResponse = appAutoScalingClient.describeScalingPolicies(DescribeScalingPoliciesRequest.builder() .serviceNamespace(ns) .resourceId(tableId) .scalableDimension(tableWCUs) .build()); if (!describeScalingPoliciesResponse.scalingPolicies().isEmpty()) { // If policies exist, consider updating an existing policy instead of creating a new one. System.out.println("Policy already exists. Consider updating it instead."); List<ScalingPolicy> polList = describeScalingPoliciesResponse.scalingPolicies(); for (ScalingPolicy pol : polList) { System.out.println("Policy name:" +pol.policyName()); } } else { // If no policies exist, proceed with creating a new policy. PredefinedMetricSpecification specification = PredefinedMetricSpecification.builder() .predefinedMetricType(MetricType.DYNAMO_DB_WRITE_CAPACITY_UTILIZATION) .build(); TargetTrackingScalingPolicyConfiguration policyConfiguration = TargetTrackingScalingPolicyConfiguration.builder() .predefinedMetricSpecification(specification) .targetValue(50.0) .scaleInCooldown(60) .scaleOutCooldown(60) .build(); PutScalingPolicyRequest putScalingPolicyRequest = PutScalingPolicyRequest.builder() .targetTrackingScalingPolicyConfiguration(policyConfiguration) .serviceNamespace(ns) .scalableDimension(tableWCUs) .resourceId(tableId) .policyName(policyName) .policyType(PolicyType.TARGET_TRACKING_SCALING) .build(); try { appAutoScalingClient.putScalingPolicy(putScalingPolicyRequest); System.out.println("You have successfully created a scaling policy for an Application Auto Scaling scalable target"); } catch (ApplicationAutoScalingException e) { System.err.println("Error: " + e.awsErrorDetails().errorMessage()); } } } }
PowerShell
適用的工具 PowerShell

範例 1:此指令程式會註冊或更新可調整的目標。可擴充的目標是 Application Auto Scaling 規模可以向外擴充和擴充的資源。

Add-AASScalableTarget -ServiceNamespace AppStream -ResourceId fleet/MyFleet -ScalableDimension appstream:fleet:DesiredCapacity -MinCapacity 2 -MaxCapacity 10