1**Example 1: To create an Auto Scaling group**
2
3The following ``create-auto-scaling-group`` example creates an Auto Scaling group in subnets in multiple Availability Zones within a Region. The instances launch with the default version of the specified launch template. Note that defaults are used for most other settings, such as the termination policies and health check configuration. ::
4
5    aws autoscaling create-auto-scaling-group \
6        --auto-scaling-group-name my-asg \
7        --launch-template LaunchTemplateId=lt-1234567890abcde12 \
8        --min-size 1 \
9        --max-size 5 \
10        --vpc-zone-identifier "subnet-5ea0c127,subnet-6194ea3b,subnet-c934b782"
11
12This command produces no output.
13
14For more information, see `Auto Scaling groups <https://docs.aws.amazon.com/autoscaling/ec2/userguide/AutoScalingGroup.html>`__ in the *Amazon EC2 Auto Scaling User Guide*.
15
16**Example 2: To attach an Application Load Balancer, Network Load Balancer, or Gateway Load Balancer**
17
18This example specifies the ARN of a target group for a load balancer that supports the expected traffic. The health check type specifies ``ELB`` so that when Elastic Load Balancing reports an instance as unhealthy, the Auto Scaling group replaces it. The command also defines a health check grace period of ``600`` seconds. The grace period helps prevent premature termination of newly launched instances. ::
19
20    aws autoscaling create-auto-scaling-group \
21        --auto-scaling-group-name my-asg \
22        --launch-template LaunchTemplateId=lt-1234567890abcde12 \
23        --target-group-arns arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/943f017f100becff \
24        --health-check-type ELB \
25        --health-check-grace-period 600 \
26        --min-size 1 \
27        --max-size 5 \
28        --vpc-zone-identifier "subnet-5ea0c127,subnet-6194ea3b,subnet-c934b782"
29
30This command produces no output.
31
32For more information, see `Elastic Load Balancing and Amazon EC2 Auto Scaling <https://docs.aws.amazon.com/autoscaling/ec2/userguide/autoscaling-load-balancer.html>`__ in the *Amazon EC2 Auto Scaling User Guide*.
33
34**Example 3: To specify a placement group and use the latest version of the launch template**
35
36This example launches instances into a placement group within a single Availability Zone. This can be useful for low-latency groups with HPC workloads. This example also specifies the minimum size, maximum size, and desired capacity of the group. ::
37
38    aws autoscaling create-auto-scaling-group \
39        --auto-scaling-group-name my-asg \
40        --launch-template LaunchTemplateId=lt-1234567890abcde12,Version='$Latest' \
41        --min-size 1 \
42        --max-size 5 \
43        --desired-capacity 3 \
44        --placement-group my-placement-group \
45        --vpc-zone-identifier "subnet-6194ea3b"
46
47This command produces no output.
48
49For more information, see `Placement groups <https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/placement-groups.html>`__ in the *Amazon EC2 User Guide for Linux Instances*.
50
51**Example 4: To specify a single instance Auto Scaling group and use a specific version of the launch template**
52
53This example creates an Auto Scaling group with minimum and maximum capacity set to ``1`` to enforce that one instance will be running. The command also specifies v1 of a launch template in which the ID of an existing ENI is specified. When you use a launch template that specifies an existing ENI for eth0, you must specify an Availability Zone for the Auto Scaling group that matches the network interface, without also specifying a subnet ID in the request. ::
54
55    aws autoscaling create-auto-scaling-group \
56        --auto-scaling-group-name my-asg-single-instance \
57        --launch-template LaunchTemplateName=my-template-for-auto-scaling,Version='1' \
58        --min-size 1 \
59        --max-size 1 \
60        --availability-zones us-west-2a
61
62This command produces no output.
63
64For more information, see `Auto Scaling groups <https://docs.aws.amazon.com/autoscaling/ec2/userguide/AutoScalingGroup.html>`__ in the *Amazon EC2 Auto Scaling User Guide*.
65
66**Example 5: To specify a different termination policy**
67
68This example creates an Auto Scaling group using a launch configuration and sets the termination policy to terminate the oldest instances first. The command also applies a tag to the group and its instances, with a key of ``Role`` and a value of ``WebServer``. ::
69
70    aws autoscaling create-auto-scaling-group \
71        --auto-scaling-group-name my-asg \
72        --launch-configuration-name my-lc \
73        --min-size 1 \
74        --max-size 5 \
75        --termination-policies "OldestInstance" \
76        --tags "ResourceId=my-asg,ResourceType=auto-scaling-group,Key=Role,Value=WebServer,PropagateAtLaunch=true" \
77        --vpc-zone-identifier "subnet-5ea0c127,subnet-6194ea3b,subnet-c934b782"
78
79This command produces no output.
80
81For more information, see `Working with Amazon EC2 Auto Scaling termination policies <https://docs.aws.amazon.com/autoscaling/ec2/userguide/ec2-auto-scaling-termination-policies.html>`__ in the *Amazon EC2 Auto Scaling User Guide*.
82
83**Example 6: To specify a launch lifecycle hook**
84
85This example creates an Auto Scaling group with a lifecycle hook that supports a custom action at instance launch. ::
86
87    aws autoscaling create-auto-scaling-group \
88        --cli-input-json file://~/config.json
89
90Contents of ``config.json`` file::
91
92    {
93        "AutoScalingGroupName": "my-asg",
94        "LaunchTemplate": {
95            "LaunchTemplateId": "lt-1234567890abcde12"
96        },
97        "LifecycleHookSpecificationList": [{
98            "LifecycleHookName": "my-launch-hook",
99            "LifecycleTransition": "autoscaling:EC2_INSTANCE_LAUNCHING",
100            "NotificationTargetARN": "arn:aws:sqs:us-west-2:123456789012:my-sqs-queue",
101            "RoleARN": "arn:aws:iam::123456789012:role/my-notification-role",
102            "NotificationMetadata": "SQS message metadata",
103            "HeartbeatTimeout": 4800,
104            "DefaultResult": "ABANDON"
105        }],
106        "MinSize": 1,
107        "MaxSize": 5,
108        "VPCZoneIdentifier": "subnet-5ea0c127,subnet-6194ea3b,subnet-c934b782",
109        "Tags": [{
110            "ResourceType": "auto-scaling-group",
111            "ResourceId": "my-asg",
112            "PropagateAtLaunch": true,
113            "Value": "test",
114            "Key": "environment"
115        }]
116    }
117
118This command produces no output.
119
120For more information, see `Amazon EC2 Auto Scaling lifecycle hooks <https://docs.aws.amazon.com/autoscaling/ec2/userguide/lifecycle-hooks.html>`__ in the *Amazon EC2 Auto Scaling User Guide*.
121
122**Example 7: To specify a termination lifecycle hook**
123
124This example creates an Auto Scaling group with a lifecycle hook that supports a custom action at instance termination. ::
125
126    aws autoscaling create-auto-scaling-group \
127        --cli-input-json file://~/config.json
128
129Contents of ``config.json``::
130
131    {
132        "AutoScalingGroupName": "my-asg",
133        "LaunchTemplate": {
134            "LaunchTemplateId": "lt-1234567890abcde12"
135        },
136        "LifecycleHookSpecificationList": [{
137            "LifecycleHookName": "my-termination-hook",
138            "LifecycleTransition": "autoscaling:EC2_INSTANCE_TERMINATING",
139            "HeartbeatTimeout": 120,
140            "DefaultResult": "CONTINUE"
141        }],
142        "MinSize": 1,
143        "MaxSize": 5,
144        "TargetGroupARNs": [
145            "arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067"
146        ],
147        "VPCZoneIdentifier": "subnet-5ea0c127,subnet-6194ea3b,subnet-c934b782"
148    }
149
150This command produces no output.
151
152For more information, see `Amazon EC2 Auto Scaling lifecycle hooks <https://docs.aws.amazon.com/autoscaling/ec2/userguide/lifecycle-hooks.html>`__ in the *Amazon EC2 Auto Scaling User Guide*.
153
154**Example 8: To specify a custom termination policy**
155
156This example creates an Auto Scaling group that specifies a custom Lambda function termination policy that tells Amazon EC2 Auto Scaling which instances are safe to terminate on scale in. ::
157
158    aws autoscaling create-auto-scaling-group \
159        --auto-scaling-group-name my-asg-single-instance \
160        --launch-template LaunchTemplateName=my-template-for-auto-scaling \
161        --min-size 1 \
162        --max-size 5 \
163        --termination-policies "arn:aws:lambda:us-west-2:123456789012:function:HelloFunction:prod" \
164        --vpc-zone-identifier "subnet-5ea0c127,subnet-6194ea3b,subnet-c934b782"
165
166This command produces no output.
167
168For more information, see `Creating a custom termination policy with Lambda <https://docs.aws.amazon.com/autoscaling/ec2/userguide/lambda-custom-termination-policy.html>`__ in the *Amazon EC2 Auto Scaling User Guide*.