AWS SDK for Java
Developer Guide

Managing Amazon EC2 Instances

Important

This is a preview release and is not recommended for production environments.

Creating an Instance

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

Imports

import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.InstanceType; import software.amazon.awssdk.services.ec2.model.RunInstancesRequest; import software.amazon.awssdk.services.ec2.model.RunInstancesResponse; import software.amazon.awssdk.services.ec2.model.Tag; import software.amazon.awssdk.services.ec2.model.CreateTagsRequest;

Code

Ec2Client ec2 = Ec2Client.create(); RunInstancesRequest run_request = RunInstancesRequest.builder() .imageId(ami_id) .instanceType(InstanceType.T1_MICRO) .maxCount(1) .minCount(1) .build(); RunInstancesResponse response = ec2.runInstances(run_request); String instance_id = response.reservation().reservationId(); Tag tag = Tag.builder() .key("Name") .value(name) .build(); CreateTagsRequest tag_request = CreateTagsRequest.builder() .tags(tag) .build(); try { ec2.createTags(tag_request); System.out.printf( "Successfully started EC2 instance %s based on AMI %s", instance_id, ami_id); } catch (Ec2Exception e) { System.err.println(e.getMessage()); System.exit(1); }

See the complete example on GitHub.

Starting an Instance

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

Imports

import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.StartInstancesRequest;

Code

Ec2Client ec2 = Ec2Client.create(); StartInstancesRequest request = StartInstancesRequest.builder() .instanceIds(instance_id).build(); ec2.startInstances(request);

See the complete example on GitHub.

Stopping an Instance

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

Imports

import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.StopInstancesRequest;

Code

Ec2Client ec2 = Ec2Client.create(); StopInstancesRequest request = StopInstancesRequest.builder() .instanceIds(instance_id).build(); ec2.stopInstances(request);

See the complete example on GitHub.

Rebooting an Instance

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

Imports

import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.RebootInstancesRequest; import software.amazon.awssdk.services.ec2.model.RebootInstancesResponse;

Code

Ec2Client ec2 = Ec2Client.create(); RebootInstancesRequest request = RebootInstancesRequest.builder() .instanceIds(instance_id).build(); RebootInstancesResponse response = ec2.rebootInstances(request);

See the complete example on GitHub.

Describing Instances

To list your instances, create a DescribeInstancesRequest and call the Ec2Client's describeInstances method. It will return a DescribeInstancesResponse 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 DescribeInstancesResponse class' reservations method, and then call instances on each returned Reservation object.

Imports

import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest; import software.amazon.awssdk.services.ec2.model.DescribeInstancesResponse; import software.amazon.awssdk.services.ec2.model.Instance; import software.amazon.awssdk.services.ec2.model.Reservation;

Code

DescribeInstancesRequest request = DescribeInstancesRequest.builder().build(); while(!done) { DescribeInstancesResponse response = ec2.describeInstances(request); for(Reservation reservation : response.reservations()) { for(Instance instance : reservation.instances()) { System.out.printf( "Found reservation with id %s, " + "AMI %s, " + "type %s, " + "state %s " + "and monitoring state %s", instance.instanceId(), instance.imageId(), instance.instanceType(), instance.state().name(), instance.monitoring().state()); System.out.println(""); } } if(response.nextToken() == null) { done = true; } }

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

See the complete example on GitHub.

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 Ec2Client's monitorInstances method.

Imports

import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.MonitorInstancesRequest;

Code

Ec2Client ec2 = Ec2Client.create(); MonitorInstancesRequest request = MonitorInstancesRequest.builder() .instanceIds(instance_id).build(); ec2.monitorInstances(request);

See the complete example on GitHub.

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 Ec2Client's unmonitorInstances method.

Imports

import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.UnmonitorInstancesRequest;

Code

Ec2Client ec2 = Ec2Client.create(); UnmonitorInstancesRequest request = UnmonitorInstancesRequest.builder() .instanceIds(instance_id).build(); ec2.unmonitorInstances(request);

See the complete example on GitHub.

More Information