3

How can I update the AMI in an Auto Scaling configuration after updating code in an EC2 instance? I don't need to use AWS CodeDeploy.

I have simple script, but it is still requires me to manually type the Image ID:

1) Create AMI

aws ec2 create-image --instance-id i-0b09axxx --name "My server" --no-reboot

2) Create new Auto Scaling Launch Configuration

aws autoscaling create-launch-configuration --launch-configuration-name "new-launch-configuration-name" --image-id "new-AMI-id"

3) Update Auto Scaling Launch Configuration with new AMI

aws autoscaling update-auto-scaling-group --auto-scaling-group-name "current-autoscaling-group-name" --launch-configuration-name "new-launch-configuration-name"

4) Delete old Auto Scaling Launch Configuration

aws autoscaling delete-launch-configuration --launch-configuration-name "old-launch-configuration-name"
  • 1
    Capture the output of the first command, parse out the new image ID into a variable, then use that variable in the second command. – EEAA Jun 26 '17 at 12:29
4

Try something like this (assuming you're using 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
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.