How to run a shell script on all amazon ec2 instances that are part of a autoscaling group

amazon ec2amazon-web-servicesautoscalingshell-scripting

Can anyone please tell me how to run a shell script on all the ec2 instances that are part of a auto scaling group.

The scenario is that i have a script that i want to run on many ec2 instances that are turned on automatically as part of auto scaling group. The native approach is ssh to each instance and run the script. I am looking for a way by which it can run automatically on all the instances when i run it on one of the ec2 instance or any better way of doing this?

Thanks in advance.

Best Answer

I would say there are two approaches:

  • Use the AWS API to determine the IP of all group members and then do SSH. -- This itself is possible on different levels, ranging from a shell script using aws-cli (describe-auto-scaling-instances and describe-instances) to a more sophisticated setup e.g. with Python boto and Fabric.

  • Have all instances register themselves in some orchestration or cluster management tool, e.g. serf or MCollective, and then use that channel to send commands to all group members.

Possibly third approach: do not explicitly run shell scripts at all, but have all instances get their setup from a Puppet or Chef server.

Related Topic