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 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;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
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;
}
}
}