Exemples d’utilisation de l’AWS CLI avec les plans d’autoscaling - AWS Command Line Interface

Exemples d’utilisation de l’AWS CLI avec les plans d’autoscaling

Les exemples de code suivants montrent comment réaliser des actions et mettre en œuvre des scénarios courants en utilisant l’AWS Command Line Interface avec les plans d’autoscaling.

Les actions sont des extraits de code de programmes plus larges et doivent être exécutées dans leur contexte. Alors que les actions vous indiquent comment appeler des fonctions de service individuelles, vous pouvez les voir en contexte dans leurs scénarios associés.

Chaque exemple inclut un lien vers le code source complet, où vous trouverez des instructions sur la configuration et l’exécution du code en contexte.

Rubriques

Actions

L’exemple de code suivant montre comment utiliser create-scaling-plan.

AWS CLI

Pour créer un plan de mise à l’échelle

L’exemple create-scaling-plan suivant crée un plan de mise à l’échelle nommé my-scaling-plan à l’aide d’un fichier JSON déjà créé (nommé config.json). La structure du plan de mise à l’échelle inclut une instruction de mise à l’échelle pour un groupe Auto Scaling nommé my-asg. Elle spécifie la propriété TagFilters en tant que source de l’application et active le dimensionnement prédictif et dynamique.

aws autoscaling-plans create-scaling-plan \ --scaling-plan-name my-scaling-plan \ --cli-input-json file://~/config.json

Contenu du fichier config.json :

{ "ApplicationSource": { "TagFilters": [ { "Key": "purpose", "Values": [ "my-application" ] } ] }, "ScalingInstructions": [ { "ServiceNamespace": "autoscaling", "ResourceId": "autoScalingGroup/my-asg", "ScalableDimension": "autoscaling:autoScalingGroup:DesiredCapacity", "ScheduledActionBufferTime": 300, "PredictiveScalingMaxCapacityBehavior": "SetForecastCapacityToMaxCapacity", "PredictiveScalingMode": "ForecastAndScale", "PredefinedLoadMetricSpecification": { "PredefinedLoadMetricType": "ASGTotalCPUUtilization" }, "ScalingPolicyUpdateBehavior": "ReplaceExternalPolicies", "MinCapacity": 1, "MaxCapacity": 4, "TargetTrackingConfigurations": [ { "PredefinedScalingMetricSpecification": { "PredefinedScalingMetricType": "ASGAverageCPUUtilization" }, "TargetValue": 50 } ] } ] }

Sortie :

{ "ScalingPlanVersion": 1 }

Pour plus d’informations, consultez le Guide de l’utilisateur AWS Auto Scaling.

  • Pour plus de détails sur l’API, consultez CreateScalingPlan dans la Référence des commandes de l’AWS CLI.

L’exemple de code suivant montre comment utiliser delete-scaling-plan.

AWS CLI

Pour supprimer un plan de mise à l’échelle

L’exemple delete-scaling-plan suivant supprime le plan de mise à l’échelle spécifié.

aws autoscaling-plans delete-scaling-plan \ --scaling-plan-name my-scaling-plan \ --scaling-plan-version 1

Cette commande ne produit aucune sortie.

Pour plus d’informations, consultez le Guide de l’utilisateur AWS Auto Scaling.

  • Pour plus de détails sur l’API, consultez DeleteScalingPlan dans la Référence des commandes de l’AWS CLI.

L’exemple de code suivant montre comment utiliser describe-scaling-plan-resources.

AWS CLI

Pour décrire les ressources évolutives d’un plan de mise à l’échelle

L’exemple describe-scaling-plan-resources suivant affiche les informations de la ressource évolutive unique (un groupe Auto Scaling) associée au plan de mise à l’échelle spécifié.

aws autoscaling-plans describe-scaling-plan-resources \ --scaling-plan-name my-scaling-plan \ --scaling-plan-version 1

Sortie :

{ "ScalingPlanResources": [ { "ScalableDimension": "autoscaling:autoScalingGroup:DesiredCapacity", "ScalingPlanVersion": 1, "ResourceId": "autoScalingGroup/my-asg", "ScalingStatusCode": "Active", "ScalingStatusMessage": "Target tracking scaling policies have been applied to the resource.", "ScalingPolicies": [ { "PolicyName": "AutoScaling-my-asg-b1ab65ae-4be3-4634-bd64-c7471662b251", "PolicyType": "TargetTrackingScaling", "TargetTrackingConfiguration": { "PredefinedScalingMetricSpecification": { "PredefinedScalingMetricType": "ALBRequestCountPerTarget", "ResourceLabel": "app/my-alb/f37c06a68c1748aa/targetgroup/my-target-group/6d4ea56ca2d6a18d" }, "TargetValue": 40.0 } } ], "ServiceNamespace": "autoscaling", "ScalingPlanName": "my-scaling-plan" } ] }

Pour plus d’informations, consultez En quoi consiste AWS Auto Scaling ? dans le Guide de l’utilisateur AWS Auto Scaling.

L’exemple de code suivant montre comment utiliser describe-scaling-plans.

AWS CLI

Pour décrire un plan de mise à l’échelle

L’exemple describe-scaling-plans suivant affiche les informations du plan de mise à l’échelle spécifié.

aws autoscaling-plans describe-scaling-plans \ --scaling-plan-names scaling-plan-with-asg-and-ddb

Sortie :

{ "ScalingPlans": [ { "LastMutatingRequestTime": 1565388443.963, "ScalingPlanVersion": 1, "CreationTime": 1565388443.963, "ScalingInstructions": [ { "ScalingPolicyUpdateBehavior": "ReplaceExternalPolicies", "ScalableDimension": "autoscaling:autoScalingGroup:DesiredCapacity", "TargetTrackingConfigurations": [ { "PredefinedScalingMetricSpecification": { "PredefinedScalingMetricType": "ASGAverageCPUUtilization" }, "TargetValue": 50.0, "EstimatedInstanceWarmup": 300, "DisableScaleIn": false } ], "ResourceId": "autoScalingGroup/my-asg", "DisableDynamicScaling": false, "MinCapacity": 1, "ServiceNamespace": "autoscaling", "MaxCapacity": 10 }, { "ScalingPolicyUpdateBehavior": "ReplaceExternalPolicies", "ScalableDimension": "dynamodb:table:ReadCapacityUnits", "TargetTrackingConfigurations": [ { "PredefinedScalingMetricSpecification": { "PredefinedScalingMetricType": "DynamoDBReadCapacityUtilization" }, "TargetValue": 50.0, "ScaleInCooldown": 60, "DisableScaleIn": false, "ScaleOutCooldown": 60 } ], "ResourceId": "table/my-table", "DisableDynamicScaling": false, "MinCapacity": 5, "ServiceNamespace": "dynamodb", "MaxCapacity": 10000 }, { "ScalingPolicyUpdateBehavior": "ReplaceExternalPolicies", "ScalableDimension": "dynamodb:table:WriteCapacityUnits", "TargetTrackingConfigurations": [ { "PredefinedScalingMetricSpecification": { "PredefinedScalingMetricType": "DynamoDBWriteCapacityUtilization" }, "TargetValue": 50.0, "ScaleInCooldown": 60, "DisableScaleIn": false, "ScaleOutCooldown": 60 } ], "ResourceId": "table/my-table", "DisableDynamicScaling": false, "MinCapacity": 5, "ServiceNamespace": "dynamodb", "MaxCapacity": 10000 } ], "ApplicationSource": { "TagFilters": [ { "Values": [ "my-application-id" ], "Key": "application" } ] }, "StatusStartTime": 1565388455.836, "ScalingPlanName": "scaling-plan-with-asg-and-ddb", "StatusMessage": "Scaling plan has been created and applied to all resources.", "StatusCode": "Active" } ] }

Pour plus d’informations, consultez En quoi consiste AWS Auto Scaling ? dans le Guide de l’utilisateur AWS Auto Scaling.

  • Pour plus de détails sur l’API, consultez DescribeScalingPlans dans la Référence des commandes de l’AWS CLI.

L’exemple de code suivant montre comment utiliser get-scaling-plan-resource-forecast-data.

AWS CLI

Pour récupérer les données de prévision de charge

Cet exemple extrait les données de prévision de charge pour une ressource évolutive (un groupe Auto Scaling) associée au plan de mise à l’échelle spécifié.

aws autoscaling-plans get-scaling-plan-resource-forecast-data \ --scaling-plan-name my-scaling-plan \ --scaling-plan-version 1 \ --service-namespace "autoscaling" \ --resource-id autoScalingGroup/my-asg \ --scalable-dimension "autoscaling:autoScalingGroup:DesiredCapacity" \ --forecast-data-type "LoadForecast" \ --start-time "2019-08-30T00:00:00Z" \ --end-time "2019-09-06T00:00:00Z"

Sortie :

{ "Datapoints": [...] }

Pour plus d’informations, consultez En quoi consiste AWS Auto Scaling dans le Guide de l’utilisateur AWS Auto Scaling.

L’exemple de code suivant montre comment utiliser update-scaling-plan.

AWS CLI

Pour mettre à jour un plan de mise à l’échelle

L’exemple update-scaling-plan suivant modifie la métrique de mise à l’échelle d’un groupe Auto Scaling dans le plan de mise à l’échelle spécifié.

aws autoscaling-plans update-scaling-plan \ --scaling-plan-name my-scaling-plan \ --scaling-plan-version 1 \ --scaling-instructions '{"ScalableDimension":"autoscaling:autoScalingGroup:DesiredCapacity","ResourceId":"autoScalingGroup/my-asg","ServiceNamespace":"autoscaling","TargetTrackingConfigurations":[{"PredefinedScalingMetricSpecification": {"PredefinedScalingMetricType":"ALBRequestCountPerTarget","ResourceLabel":"app/my-alb/f37c06a68c1748aa/targetgroup/my-target-group/6d4ea56ca2d6a18d"},"TargetValue":40.0}],"MinCapacity": 1,"MaxCapacity": 10}'

Cette commande ne produit aucune sortie.

Pour plus d’informations, consultez En quoi consiste AWS Auto Scaling ? dans le Guide de l’utilisateur AWS Auto Scaling.

  • Pour plus de détails sur l’API, consultez UpdateScalingPlan dans la Référence des commandes de l’AWS CLI.