Is it possible to pass a script while I'm starting an instance? So that the instance will execute that script once its initialized?
public static void startInstance(final String instanceId) {
StartInstancesRequest startRequest = new StartInstancesRequest().withInstanceIds(instanceId);
logger.info(String.format("Starting instance '%s':...", instanceId));
ec2.startInstances(startRequest);
}
Any idea? Script will be kind of startup script which I need to execute as soon as the instance is ready. Thanks in advance.
You can use the Userdata to pass a script to a new EC2 Instance although I dont know if you can start a pre existing instance and pass it data.
I have a pre configured AMI that has a script saved on the root drive, this script is used to configure the server and download the latest source.
So your request could look like the following:
var runInstance = new RunInstancesRequest();
The user data can contain either a reference to a script or the script itself.
I introduced the concept of user-data scripts in the community Ubuntu AMIs in 2009 (simplified version of some ideas from RightScale):
It boils down to:
This feature was later included in the official Ubuntu AMIs in the CloudInit package:
Amazon now includes the CloudInit package in their own Amazon Linux AMIs:
Other distros are welcome to (and may already) include this feature in their AMIs.