Auto Scaling examples using SDK for Kotlin - AWS SDK for Kotlin

Auto Scaling examples using SDK for Kotlin

The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for Kotlin with Auto Scaling.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios and cross-service examples.

Scenarios are code examples that show you how to accomplish a specific task by calling multiple functions within the same service.

Each example includes a link to GitHub, where you can find instructions on how to set up and run the code in context.

Actions

The following code example shows how to use CreateAutoScalingGroup.

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 createAutoScalingGroup( groupName: String, launchTemplateNameVal: String, serviceLinkedRoleARNVal: String, vpcZoneIdVal: String, ) { val templateSpecification = LaunchTemplateSpecification { launchTemplateName = launchTemplateNameVal } val request = CreateAutoScalingGroupRequest { autoScalingGroupName = groupName availabilityZones = listOf("us-east-1a") launchTemplate = templateSpecification maxSize = 1 minSize = 1 vpcZoneIdentifier = vpcZoneIdVal serviceLinkedRoleArn = serviceLinkedRoleARNVal } // This object is required for the waiter call. val groupsRequestWaiter = DescribeAutoScalingGroupsRequest { autoScalingGroupNames = listOf(groupName) } AutoScalingClient { region = "us-east-1" }.use { autoScalingClient -> autoScalingClient.createAutoScalingGroup(request) autoScalingClient.waitUntilGroupExists(groupsRequestWaiter) println("$groupName was created!") } }

The following code example shows how to use DeleteAutoScalingGroup.

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 deleteSpecificAutoScalingGroup(groupName: String) { val deleteAutoScalingGroupRequest = DeleteAutoScalingGroupRequest { autoScalingGroupName = groupName forceDelete = true } AutoScalingClient { region = "us-east-1" }.use { autoScalingClient -> autoScalingClient.deleteAutoScalingGroup(deleteAutoScalingGroupRequest) println("You successfully deleted $groupName") } }

The following code example shows how to use DescribeAutoScalingGroups.

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 getAutoScalingGroups(groupName: String) { val scalingGroupsRequest = DescribeAutoScalingGroupsRequest { autoScalingGroupNames = listOf(groupName) } AutoScalingClient { region = "us-east-1" }.use { autoScalingClient -> val response = autoScalingClient.describeAutoScalingGroups(scalingGroupsRequest) response.autoScalingGroups?.forEach { group -> println("The group name is ${group.autoScalingGroupName}") println("The group ARN is ${group.autoScalingGroupArn}") group.instances?.forEach { instance -> println("The instance id is ${instance.instanceId}") println("The lifecycle state is " + instance.lifecycleState) } } } }

The following code example shows how to use DescribeAutoScalingInstances.

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 describeAutoScalingInstance(id: String) { val describeAutoScalingInstancesRequest = DescribeAutoScalingInstancesRequest { instanceIds = listOf(id) } AutoScalingClient { region = "us-east-1" }.use { autoScalingClient -> val response = autoScalingClient.describeAutoScalingInstances(describeAutoScalingInstancesRequest) response.autoScalingInstances?.forEach { group -> println("The instance lifecycle state is: ${group.lifecycleState}") } } }

The following code example shows how to use DescribeScalingActivities.

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 describeAutoScalingGroups(groupName: String) { val groupsReques = DescribeAutoScalingGroupsRequest { autoScalingGroupNames = listOf(groupName) maxRecords = 10 } AutoScalingClient { region = "us-east-1" }.use { autoScalingClient -> val response = autoScalingClient.describeAutoScalingGroups(groupsReques) response.autoScalingGroups?.forEach { group -> println("The service to use for the health checks: ${group.healthCheckType}") } } }

The following code example shows how to use DisableMetricsCollection.

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 disableMetricsCollection(groupName: String) { val disableMetricsCollectionRequest = DisableMetricsCollectionRequest { autoScalingGroupName = groupName metrics = listOf("GroupMaxSize") } AutoScalingClient { region = "us-east-1" }.use { autoScalingClient -> autoScalingClient.disableMetricsCollection(disableMetricsCollectionRequest) println("The disable metrics collection operation was successful") } }

The following code example shows how to use EnableMetricsCollection.

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 enableMetricsCollection(groupName: String?) { val collectionRequest = EnableMetricsCollectionRequest { autoScalingGroupName = groupName metrics = listOf("GroupMaxSize") granularity = "1Minute" } AutoScalingClient { region = "us-east-1" }.use { autoScalingClient -> autoScalingClient.enableMetricsCollection(collectionRequest) println("The enable metrics collection operation was successful") } }

The following code example shows how to use SetDesiredCapacity.

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 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") } }

The following code example shows how to use TerminateInstanceInAutoScalingGroup.

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 terminateInstanceInAutoScalingGroup(instanceIdVal: String) { val request = TerminateInstanceInAutoScalingGroupRequest { instanceId = instanceIdVal shouldDecrementDesiredCapacity = false } AutoScalingClient { region = "us-east-1" }.use { autoScalingClient -> autoScalingClient.terminateInstanceInAutoScalingGroup(request) println("You have terminated instance $instanceIdVal") } }

The following code example shows how to use UpdateAutoScalingGroup.

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") } }

Scenarios

The following code example shows how to:

  • Create an Amazon EC2 Auto Scaling group with a launch template and Availability Zones, and get information about running instances.

  • Enable Amazon CloudWatch metrics collection.

  • Update the group's desired capacity and wait for an instance to start.

  • Terminate an instance in the group.

  • List scaling activities that occur in response to user requests and capacity changes.

  • Get statistics for CloudWatch metrics, then clean up resources.

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 main(args: Array<String>) { val usage = """ Usage: <groupName> <launchTemplateName> <serviceLinkedRoleARN> <vpcZoneId> Where: groupName - The name of the Auto Scaling group. launchTemplateName - The name of the launch template. serviceLinkedRoleARN - The Amazon Resource Name (ARN) of the service-linked role that the Auto Scaling group uses. vpcZoneId - A subnet Id for a virtual private cloud (VPC) where instances in the Auto Scaling group can be created. """ if (args.size != 4) { println(usage) exitProcess(1) } val groupName = args[0] val launchTemplateName = args[1] val serviceLinkedRoleARN = args[2] val vpcZoneId = args[3] println("**** Create an Auto Scaling group named $groupName") createAutoScalingGroup(groupName, launchTemplateName, serviceLinkedRoleARN, vpcZoneId) println("Wait 1 min for the resources, including the instance. Otherwise, an empty instance Id is returned") delay(60000) val instanceId = getSpecificAutoScaling(groupName) if (instanceId.compareTo("") == 0) { println("Error - no instance Id value") exitProcess(1) } else { println("The instance Id value is $instanceId") } println("**** Describe Auto Scaling with the Id value $instanceId") describeAutoScalingInstance(instanceId) println("**** Enable metrics collection $instanceId") enableMetricsCollection(groupName) println("**** Update an Auto Scaling group to maximum size of 3") updateAutoScalingGroup(groupName, launchTemplateName, serviceLinkedRoleARN) println("**** Describe all Auto Scaling groups to show the current state of the groups") describeAutoScalingGroups(groupName) println("**** Describe account details") describeAccountLimits() println("Wait 1 min for the resources, including the instance. Otherwise, an empty instance Id is returned") delay(60000) println("**** Set desired capacity to 2") setDesiredCapacity(groupName) println("**** Get the two instance Id values and state") getAutoScalingGroups(groupName) println("**** List the scaling activities that have occurred for the group") describeScalingActivities(groupName) println("**** Terminate an instance in the Auto Scaling group") terminateInstanceInAutoScalingGroup(instanceId) println("**** Stop the metrics collection") disableMetricsCollection(groupName) println("**** Delete the Auto Scaling group") deleteSpecificAutoScalingGroup(groupName) } suspend fun describeAutoScalingGroups(groupName: String) { val groupsReques = DescribeAutoScalingGroupsRequest { autoScalingGroupNames = listOf(groupName) maxRecords = 10 } AutoScalingClient { region = "us-east-1" }.use { autoScalingClient -> val response = autoScalingClient.describeAutoScalingGroups(groupsReques) response.autoScalingGroups?.forEach { group -> println("The service to use for the health checks: ${group.healthCheckType}") } } } suspend fun disableMetricsCollection(groupName: String) { val disableMetricsCollectionRequest = DisableMetricsCollectionRequest { autoScalingGroupName = groupName metrics = listOf("GroupMaxSize") } AutoScalingClient { region = "us-east-1" }.use { autoScalingClient -> autoScalingClient.disableMetricsCollection(disableMetricsCollectionRequest) println("The disable metrics collection operation was successful") } } suspend fun describeScalingActivities(groupName: String?) { val scalingActivitiesRequest = DescribeScalingActivitiesRequest { autoScalingGroupName = groupName maxRecords = 10 } AutoScalingClient { region = "us-east-1" }.use { autoScalingClient -> val response = autoScalingClient.describeScalingActivities(scalingActivitiesRequest) response.activities?.forEach { activity -> println("The activity Id is ${activity.activityId}") println("The activity details are ${activity.details}") } } } suspend fun getAutoScalingGroups(groupName: String) { val scalingGroupsRequest = DescribeAutoScalingGroupsRequest { autoScalingGroupNames = listOf(groupName) } AutoScalingClient { region = "us-east-1" }.use { autoScalingClient -> val response = autoScalingClient.describeAutoScalingGroups(scalingGroupsRequest) response.autoScalingGroups?.forEach { group -> println("The group name is ${group.autoScalingGroupName}") println("The group ARN is ${group.autoScalingGroupArn}") group.instances?.forEach { instance -> println("The instance id is ${instance.instanceId}") println("The lifecycle state is " + instance.lifecycleState) } } } } 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") } } 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") } } suspend fun createAutoScalingGroup( groupName: String, launchTemplateNameVal: String, serviceLinkedRoleARNVal: String, vpcZoneIdVal: String, ) { val templateSpecification = LaunchTemplateSpecification { launchTemplateName = launchTemplateNameVal } val request = CreateAutoScalingGroupRequest { autoScalingGroupName = groupName availabilityZones = listOf("us-east-1a") launchTemplate = templateSpecification maxSize = 1 minSize = 1 vpcZoneIdentifier = vpcZoneIdVal serviceLinkedRoleArn = serviceLinkedRoleARNVal } // This object is required for the waiter call. val groupsRequestWaiter = DescribeAutoScalingGroupsRequest { autoScalingGroupNames = listOf(groupName) } AutoScalingClient { region = "us-east-1" }.use { autoScalingClient -> autoScalingClient.createAutoScalingGroup(request) autoScalingClient.waitUntilGroupExists(groupsRequestWaiter) println("$groupName was created!") } } suspend fun describeAutoScalingInstance(id: String) { val describeAutoScalingInstancesRequest = DescribeAutoScalingInstancesRequest { instanceIds = listOf(id) } AutoScalingClient { region = "us-east-1" }.use { autoScalingClient -> val response = autoScalingClient.describeAutoScalingInstances(describeAutoScalingInstancesRequest) response.autoScalingInstances?.forEach { group -> println("The instance lifecycle state is: ${group.lifecycleState}") } } } suspend fun enableMetricsCollection(groupName: String?) { val collectionRequest = EnableMetricsCollectionRequest { autoScalingGroupName = groupName metrics = listOf("GroupMaxSize") granularity = "1Minute" } AutoScalingClient { region = "us-east-1" }.use { autoScalingClient -> autoScalingClient.enableMetricsCollection(collectionRequest) println("The enable metrics collection operation was successful") } } suspend fun getSpecificAutoScaling(groupName: String): String { var instanceId = "" val scalingGroupsRequest = DescribeAutoScalingGroupsRequest { autoScalingGroupNames = listOf(groupName) } AutoScalingClient { region = "us-east-1" }.use { autoScalingClient -> val response = autoScalingClient.describeAutoScalingGroups(scalingGroupsRequest) response.autoScalingGroups?.forEach { group -> println("The group name is ${group.autoScalingGroupName}") println("The group ARN is ${group.autoScalingGroupArn}") group.instances?.forEach { instance -> instanceId = instance.instanceId.toString() } } } return instanceId } suspend fun describeAccountLimits() { AutoScalingClient { region = "us-east-1" }.use { autoScalingClient -> val response = autoScalingClient.describeAccountLimits(DescribeAccountLimitsRequest {}) println("The max number of Auto Scaling groups is ${response.maxNumberOfAutoScalingGroups}") println("The current number of Auto Scaling groups is ${response.numberOfAutoScalingGroups}") } } suspend fun terminateInstanceInAutoScalingGroup(instanceIdVal: String) { val request = TerminateInstanceInAutoScalingGroupRequest { instanceId = instanceIdVal shouldDecrementDesiredCapacity = false } AutoScalingClient { region = "us-east-1" }.use { autoScalingClient -> autoScalingClient.terminateInstanceInAutoScalingGroup(request) println("You have terminated instance $instanceIdVal") } } suspend fun deleteSpecificAutoScalingGroup(groupName: String) { val deleteAutoScalingGroupRequest = DeleteAutoScalingGroupRequest { autoScalingGroupName = groupName forceDelete = true } AutoScalingClient { region = "us-east-1" }.use { autoScalingClient -> autoScalingClient.deleteAutoScalingGroup(deleteAutoScalingGroupRequest) println("You successfully deleted $groupName") } }