如何在更新 EC2 实例中的代码后更新 Auto Scaling 配置中的 AMI?我不需要使用 AWS CodeDeploy。
我有一个简单的脚本,但仍然需要我手动输入图像 ID:
1)创建 AMI
aws ec2 create-image --instance-id i-0b09axxx --name "My server" --no-reboot
2)创建新的自动扩展启动配置
aws autoscaling create-launch-configuration --launch-configuration-name "new-launch-configuration-name" --image-id "new-AMI-id"
3)使用新的 AMI 更新 Auto Scaling 启动配置
aws autoscaling update-auto-scaling-group --auto-scaling-group-name "current-autoscaling-group-name" --launch-configuration-name "new-launch-configuration-name"
4)删除旧的自动扩展启动配置
aws autoscaling delete-launch-configuration --launch-configuration-name "old-launch-configuration-name"
答案1
尝试这样的操作(假设您使用的是Linux):
#Define parameters
INSTANCE=i-abcd1234
ASG_NAME="current-autoscaling-group-name"
OLD_LC="old-launch-configuration-name"
NEW_LC="new-launch-configuration-name"
# Create AMI
IMAGE=`aws ec2 create-image --instance-id $INSTANCE --name NEW-IMAGE --output text`
# Create Launch Configuration
aws autoscaling create-launch-configuration --launch-configuration-name $NEW_LC --image-id $IMAGE --instance-type t2.micro
# Update Auto Scaling Group to use new Launch Configuration
aws autoscaling update-auto-scaling-group --auto-scaling-group-name $ASG_NAME --launch-configuration-name $NEW_LC
# Delete old Auto Scaling Launch Configuration
aws autoscaling delete-launch-configuration --launch-configuration-name $OLD_LC