Manage Amazon EC2 instances - Amazon SDK for Java 2.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).

Manage Amazon EC2 instances

Create 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.regions.Region; 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; import software.amazon.awssdk.services.ec2.model.Ec2Exception;

Code

public static String createEC2Instance(Ec2Client ec2,String name, String amiId ) { RunInstancesRequest runRequest = RunInstancesRequest.builder() .imageId(amiId) .instanceType(InstanceType.T1_MICRO) .maxCount(1) .minCount(1) .build(); RunInstancesResponse response = ec2.runInstances(runRequest); String instanceId = response.instances().get(0).instanceId(); Tag tag = Tag.builder() .key("Name") .value(name) .build(); CreateTagsRequest tagRequest = CreateTagsRequest.builder() .resources(instanceId) .tags(tag) .build(); try { ec2.createTags(tagRequest); System.out.printf( "Successfully started EC2 Instance %s based on AMI %s", instanceId, amiId); return instanceId; } catch (Ec2Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; }

See the complete example on GitHub.

Start 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.regions.Region; import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.StartInstancesRequest; import software.amazon.awssdk.services.ec2.model.StopInstancesRequest;

Code

public static void startInstance(Ec2Client ec2, String instanceId) { StartInstancesRequest request = StartInstancesRequest.builder() .instanceIds(instanceId) .build(); ec2.startInstances(request); System.out.printf("Successfully started instance %s", instanceId); }

See the complete example on GitHub.

Stop 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.regions.Region; import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.StartInstancesRequest; import software.amazon.awssdk.services.ec2.model.StopInstancesRequest;

Code

public static void stopInstance(Ec2Client ec2, String instanceId) { StopInstancesRequest request = StopInstancesRequest.builder() .instanceIds(instanceId) .build(); ec2.stopInstances(request); System.out.printf("Successfully stopped instance %s", instanceId); }

See the complete example on GitHub.

Reboot 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.regions.Region; import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.Ec2Exception; import software.amazon.awssdk.services.ec2.model.RebootInstancesRequest;

Code

public static void rebootEC2Instance(Ec2Client ec2, String instanceId) { try { RebootInstancesRequest request = RebootInstancesRequest.builder() .instanceIds(instanceId) .build(); ec2.rebootInstances(request); System.out.printf( "Successfully rebooted instance %s", instanceId); } catch (Ec2Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }

See the complete example on GitHub.

Describe 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.regions.Region; 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; import software.amazon.awssdk.services.ec2.model.Ec2Exception;

Code

public static void describeEC2Instances( Ec2Client ec2){ String nextToken = null; try { do { DescribeInstancesRequest request = DescribeInstancesRequest.builder().maxResults(6).nextToken(nextToken).build(); DescribeInstancesResponse response = ec2.describeInstances(request); for (Reservation reservation : response.reservations()) { for (Instance instance : reservation.instances()) { System.out.println("Instance Id is " + instance.instanceId()); System.out.println("Image id is "+ instance.imageId()); System.out.println("Instance type is "+ instance.instanceType()); System.out.println("Instance state name is "+ instance.state().name()); System.out.println("monitoring information is "+ instance.monitoring().state()); } } nextToken = response.nextToken(); } while (nextToken != null); } catch (Ec2Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }

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.

Monitor 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.regions.Region; import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.MonitorInstancesRequest; import software.amazon.awssdk.services.ec2.model.UnmonitorInstancesRequest;

Code

public static void monitorInstance( Ec2Client ec2, String instanceId) { MonitorInstancesRequest request = MonitorInstancesRequest.builder() .instanceIds(instanceId).build(); ec2.monitorInstances(request); System.out.printf( "Successfully enabled monitoring for instance %s", instanceId); }

See the complete example on GitHub.

Stop 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.regions.Region; import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.MonitorInstancesRequest; import software.amazon.awssdk.services.ec2.model.UnmonitorInstancesRequest;

Code

public static void unmonitorInstance(Ec2Client ec2, String instanceId) { UnmonitorInstancesRequest request = UnmonitorInstancesRequest.builder() .instanceIds(instanceId).build(); ec2.unmonitorInstances(request); System.out.printf( "Successfully disabled monitoring for instance %s", instanceId); }

See the complete example on GitHub.

More information