Managing Amazon EC2 Instances - Amazon SDK for Java 1.x
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

The Amazon SDK for Java 1.x has entered maintenance mode as of July 31, 2024, and will reach end-of-support on December 31, 2025. We recommend that you migrate to the Amazon SDK for Java 2.x to continue receiving new features, availability improvements, and security updates.

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

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; import com.amazonaws.services.ec2.model.Tag;

Code

RunInstancesRequest run_request = new RunInstancesRequest() .withImageId(ami_id) .withInstanceType(InstanceType.T1Micro) .withMaxCount(1) .withMinCount(1); RunInstancesResult run_response = ec2.runInstances(run_request); String reservation_id = run_response.getReservation().getInstances().get(0).getInstanceId();

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

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

Code

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

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

Code

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

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

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 `getInstances on each returned Reservation object.

Imports

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

final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient(); boolean done = false; DescribeInstancesRequest request = new DescribeInstancesRequest(); while(!done) { DescribeInstancesResult response = ec2.describeInstances(request); for(Reservation reservation : response.getReservations()) { for(Instance instance : reservation.getInstances()) { System.out.printf( "Found instance 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

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

Code

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

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

Code

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

See the complete example.

More Information