Skip to content

Instantly share code, notes, and snippets.

@AlexsJones
Created March 19, 2014 16:17
Show Gist options
  • Save AlexsJones/9645295 to your computer and use it in GitHub Desktop.
Save AlexsJones/9645295 to your computer and use it in GitHub Desktop.
// CONNECT TO EC2
InputStream credentialsAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("AwsCredentials.properties");
Preconditions.checkNotNull(credentialsAsStream, "File 'AwsCredentials.properties' NOT found in the classpath");
AWSCredentials credentials = new PropertiesCredentials(credentialsAsStream);
AmazonEC2 ec2 = new AmazonEC2Client(credentials);
ec2.setEndpoint("ec2.eu-west-1.amazonaws.com");
// CREATE EC2 INSTANCES
RunInstancesRequest runInstancesRequest = new RunInstancesRequest()
.withInstanceType("t1.micro")
.withImageId("ami-62201116")
.withMinCount(2)
.withMaxCount(2)
.withSecurityGroupIds("tomcat")
.withKeyName("xebia-france")
.withUserData(Base64.encodeBase64String(myUserData.getBytes()))
;
RunInstancesResult runInstances = ec2.runInstances(runInstancesRequest);
// TAG EC2 INSTANCES
List<Instance> instances = runInstances.getReservation().getInstances();
int idx = 1;
for (Instance instance : instances) {
CreateTagsRequest createTagsRequest = new CreateTagsRequest();
createTagsRequest.withResources(instance.getInstanceId()) //
.withTags(new Tag("Name", "travel-ecommerce-" + idx));
ec2.createTags(createTagsRequest);
idx++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment