与 AWS SDK或SetDesiredCapacity一起使用 CLI - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

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

与 AWS SDK或SetDesiredCapacity一起使用 CLI

以下代码示例演示如何使用 SetDesiredCapacity

操作示例是大型程序的代码摘录,必须在上下文中运行。在以下代码示例中,您可以查看此操作的上下文:

.NET
AWS SDK for .NET
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

/// <summary> /// Set the desired capacity of an Auto Scaling group. /// </summary> /// <param name="groupName">The name of the Auto Scaling group.</param> /// <param name="desiredCapacity">The desired capacity for the Auto /// Scaling group.</param> /// <returns>A Boolean value indicating the success of the action.</returns> public async Task<bool> SetDesiredCapacityAsync( string groupName, int desiredCapacity) { var capacityRequest = new SetDesiredCapacityRequest { AutoScalingGroupName = groupName, DesiredCapacity = desiredCapacity, }; var response = await _amazonAutoScaling.SetDesiredCapacityAsync(capacityRequest); Console.WriteLine($"You have set the DesiredCapacity to {desiredCapacity}."); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; }
  • 有关API详细信息,请参阅 “AWS SDK for .NET API参考 SetDesiredCapacity” 中的。

C++
SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::AutoScaling::AutoScalingClient autoScalingClient(clientConfig); Aws::AutoScaling::Model::SetDesiredCapacityRequest request; request.SetAutoScalingGroupName(groupName); request.SetDesiredCapacity(2); Aws::AutoScaling::Model::SetDesiredCapacityOutcome outcome = autoScalingClient.SetDesiredCapacity(request); if (!outcome.IsSuccess()) { std::cerr << "Error with AutoScaling::SetDesiredCapacityRequest. " << outcome.GetError().GetMessage() << std::endl; }
  • 有关API详细信息,请参阅 “AWS SDK for C++ API参考 SetDesiredCapacity” 中的。

CLI
AWS CLI

为自动扩缩组设置所需容量

此示例将为指定的自动扩缩组设置所需容量。

aws autoscaling set-desired-capacity \ --auto-scaling-group-name my-asg \ --desired-capacity 2 \ --honor-cooldown

如果成功,该命令将返回到提示符。

Java
SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

public static void setDesiredCapacity(AutoScalingClient autoScalingClient, String groupName) { try { SetDesiredCapacityRequest capacityRequest = SetDesiredCapacityRequest.builder() .autoScalingGroupName(groupName) .desiredCapacity(2) .build(); autoScalingClient.setDesiredCapacity(capacityRequest); System.out.println("You have set the DesiredCapacity to 2"); } catch (AutoScalingException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • 有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 SetDesiredCapacity” 中的。

Kotlin
SDK对于 Kotlin 来说
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

suspend fun setDesiredCapacity(groupName: String) { val capacityRequest = SetDesiredCapacityRequest { autoScalingGroupName = groupName desiredCapacity = 2 } AutoScalingClient { region = "us-east-1" }.use { autoScalingClient -> autoScalingClient.setDesiredCapacity(capacityRequest) println("You set the DesiredCapacity to 2") } }
PHP
SDK for PHP
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

public function setDesiredCapacity($autoScalingGroupName, $desiredCapacity) { return $this->autoScalingClient->setDesiredCapacity([ 'AutoScalingGroupName' => $autoScalingGroupName, 'DesiredCapacity' => $desiredCapacity, ]); }
  • 有关API详细信息,请参阅 “AWS SDK for PHP API参考 SetDesiredCapacity” 中的。

PowerShell
用于 PowerShell

示例 1:此示例设置指定自动扩缩组的大小。

Set-ASDesiredCapacity -AutoScalingGroupName my-asg -DesiredCapacity 2

示例 2:此示例设置指定自动扩缩组的大小,并等待冷却时间结束后再扩缩到新大小。

Set-ASDesiredCapacity -AutoScalingGroupName my-asg -DesiredCapacity 2 -HonorCooldown $true
  • 有关API详细信息,请参阅 AWS Tools for PowerShell Cmdlet 参考SetDesiredCapacity中的。

Python
SDK适用于 Python (Boto3)
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

class AutoScalingWrapper: """Encapsulates Amazon EC2 Auto Scaling actions.""" def __init__(self, autoscaling_client): """ :param autoscaling_client: A Boto3 Amazon EC2 Auto Scaling client. """ self.autoscaling_client = autoscaling_client def set_desired_capacity(self, group_name: str, capacity: int) -> None: """ Sets the desired capacity of the group. Amazon EC2 Auto Scaling tries to keep the number of running instances equal to the desired capacity. :param group_name: The name of the group to update. :param capacity: The desired number of running instances. :return: None :raises ClientError: If there is an error setting the desired capacity. """ try: self.autoscaling_client.set_desired_capacity( AutoScalingGroupName=group_name, DesiredCapacity=capacity, HonorCooldown=False, ) logger.info( f"Successfully set desired capacity of {capacity} for Auto Scaling group '{group_name}'." ) except ClientError as err: error_code = err.response["Error"]["Code"] logger.error( f"Failed to set desired capacity for Auto Scaling group '{group_name}'." ) if error_code == "ScalingActivityInProgress": logger.error( f"A scaling activity is currently in progress for the Auto Scaling group '{group_name}'. " "Please wait for the activity to complete before attempting to set the desired capacity." ) logger.error(f"Full error:\n\t{err}") raise
  • 有关API详细信息,请参阅SetDesiredCapacity中的 AWS SDKPython (Boto3) API 参考。

Rust
SDK对于 Rust
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

pub async fn scale_desired_capacity(&self, capacity: i32) -> Result<(), ScenarioError> { // 7. SetDesiredCapacity: set desired capacity to 2. // Wait for a second instance to launch. let update_group = self .autoscaling .set_desired_capacity() .auto_scaling_group_name(self.auto_scaling_group_name.clone()) .desired_capacity(capacity) .send() .await; if let Err(err) = update_group { return Err(ScenarioError::new( format!("Failed to update group to desired capacity ({capacity}))").as_str(), &err, )); } Ok(()) }