|
| 1 | +.. Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +
|
| 3 | + This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 |
| 4 | + International License (the "License"). You may not use this file except in compliance with the |
| 5 | + License. A copy of the License is located at http://creativecommons.org/licenses/by-nc-sa/4.0/. |
| 6 | +
|
| 7 | + This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 8 | + either express or implied. See the License for the specific language governing permissions and |
| 9 | + limitations under the License. |
| 10 | +
|
| 11 | +######################## |
| 12 | +Managing |EC2| Instances |
| 13 | +######################## |
| 14 | + |
| 15 | +Creating an Instance |
| 16 | +==================== |
| 17 | + |
| 18 | +Create a new |EC2| instance by calling the |ec2client|'s :methodname:`runInstances` method, |
| 19 | +providing it with a :aws-java-class:`RunInstancesRequest <services/ec2/model/RunInstancesRequest>` |
| 20 | +containing the :ec2-ug:`Amazon Machine Image (AMI) <AMIs>` to use and an :ec2-ug:`instance type |
| 21 | +<instance-types>`. |
| 22 | + |
| 23 | +**Imports** |
| 24 | + |
| 25 | +.. literalinclude:: example_code/ec2/src/main/java/aws/example/ec2/CreateInstance.java |
| 26 | + :lines: 16-20 |
| 27 | + |
| 28 | +**Code** |
| 29 | + |
| 30 | +.. literalinclude:: example_code/ec2/src/main/java/aws/example/ec2/CreateInstance.java |
| 31 | + :lines: 44-54 |
| 32 | + :dedent: 8 |
| 33 | + |
| 34 | +See the :sdk-examples-java-ec2:`complete example <CreateInstance.java>`. |
| 35 | + |
| 36 | + |
| 37 | +Starting an Instance |
| 38 | +==================== |
| 39 | + |
| 40 | +To start an |EC2| instance, call the |ec2client|'s :methodname:`startInstances` method, providing it |
| 41 | +with a :aws-java-class:`StartInstancesRequest <services/ec2/model/StartInstancesRequest>` containing |
| 42 | +the ID of the instance to start. |
| 43 | + |
| 44 | +**Imports** |
| 45 | + |
| 46 | +.. literalinclude:: example_code/ec2/src/main/java/aws/example/ec2/StartStopInstance.java |
| 47 | + :lines: 16-17, 20 |
| 48 | + |
| 49 | +**Code** |
| 50 | + |
| 51 | +.. literalinclude:: example_code/ec2/src/main/java/aws/example/ec2/StartStopInstance.java |
| 52 | + :lines: 30-31, 49-52 |
| 53 | + :dedent: 8 |
| 54 | + |
| 55 | +See the :sdk-examples-java-ec2:`complete example <StartStopInstance.java>`. |
| 56 | + |
| 57 | + |
| 58 | +Stopping an Instance |
| 59 | +==================== |
| 60 | + |
| 61 | +To stop an |EC2| instance, call the |ec2client|'s :methodname:`stopInstances` method, providing it |
| 62 | +with a :aws-java-class:`StopInstancesRequest <services/ec2/model/StopInstancesRequest>` containing |
| 63 | +the ID of the instance to stop. |
| 64 | + |
| 65 | +**Imports** |
| 66 | + |
| 67 | +.. literalinclude:: example_code/ec2/src/main/java/aws/example/ec2/StartStopInstance.java |
| 68 | + :lines: 16-17, 21 |
| 69 | + |
| 70 | +**Code** |
| 71 | + |
| 72 | +.. literalinclude:: example_code/ec2/src/main/java/aws/example/ec2/StartStopInstance.java |
| 73 | + :lines: 59-60, 77-80 |
| 74 | + :dedent: 8 |
| 75 | + |
| 76 | +See the :sdk-examples-java-ec2:`complete example <StartStopInstance.java>`. |
| 77 | + |
| 78 | + |
| 79 | +Rebooting an Instance |
| 80 | +===================== |
| 81 | + |
| 82 | +To reboot an |EC2| instance, call the |ec2client|'s :methodname:`rebootInstances` method, providing it |
| 83 | +with a :aws-java-class:`RebootInstancesRequest <services/ec2/model/RebootInstancesRequest>` containing |
| 84 | +the ID of the instance to reboot. |
| 85 | + |
| 86 | +**Imports** |
| 87 | + |
| 88 | +.. literalinclude:: example_code/ec2/src/main/java/aws/example/ec2/RebootInstance.java |
| 89 | + :lines: 16-19 |
| 90 | + |
| 91 | +**Code** |
| 92 | + |
| 93 | +.. literalinclude:: example_code/ec2/src/main/java/aws/example/ec2/RebootInstance.java |
| 94 | + :lines: 39-44 |
| 95 | + :dedent: 8 |
| 96 | + |
| 97 | +See the :sdk-examples-java-ec2:`complete example <RebootInstance.java>`. |
| 98 | + |
| 99 | + |
| 100 | +Describing Instances |
| 101 | +==================== |
| 102 | + |
| 103 | +To list your instances, create a :aws-java-class:`DescribeInstancesRequest |
| 104 | +<services/ec2/model/DescribeInstancesRequest>` and call the |ec2client|'s |
| 105 | +:methodname:`describeInstances` method. It will return a :aws-java-class:`DescribeInstancesResult |
| 106 | +<services/ec2/model/DescribeInstancesResult>` object that you can use to list the |EC2| instances |
| 107 | +for your account and region. |
| 108 | + |
| 109 | +Instances are grouped by *reservation*. Each reservation corresponds to the call to |
| 110 | +:methodname:`startInstances` that launched the instance. To list your instances, you must first call |
| 111 | +the :classname:`DescribeInstancesResult` class' :methodname:`getReservations' method, and then call |
| 112 | +:methodname:`getInstances` on each returned Reservation object. |
| 113 | + |
| 114 | +**Imports** |
| 115 | + |
| 116 | +.. literalinclude:: example_code/ec2/src/main/java/aws/example/ec2/DescribeInstances.java |
| 117 | + :lines: 16-21 |
| 118 | + |
| 119 | +**Code** |
| 120 | + |
| 121 | +.. literalinclude:: example_code/ec2/src/main/java/aws/example/ec2/DescribeInstances.java |
| 122 | + :lines: 30-58 |
| 123 | + :dedent: 8 |
| 124 | + |
| 125 | +Results are paged; you can get further results by passing the value returned from the result |
| 126 | +object's :methodname:`getNextToken` method to your original request object's |
| 127 | +:methodname:`setNextToken` method, then using the same request object in your next call to |
| 128 | +:methodname:`describeInstances`. |
| 129 | + |
| 130 | +See the :sdk-examples-java-ec2:`complete example <DescribeInstances.java>`. |
| 131 | + |
| 132 | + |
| 133 | +Monitoring an Instance |
| 134 | +====================== |
| 135 | + |
| 136 | +You can monitor various aspects of your |EC2| instances, such as CPU and network utilization, |
| 137 | +available memory, and disk space remaining. To learn more about instance monitoring, see |
| 138 | +:ec2-ug:`Monitoring Amazon EC2 <monitoring_ec2>` in the |ec2-ug|. |
| 139 | + |
| 140 | +To start monitoring an instance, you must create a :aws-java-class:`MonitorInstancesRequest |
| 141 | +<services/ec2/model/MonitorInstancesRequest>` with the ID of the instance to monitor, and pass it to |
| 142 | +the |ec2client|'s :methodname:`monitorInstances` method. |
| 143 | + |
| 144 | +**Imports** |
| 145 | + |
| 146 | +.. literalinclude:: example_code/ec2/src/main/java/aws/example/ec2/MonitorInstance.java |
| 147 | + :lines: 16-18 |
| 148 | + |
| 149 | +**Code** |
| 150 | + |
| 151 | +.. literalinclude:: example_code/ec2/src/main/java/aws/example/ec2/MonitorInstance.java |
| 152 | + :lines: 30-31, 50-53 |
| 153 | + :dedent: 8 |
| 154 | + |
| 155 | +See the :sdk-examples-java-ec2:`complete example <MonitorInstance.java>`. |
| 156 | + |
| 157 | + |
| 158 | +Stopping Instance Monitoring |
| 159 | +============================ |
| 160 | + |
| 161 | +To stop monitoring an instance, create an :aws-java-class:`UnmonitorInstancesRequest |
| 162 | +<services/ec2/model/UnmonitorInstancesRequest>` with the ID of the instance to stop monitoring, and |
| 163 | +pass it to the |ec2client|'s :methodname:`unmonitorInstances` method. |
| 164 | + |
| 165 | +**Imports** |
| 166 | + |
| 167 | +.. literalinclude:: example_code/ec2/src/main/java/aws/example/ec2/MonitorInstance.java |
| 168 | + :lines: 16-17, 19 |
| 169 | + |
| 170 | +**Code** |
| 171 | + |
| 172 | +.. literalinclude:: example_code/ec2/src/main/java/aws/example/ec2/MonitorInstance.java |
| 173 | + :lines: 62-63, 82-85 |
| 174 | + :dedent: 8 |
| 175 | + |
| 176 | +See the :sdk-examples-java-ec2:`complete example <MonitorInstance.java>`. |
| 177 | + |
| 178 | + |
| 179 | +More Information |
| 180 | +================ |
| 181 | + |
| 182 | +* :ec2-api:`RunInstances` in the |ec2-api| |
| 183 | +* :ec2-api:`DescribeInstances` in the |ec2-api| |
| 184 | +* :ec2-api:`StartInstances` in the |ec2-api| |
| 185 | +* :ec2-api:`StopInstances` in the |ec2-api| |
| 186 | +* :ec2-api:`RebootInstances` in the |ec2-api| |
| 187 | +* :ec2-api:`DescribeInstances` in the |ec2-api| |
| 188 | +* :ec2-api:`MonitorInstances` in the |ec2-api| |
| 189 | +* :ec2-api:`UnmonitorInstances` in the |ec2-api| |
| 190 | + |
| 191 | + |
| 192 | + |
0 commit comments