createAutoScalingGroup
We strongly recommend using a launch template when calling this operation to ensure full functionality for Amazon EC2 Auto Scaling and Amazon EC2.
Creates an Auto Scaling group with the specified name and attributes.
If you exceed your maximum limit of Auto Scaling groups, the call fails. To query this limit, call the DescribeAccountLimits API. For information about updating this limit, see Quotas for Amazon EC2 Auto Scaling in the Amazon EC2 Auto Scaling User Guide.
For introductory exercises for creating an Auto Scaling group, see Getting started with Amazon EC2 Auto Scaling and Tutorial: Set up a scaled and load-balanced application in the Amazon EC2 Auto Scaling User Guide. For more information, see Auto Scaling groups in the Amazon EC2 Auto Scaling User Guide.
Every Auto Scaling group has three size properties (DesiredCapacity
, MaxSize
, and MinSize
). Usually, you set these sizes based on a specific number of instances. However, if you configure a mixed instances policy that defines weights for the instance types, you must specify these sizes with the same units that you use for weighting instances.
Samples
import aws.sdk.kotlin.services.autoscaling.model.CpuManufacturer
import aws.sdk.kotlin.services.autoscaling.model.LaunchTemplateOverrides
fun main() {
//sampleStart
// This example creates an Auto Scaling group.
autoScalingClient.createAutoScalingGroup {
autoScalingGroupName = "my-auto-scaling-group"
launchTemplate = LaunchTemplateSpecification {
launchTemplateName = "my-template-for-auto-scaling"
version = "$Default"
}
minSize = 1
maxSize = 3
maxInstanceLifetime = 2592000
defaultInstanceWarmup = 120
vpcZoneIdentifier = "subnet-057fa0918fEXAMPLE"
}
//sampleEnd
}
import aws.sdk.kotlin.services.autoscaling.model.CpuManufacturer
import aws.sdk.kotlin.services.autoscaling.model.LaunchTemplateOverrides
fun main() {
//sampleStart
// This example creates an Auto Scaling group and attaches the specified target group.
autoScalingClient.createAutoScalingGroup {
autoScalingGroupName = "my-auto-scaling-group"
launchTemplate = LaunchTemplateSpecification {
launchTemplateName = "my-template-for-auto-scaling"
version = "$Default"
}
minSize = 1
maxSize = 3
targetGroupArns = listOf<String>(
"arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067"
)
healthCheckType = "ELB"
healthCheckGracePeriod = 300
vpcZoneIdentifier = "subnet-057fa0918fEXAMPLE, subnet-610acd08EXAMPLE"
}
//sampleEnd
}
import aws.sdk.kotlin.services.autoscaling.model.CpuManufacturer
import aws.sdk.kotlin.services.autoscaling.model.LaunchTemplateOverrides
fun main() {
//sampleStart
// This example creates an Auto Scaling group with a mixed instances policy. It specifies the c5.
// large, c5a. large, and c6g. large instance types and defines a different launch template for the c6g.
// large instance type.
autoScalingClient.createAutoScalingGroup {
autoScalingGroupName = "my-asg"
mixedInstancesPolicy = MixedInstancesPolicy {
launchTemplate = LaunchTemplate {
launchTemplateSpecification = LaunchTemplateSpecification {
launchTemplateName = "my-launch-template-for-x86"
version = "$Default"
}
overrides = listOf<LaunchTemplateOverrides>(
LaunchTemplateOverrides {
instanceType = "c6g.large"
launchTemplateSpecification = LaunchTemplateSpecification {
launchTemplateName = "my-launch-template-for-arm"
version = "$Default"
}
},
LaunchTemplateOverrides {
instanceType = "c5.large"
},
LaunchTemplateOverrides {
instanceType = "c5a.large"
}
)
}
instancesDistribution = InstancesDistribution {
onDemandBaseCapacity = 1
onDemandPercentageAboveBaseCapacity = 50
spotAllocationStrategy = "price-capacity-optimized"
}
}
minSize = 1
maxSize = 5
desiredCapacity = 3
vpcZoneIdentifier = "subnet-057fa0918fEXAMPLE, subnet-610acd08EXAMPLE"
}
//sampleEnd
}
import aws.sdk.kotlin.services.autoscaling.model.CpuManufacturer
import aws.sdk.kotlin.services.autoscaling.model.LaunchTemplateOverrides
fun main() {
//sampleStart
// This example creates an Auto Scaling group using attribute based instance type selection. It
// requires the instance types to have a minimum of four vCPUs and a maximum of eight vCPUs, a minimum of 16,
// 384 MiB of memory, and an Intel manufactured CPU.
autoScalingClient.createAutoScalingGroup {
autoScalingGroupName = "my-asg"
mixedInstancesPolicy = MixedInstancesPolicy {
launchTemplate = LaunchTemplate {
launchTemplateSpecification = LaunchTemplateSpecification {
launchTemplateName = "my-template-for-auto-scaling"
version = "$Default"
}
overrides = listOf<LaunchTemplateOverrides>(
LaunchTemplateOverrides {
instanceRequirements = InstanceRequirements {
vCpuCount = VCpuCountRequest {
min = 4
max = 8
}
memoryMib = MemoryMibRequest {
min = 16384
}
cpuManufacturers = listOf<CpuManufacturer>(
CpuManufacturer.fromValue("intel")
)
}
}
)
}
instancesDistribution = InstancesDistribution {
onDemandPercentageAboveBaseCapacity = 50
spotAllocationStrategy = "price-capacity-optimized"
}
}
minSize = 0
maxSize = 100
desiredCapacity = 4
desiredCapacityType = "units"
vpcZoneIdentifier = "subnet-057fa0918fEXAMPLE, subnet-610acd08EXAMPLE"
}
//sampleEnd
}