View Christos Tranoris's profile on LinkedIn


Visit The Model Driven Software Network

Who's online

There are currently 0 users and 0 guests online.

Recent comments

Syndicate

Syndicate content

A java example wrapper for creating AMIs in the Amazon EC2

Here is a sample code for creating an AMI from java code, using the Amazon's EC2 aws-java-sdk. I didn't manage to find a good "hello world" example for java, so I post my attempt here. I hope that you can kick-start working on the cloud.
First, you create an instance of the wrapper. Then, you can use it for example like:  createAMInstances(amiId, 1, 3, "mykey", "m1.small", "us-east-1a");
 
The above call create 3 small AMIs and a key  on the zone us-east-1a. It will create it to your account's default region (you can modify the code to select also and region)
 

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2Client;
import com.amazonaws.services.ec2.model.CreateKeyPairRequest;
import com.amazonaws.services.ec2.model.CreateKeyPairResult;
import com.amazonaws.services.ec2.model.DescribeInstancesResult;
import com.amazonaws.services.ec2.model.Instance;
import com.amazonaws.services.ec2.model.KeyPair;
import com.amazonaws.services.ec2.model.Placement;
import com.amazonaws.services.ec2.model.Reservation;
import com.amazonaws.services.ec2.model.RunInstancesRequest;
import com.amazonaws.services.ec2.model.RunInstancesResult;
import com.amazonaws.services.ec2.model.TerminateInstancesRequest;
import com.amazonaws.services.ec2.model.TerminateInstancesResult;

public class EC2_Wrapper {

private AmazonEC2 ec2;
private String ReservationId;
private String instanceId;
private AWSCredentials credentials;
/**
* @return the reservationId
*/
public String getReservationId() {
return ReservationId;
}
/**
* @return the instanceId
*/
public String getInstanceId() {
return instanceId;
}
private KeyPair keyPair;

public EC2_Wrapper(String accKey, String secKey) throws Exception {
credentials = new BasicAWSCredentials(accKey, secKey);
ec2 = new AmazonEC2Client(credentials);
}

public String getAMInstanceName() {
return instanceId;
}

public void createAMInstanceSmall(String amiId, String keyPairName)
throws Exception {
createAMInstances(amiId, 1, 1, keyPairName, "m1.small", "us-east-1a");
}

public void createAMInstances(String AMId, int min, int max,
String keyPairName, String insType, String availabilityZone)
throws Exception {
RunInstancesRequest request = new RunInstancesRequest();
request.setInstanceType(insType);
request.setMinCount(min);
request.setMaxCount(max);
// set to zone
Placement p = new Placement();
p.setAvailabilityZone(availabilityZone);
request.setPlacement(p);
request.setImageId(AMId);
// Create key pair for user..
CreateKeyPairRequest kpReq = new CreateKeyPairRequest();
kpReq.setKeyName(keyPairName);
CreateKeyPairResult kpres = this.ec2.createKeyPair(kpReq);
keyPair = kpres.getKeyPair();
System.out.println("You havekeyPair.getKeyName = "
+ keyPair.getKeyName() + "\nkeyPair.getKeyFingerprint()="
+ keyPair.getKeyFingerprint() + "\nkeyPair.getKeyMaterial()="
+ keyPair.getKeyMaterial());
request.setKeyName(keyPairName);// assign Keypair name for this request
// request.getSecurityGroups().add("groupname");//create security groups
// if you want create it! the security group will be default!
// make sure to have access to SSH port 22 on the default group on the EC2console
RunInstancesResult runInstancesRes = this.ec2.runInstances(request);
this.ReservationId = runInstancesRes.getReservation()
.getReservationId();
DescribeInstancesResult describeInstancesRequest = ec2
.describeInstances();
List reservations = describeInstancesRequest
.getReservations();
Set instances = new HashSet();

for (Reservation reservation : reservations) {
instances.addAll(reservation.getInstances());
if (reservation.getReservationId().equals(this.ReservationId)) {
this.instanceId = ((Instance) reservation.getInstances().get(0))
.getInstanceId();
System.out.println("this.instanceId = " + this.instanceId);
}
}
System.out.println("You have " + instances.size()
+ " Amazon EC2 instance(s).");
}

public void terminateAMIs() throws Exception {
try {
TerminateInstancesRequest rq = new TerminateInstancesRequest();

rq.getInstanceIds().add(this.instanceId);

TerminateInstancesResult rsp = this.ec2.terminateInstances(rq);

} catch (Exception ex) {
throw ex;
}
}
}

Posted in Submitted by tranoris on June 15, 2010 - 23:49.



Anonymous | July 10, 2014 - 13:53

For beginners like me these codes are very useful to understand the principles of using java api of aws ec2's to manage instances, thank you so much!

Anonymous | July 9, 2011 - 12:15

This is really useful stuff...

Made this amendment, thought it was useful to share back:

List <Reservation> reservations = describeInstancesRequest.getReservations();

Best regards,

Edgarator

Anonymous | August 25, 2010 - 15:25

Hi Christos,
your code helped me a lot. As you suggested i used it as a starting point for building a more complete example.
This is the link to the article:
http://instrumentelem.sourceforge.net/wiki/index.php/Amazon_ec2

it is a wiki page so feel free to contribute!

Talk soon,
Francesco

Dr. Francesco Lelli
Web: http://www.inf.usi.ch/postdoc/lelli/

tranoris | August 26, 2010 - 11:18

Thanks, that is really good to know