AWS提供了自动扩展组(Auto Scaling Group)来实现自动缩放延迟启动和终止。下面是一个使用AWS CLI创建自动扩展组并设置延迟启动和终止的示例代码:
aws autoscaling create-launch-configuration --launch-configuration-name my-launch-config --image-id ami-12345678 --instance-type t2.micro --security-groups sg-12345678 --key-name my-key --instance-monitoring Enabled=true --no-ebs-optimized
在上面的代码中,需要将image-id
替换为你要使用的AMI ID,security-groups
替换为你的安全组ID,key-name
替换为你的密钥对名称。
aws autoscaling create-auto-scaling-group --auto-scaling-group-name my-asg --launch-configuration-name my-launch-config --min-size 2 --max-size 5 --desired-capacity 2 --default-cooldown 300 --health-check-type EC2 --health-check-grace-period 300 --vpc-zone-identifier subnet-12345678 --tags Key=Name,Value=my-asg,PropagateAtLaunch=true
在上面的代码中,需要将vpc-zone-identifier
替换为你的子网ID。
aws autoscaling put-scaling-policy --policy-name my-scale-out-policy --auto-scaling-group-name my-asg --scaling-adjustment 1 --adjustment-type ChangeInCapacity --cooldown 300
aws autoscaling put-scaling-policy --policy-name my-scale-in-policy --auto-scaling-group-name my-asg --scaling-adjustment -1 --adjustment-type ChangeInCapacity --cooldown 300
在上面的代码中,scaling-adjustment
参数用于设置每次扩展或缩小的实例数量,cooldown
参数用于设置扩展或缩小操作之间的冷却时间。
以上是使用AWS CLI创建自动扩展组并设置延迟启动和终止的示例代码。你可以根据自己的需求进行修改和扩展。