There are more AWS SDK examples available in the AWS Doc SDK Examples
Use UpdateAutoScalingGroup
with an AWS SDK or CLI
The following code examples show how to use UpdateAutoScalingGroup
.
Action examples are code excerpts from larger programs and must be run in context. You can see this action in context in the following code examples:
- .NET
-
- AWS SDK for .NET
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. /// <summary> /// Update the capacity of an Auto Scaling group. /// </summary> /// <param name="groupName">The name of the Auto Scaling group.</param> /// <param name="launchTemplateName">The name of the EC2 launch template.</param> /// <param name="maxSize">The maximum number of instances that can be /// created for the Auto Scaling group.</param> /// <returns>A Boolean value indicating the success of the action.</returns> public async Task<bool> UpdateAutoScalingGroupAsync( string groupName, string launchTemplateName, int maxSize) { var templateSpecification = new LaunchTemplateSpecification { LaunchTemplateName = launchTemplateName, }; var groupRequest = new UpdateAutoScalingGroupRequest { MaxSize = maxSize, AutoScalingGroupName = groupName, LaunchTemplate = templateSpecification, }; var response = await _amazonAutoScaling.UpdateAutoScalingGroupAsync(groupRequest); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine($"You successfully updated the Auto Scaling group {groupName}."); return true; } else { return false; } }
-
For API details, see UpdateAutoScalingGroup in AWS SDK for .NET API Reference.
-
- C++
-
- SDK for C++
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. 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::UpdateAutoScalingGroupRequest request; request.SetAutoScalingGroupName(groupName); request.SetMaxSize(3); Aws::AutoScaling::Model::UpdateAutoScalingGroupOutcome outcome = autoScalingClient.UpdateAutoScalingGroup(request); if (!outcome.IsSuccess()) { std::cerr << "Error with AutoScaling::UpdateAutoScalingGroup. " << outcome.GetError().GetMessage() << std::endl; }
-
For API details, see UpdateAutoScalingGroup in AWS SDK for C++ API Reference.
-
- CLI
-
- AWS CLI
-
Example 1: To update the size limits of an Auto Scaling group
This example updates the specified Auto Scaling group with a minimum size of 2 and a maximum size of 10.
aws autoscaling update-auto-scaling-group \ --auto-scaling-group-name
my-asg
\ --min-size2
\ --max-size10
This command produces no output.
For more information, see Setting capacity limits for your Auto Scaling group in the Amazon EC2 Auto Scaling User Guide.
Example 2: To add Elastic Load Balancing health checks and specify which Availability Zones and subnets to use
This example updates the specified Auto Scaling group to add Elastic Load Balancing health checks. This command also updates the value of
--vpc-zone-identifier
with a list of subnet IDs in multiple Availability Zones.aws autoscaling update-auto-scaling-group \ --auto-scaling-group-name
my-asg
\ --health-check-typeELB
\ --health-check-grace-period600
\ --vpc-zone-identifier"subnet-5ea0c127,subnet-6194ea3b,subnet-c934b782"
This command produces no output.
For more information, see Elastic Load Balancing and Amazon EC2 Auto Scaling in the Amazon EC2 Auto Scaling User Guide.
Example 3: To update the placement group and termination policy
This example updates the placement group and termination policy to use.
aws autoscaling update-auto-scaling-group \ --auto-scaling-group-name
my-asg
\ --placement-groupmy-placement-group
\ --termination-policies"OldestInstance"
This command produces no output.
For more information, see Auto Scaling groups in the Amazon EC2 Auto Scaling User Guide.
Example 4: To use the latest version of the launch template
This example updates the specified Auto Scaling group to use the latest version of the specified launch template.
aws autoscaling update-auto-scaling-group \ --auto-scaling-group-name
my-asg
\ --launch-template LaunchTemplateId=lt-1234567890abcde12,Version='$Latest'This command produces no output.
For more information, see Launch templates in the Amazon EC2 Auto Scaling User Guide.
Example 5: To use a specific version of the launch template
This example updates the specified Auto Scaling group to use a specific version of a launch template instead of the latest or default version.
aws autoscaling update-auto-scaling-group \ --auto-scaling-group-name
my-asg
\ --launch-template LaunchTemplateName=my-template-for-auto-scaling,Version='2'This command produces no output.
For more information, see Launch templates in the Amazon EC2 Auto Scaling User Guide.
Example 6: To define a mixed instances policy and enable capacity rebalancing
This example updates the specified Auto Scaling group to use a mixed instances policy and enables capacity rebalancing. This structure lets you specify groups with Spot and On-Demand capacities and use different launch templates for different architectures.
aws autoscaling update-auto-scaling-group \ --cli-input-json
file://~/config.json
Contents of
config.json
:{ "AutoScalingGroupName": "my-asg", "CapacityRebalance": true, "MixedInstancesPolicy": { "LaunchTemplate": { "LaunchTemplateSpecification": { "LaunchTemplateName": "my-launch-template-for-x86", "Version": "$Latest" }, "Overrides": [ { "InstanceType": "c6g.large", "LaunchTemplateSpecification": { "LaunchTemplateName": "my-launch-template-for-arm", "Version": "$Latest" } }, { "InstanceType": "c5.large" }, { "InstanceType": "c5a.large" } ] }, "InstancesDistribution": { "OnDemandPercentageAboveBaseCapacity": 50, "SpotAllocationStrategy": "capacity-optimized" } } }
This command produces no output.
For more information, see Auto Scaling groups with multiple instance types and purchase options in the Amazon EC2 Auto Scaling User Guide.
-
For API details, see UpdateAutoScalingGroup
in AWS CLI Command Reference.
-
- Java
-
- SDK for Java 2.x
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. public static void updateAutoScalingGroup(AutoScalingClient autoScalingClient, String groupName, String launchTemplateName) { try { AutoScalingWaiter waiter = autoScalingClient.waiter(); LaunchTemplateSpecification templateSpecification = LaunchTemplateSpecification.builder() .launchTemplateName(launchTemplateName) .build(); UpdateAutoScalingGroupRequest groupRequest = UpdateAutoScalingGroupRequest.builder() .maxSize(3) .autoScalingGroupName(groupName) .launchTemplate(templateSpecification) .build(); autoScalingClient.updateAutoScalingGroup(groupRequest); DescribeAutoScalingGroupsRequest groupsRequest = DescribeAutoScalingGroupsRequest.builder() .autoScalingGroupNames(groupName) .build(); WaiterResponse<DescribeAutoScalingGroupsResponse> waiterResponse = waiter .waitUntilGroupInService(groupsRequest); waiterResponse.matched().response().ifPresent(System.out::println); System.out.println("You successfully updated the auto scaling group " + groupName); } catch (AutoScalingException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
-
For API details, see UpdateAutoScalingGroup in AWS SDK for Java 2.x API Reference.
-
- Kotlin
-
- SDK for Kotlin
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. suspend fun updateAutoScalingGroup( groupName: String, launchTemplateNameVal: String, serviceLinkedRoleARNVal: String, ) { val templateSpecification = LaunchTemplateSpecification { launchTemplateName = launchTemplateNameVal } val groupRequest = UpdateAutoScalingGroupRequest { maxSize = 3 serviceLinkedRoleArn = serviceLinkedRoleARNVal autoScalingGroupName = groupName launchTemplate = templateSpecification } val groupsRequestWaiter = DescribeAutoScalingGroupsRequest { autoScalingGroupNames = listOf(groupName) } AutoScalingClient { region = "us-east-1" }.use { autoScalingClient -> autoScalingClient.updateAutoScalingGroup(groupRequest) autoScalingClient.waitUntilGroupExists(groupsRequestWaiter) println("You successfully updated the Auto Scaling group $groupName") } }
-
For API details, see UpdateAutoScalingGroup
in AWS SDK for Kotlin API reference.
-
- PHP
-
- SDK for PHP
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. public function updateAutoScalingGroup($autoScalingGroupName, $args) { if (array_key_exists('MaxSize', $args)) { $maxSize = ['MaxSize' => $args['MaxSize']]; } else { $maxSize = []; } if (array_key_exists('MinSize', $args)) { $minSize = ['MinSize' => $args['MinSize']]; } else { $minSize = []; } $parameters = ['AutoScalingGroupName' => $autoScalingGroupName]; $parameters = array_merge($parameters, $minSize, $maxSize); return $this->autoScalingClient->updateAutoScalingGroup($parameters); }
-
For API details, see UpdateAutoScalingGroup in AWS SDK for PHP API Reference.
-
- PowerShell
-
- Tools for PowerShell
-
Example 1: This example updates the minimum and maximum size of the specified Auto Scaling group.
Update-ASAutoScalingGroup -AutoScalingGroupName my-asg -MaxSize 5 -MinSize 1
Example 2: This example updates the default cooldown period of the specified Auto Scaling group.
Update-ASAutoScalingGroup -AutoScalingGroupName my-asg -DefaultCooldown 10
Example 3: This example updates the Availability Zones of the specified Auto Scaling group.
Update-ASAutoScalingGroup -AutoScalingGroupName my-asg -AvailabilityZone @("us-west-2a", "us-west-2b")
Example 4: This example updates the specified Auto Scaling group to use Elastic Load Balancing health checks.
Update-ASAutoScalingGroup -AutoScalingGroupName my-asg -HealthCheckType ELB -HealthCheckGracePeriod 60
-
For API details, see UpdateAutoScalingGroup in AWS Tools for PowerShell Cmdlet Reference.
-
- Python
-
- SDK for Python (Boto3)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. 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 update_group(self, group_name: str, **kwargs: Any) -> None: """ Updates an Auto Scaling group. :param group_name: The name of the group to update. :param kwargs: Keyword arguments to pass through to the service. :return: None :raises ClientError: If there is an error updating the Auto Scaling group. """ try: self.autoscaling_client.update_auto_scaling_group( AutoScalingGroupName=group_name, **kwargs ) logger.info(f"Successfully updated Auto Scaling group {group_name}.") except ClientError as err: error_code = err.response["Error"]["Code"] logger.error(f"Failed to update Auto Scaling group {group_name}.") if error_code == "ResourceInUse": logger.error( "The Auto Scaling group '%s' is currently in use and cannot be modified. Please try again later.", group_name, ) elif 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 update the group." ) logger.error(f"Full error:\n\t{err}") raise
-
For API details, see UpdateAutoScalingGroup in AWS SDK for Python (Boto3) API Reference.
-
- Rust
-
- SDK for Rust
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. async fn update_group(client: &Client, name: &str, size: i32) -> Result<(), Error> { client .update_auto_scaling_group() .auto_scaling_group_name(name) .max_size(size) .send() .await?; println!("Updated AutoScaling group"); Ok(()) }
-
For API details, see UpdateAutoScalingGroup
in AWS SDK for Rust API reference.
-