0

I am able to easily create instances using the AWS Java API from a snapshot that I have created in the environment using runInstances(...). I have the snapshot set up in such a way that it auto-launches a set of processes and other things so that I have a fully functional configuration.

What I would like to do, is have the instance automatically run a command I configure and set a launch time WITHOUT POLLING. Is there any functionality to do this?

Right now I am polling for when it becomes available using the Java Secure Channel library; however, I would prefer to not have to worry about it and just let AWS handle it if at all possible.

The command changes at launch time depending on how/when things run, so I may need to execute a command such as:

SomeBashScript.sh -e SOME_ID some_command
el n00b
  • 1,957
  • 7
  • 37
  • 64
  • 2
    Sounds like you want `User-Data`? http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html#user-data-shell-scripts – wkl Dec 29 '16 at 17:02
  • I'm assuming that "set a launch time without polling" should read "set AT launch time without polling". You can configure a launch time script in userdata and that could pull another script, or some data, from an S3 bucket (or elsewhere) to drive the launch-time script's actions. – jarmod Dec 29 '16 at 17:15
  • @birryree yes, that's what I am thinking of - I will need to read through that documentation to see how to put it together – el n00b Dec 29 '16 at 17:27

1 Answers1

0

The API for creating instances has the following method available on it on the RunInstancesRequest class which I am using to launch the data:

public void setUserData(String userData)

This covers exactly what @birryee was saying in the User Guide.

My sample application is therefore using the following Java code in order to get things launched:

// create the actual request
RunInstancesRequest request = new RunInstancesRequest();
request.withMinCount(1).withMaxCount(count);
request.withKeyName("TheOwner");
request.withSecurityGroups("SSH");
request.withInstanceType(InstanceType.T2Micro);
request.withImageId("MyImageID");
request.withRegion(Region.getRegion(Regions.US_EAST_1));

// add a web hook call to notify us when the instance is launched
request.withUserData("#!/bin/bash\ncurl -X POST http://domain.com/email-me");

// launch it
RunInstancesResult awsResponse = awsClient.runInstances(request);
el n00b
  • 1,957
  • 7
  • 37
  • 64
  • Your userdata script should probably be more sophisticated than that. It is possible, for example, that the underlying network stack may not be fully up at the point that your script runs so your curl attempt could fail. Some ideas here: http://stackoverflow.com/questions/24962839/waiting-for-network-link-to-be-up-before-continuing-in-bash – jarmod Dec 29 '16 at 21:34
  • @jarmod It is a bit more sophisticated for the real launch. There's actually a shell script pre-deployed on the image that handles everything including looping and waiting for network connectivity where I run specialized commands. But thank you! – el n00b Jan 06 '17 at 23:47