Menu
AWS SDK for Java
Developer Guide

Managing Amazon EC2 Instances

Creating an Instance

Create a new Amazon EC2 instance by calling the AmazonEC2Client's runInstances method, providing it with a RunInstancesRequest containing the Amazon Machine Image (AMI) to use and an instance type.

Imports

Copy
import com.amazonaws.services.ec2.AmazonEC2; import com.amazonaws.services.ec2.AmazonEC2ClientBuilder; import com.amazonaws.services.ec2.model.InstanceType; import com.amazonaws.services.ec2.model.RunInstancesRequest; import com.amazonaws.services.ec2.model.RunInstancesResult;

Code

Copy
final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient(); RunInstancesRequest run_request = new RunInstancesRequest() .withImageId(ami_id) .withInstanceType(InstanceType.T1Micro) .withMaxCount(1) .withMinCount(1); RunInstancesResult run_response = ec2.runInstances(run_request); String instance_id = run_response.getReservation().getReservationId();

See the complete example.

Starting an Instance

To start an Amazon EC2 instance, call the AmazonEC2Client's startInstances method, providing it with a StartInstancesRequest containing the ID of the instance to start.

Imports

Copy
import com.amazonaws.services.ec2.AmazonEC2; import com.amazonaws.services.ec2.AmazonEC2ClientBuilder; import com.amazonaws.services.ec2.model.StartInstancesRequest;

Code

Copy
final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient(); StartInstancesRequest request = new StartInstancesRequest() .withInstanceIds(instance_id); ec2.startInstances(request);

See the complete example.

Stopping an Instance

To stop an Amazon EC2 instance, call the AmazonEC2Client's stopInstances method, providing it with a StopInstancesRequest containing the ID of the instance to stop.

Imports

Copy
import com.amazonaws.services.ec2.AmazonEC2; import com.amazonaws.services.ec2.AmazonEC2ClientBuilder; import com.amazonaws.services.ec2.model.StopInstancesRequest;

Code

Copy
final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient(); StopInstancesRequest request = new StopInstancesRequest() .withInstanceIds(instance_id); ec2.stopInstances(request);

See the complete example.

Rebooting an Instance

To reboot an Amazon EC2 instance, call the AmazonEC2Client's rebootInstances method, providing it with a RebootInstancesRequest containing the ID of the instance to reboot.

Imports

Copy
import com.amazonaws.services.ec2.AmazonEC2; import com.amazonaws.services.ec2.AmazonEC2ClientBuilder; import com.amazonaws.services.ec2.model.RebootInstancesRequest; import com.amazonaws.services.ec2.model.RebootInstancesResult;

Code

Copy
final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient(); RebootInstancesRequest request = new RebootInstancesRequest() .withInstanceIds(instance_id); RebootInstancesResult response = ec2.rebootInstances(request);

See the complete example.

Describing Instances

To list your instances, create a DescribeInstancesRequest and call the AmazonEC2Client's describeInstances method. It will return a DescribeInstancesResult object that you can use to list the Amazon EC2 instances for your account and region.

Instances are grouped by reservation. Each reservation corresponds to the call to startInstances that launched the instance. To list your instances, you must first call the DescribeInstancesResult class' getReservations' method, and then call :methodname:`getInstances on each returned Reservation object.

Imports

Copy
import com.amazonaws.services.ec2.AmazonEC2; import com.amazonaws.services.ec2.AmazonEC2ClientBuilder; import com.amazonaws.services.ec2.model.DescribeInstancesRequest; import com.amazonaws.services.ec2.model.DescribeInstancesResult; import com.amazonaws.services.ec2.model.Instance; import com.amazonaws.services.ec2.model.Reservation;

Code

Copy
final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient(); boolean done = false; while(!done) { DescribeInstancesRequest request = new DescribeInstancesRequest(); DescribeInstancesResult response = ec2.describeInstances(request); for(Reservation reservation : response.getReservations()) { for(Instance instance : reservation.getInstances()) { System.out.printf( "Found reservation with id %s, " + "AMI %s, " + "type %s, " + "state %s " + "and monitoring state %s", instance.getInstanceId(), instance.getImageId(), instance.getInstanceType(), instance.getState().getName(), instance.getMonitoring().getState()); } } request.setNextToken(response.getNextToken()); if(response.getNextToken() == null) { done = true; } }

Results are paged; you can get further results by passing the value returned from the result object's getNextToken method to your original request object's setNextToken method, then using the same request object in your next call to describeInstances.

See the complete example.

Monitoring an Instance

You can monitor various aspects of your Amazon EC2 instances, such as CPU and network utilization, available memory, and disk space remaining. To learn more about instance monitoring, see Monitoring Amazon EC2 in the Amazon EC2 User Guide for Linux Instances.

To start monitoring an instance, you must create a MonitorInstancesRequest with the ID of the instance to monitor, and pass it to the AmazonEC2Client's monitorInstances method.

Imports

Copy
import com.amazonaws.services.ec2.AmazonEC2; import com.amazonaws.services.ec2.AmazonEC2ClientBuilder; import com.amazonaws.services.ec2.model.MonitorInstancesRequest;

Code

Copy
final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient(); MonitorInstancesRequest request = new MonitorInstancesRequest() .withInstanceIds(instance_id); ec2.monitorInstances(request);

See the complete example.

Stopping Instance Monitoring

To stop monitoring an instance, create an UnmonitorInstancesRequest with the ID of the instance to stop monitoring, and pass it to the AmazonEC2Client's unmonitorInstances method.

Imports

Copy
import com.amazonaws.services.ec2.AmazonEC2; import com.amazonaws.services.ec2.AmazonEC2ClientBuilder; import com.amazonaws.services.ec2.model.UnmonitorInstancesRequest;

Code

Copy
final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient(); UnmonitorInstancesRequest request = new UnmonitorInstancesRequest() .withInstanceIds(instance_id); ec2.unmonitorInstances(request);

See the complete example.

More Information