Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
in AWS by (19.1k points)

I want to automate the launch of a set of Linux EC2 instances.

Basically, I want to write a script/program that would :

  • Instantiate N occurrences of a given AMI of mine.
  • For each started instance, it would upload a customized script and let the script run into the instance.

Using VMWare, I would typically do that using vmrun or the Vix SDK.

What are the options in Amazon AWS/EC2?

1 Answer

0 votes
by (44.4k points)

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:

ec2-run-instances \

--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.

Related questions

0 votes
1 answer

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer
0 votes
1 answer
asked Jul 8, 2019 in AWS by Amyra (12.9k points)

Browse Categories