The answer depends a bit on what AMI you are running as the features provided is entirely AMI dependent.
The Amazon Linux AMIS and also the official Ubuntu AMIs have the cloud-init package installed. This incorporates a range of ways you'll be able to trigger startup actions, but the one that matches your request most closely (and my favourite because I invented it) is the concept of a user-data script.
You can merely pass any script (starting with the 2 characters #!) as the user-data once beginning the EC2 instances. It will be run as root on the first boot of the instance.
You also needed to run over one EC2 instance with a similar script. The ec2-run-instances command and also the related Apis and web console enable you to specify any range of instances, to begin with, constant user-data. For example:
--instance-count 10 \
--user-data-file $MYSCRIPT \
--key $USER \
If you are currently running an AMI that does not have cloud-init installed, you could do one of:
- Switch to an AMI that has cloud-init installed, or
- Build a custom version of your AMI that has cloud-init installed, or
- Write a more sophisticated wrapper script that creates a record of all of the instance ids once they're beginning, waits for all of the instances to move to the running state, waits for the sshd to accept connections, uploads your startup script to each instance, and runs the startup script on each instance.