Skip to content

Commit 9eb0c05

Browse files
committed
add more EC2 examples
1 parent d53066e commit 9eb0c05

9 files changed

+612
-12
lines changed

doc_source/_includes.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
.. _mvnlong: Maven_
2525

2626
.. |iamclient| replace:: :aws-java-class:`AmazonIdentityManagementClient <services/identitymanagement/AmazonIdentityManagementClient>`
27-
.. |s3client| replace:: :aws-java-class:`AmazonS3 <services/s3/AmazonS3>`
28-
.. |sqsclient| replace:: :aws-java-class:`AmazonSQS <services/sqs/AmazonSQS>`
29-
.. |ddbclient| replace:: :aws-java-class:`AmazonDynamoDB <services/dynamodbv2/AmazonDynamoDB>`
27+
.. |ec2client| replace:: :aws-java-class:`AmazonEC2Client <services/ec2/AmazonEC2Client>`
28+
.. |s3client| replace:: :aws-java-class:`AmazonS3 <services/s3/AmazonS3Client>`
29+
.. |sqsclient| replace:: :aws-java-class:`AmazonSQS <services/sqs/AmazonSQSClient>`
30+
.. |ddbclient| replace:: :aws-java-class:`AmazonDynamoDB <services/dynamodbv2/AmazonDynamoDBClient>`
3031
.. |xfermgr| replace:: :aws-java-class:`TransferManager <services/s3/transfer/TransferManager>`
3132

3233

doc_source/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@
391391

392392
# links to examples for a particular service (ex: :sdk-examples-java-s3:`ListTables.java`)
393393
samples_url = 'https://github.com/awsdocs/aws-doc-sdk-examples/'
394-
for svc in ['s3', 'dynamodb', 'sqs', 'iam']:
394+
for svc in [ 'dynamodb', 'ec2', 'iam', 's3', 'sqs' ]:
395395
extlinks['sdk-examples-java-%s' % svc] = (samples_url +
396396
'blob/master/java/example_code/{svc}/src/main/java/aws/example/{svc}/'.format(svc=svc) +
397397
'%s', '')

doc_source/document-history.rst

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ This topic describes important changes to the |sdk-java-dg| over the course of i
1616

1717
**This documentation was built on:** |today|
1818

19+
Mar 27, 2017
20+
Added more |EC2| examples to the :doc:`prog-services-ec2` section: :doc:`examples-ec2-instances`,
21+
:doc:`examples-ec2-elastic-ip`, :doc:`examples-ec2-regions-zones`, :doc:`examples-ec2-key-pairs`, and
22+
:doc:`examples-ec2-security-groups`.
23+
1924
Mar 21, 2017
2025
Added a new set of |IAM| examples to the :doc:`examples-iam` section:
21-
22-
* :doc:`examples-iam-access-keys`
23-
* :doc:`examples-iam-users`
24-
* :doc:`examples-iam-account-aliases`
25-
* :doc:`examples-iam-policies`
26-
* :doc:`examples-iam-server-certificates`
26+
:doc:`examples-iam-access-keys`, :doc:`examples-iam-users`, :doc:`examples-iam-account-aliases`,
27+
:doc:`examples-iam-policies`, and :doc:`examples-iam-server-certificates`
2728

2829
Mar 13, 2017
2930
Added three new topics to the |SQS| section: :doc:`examples-sqs-long-polling`,
@@ -125,5 +126,3 @@ Sep 9, 2013
125126
This topic, *Document History*, tracks changes to the |sdk-java-dg|. It is intended as a
126127
companion to the release notes history.
127128

128-
129-
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
Using Elastic IP Addresses in |EC2|
13+
###################################
14+
15+
.. meta::
16+
:description: How to manage IAM access keys with the AWS SDK for Java.
17+
:keywords: AWS SDK for Java, code examples, EC2 security groups,
18+
19+
Allocating an Elastic IP Address
20+
================================
21+
22+
To use an Elastic IP address, you first allocate one to your account, and then associate it with
23+
your instance or a network interface.
24+
25+
To allocate an Elastic IP address, call the |ec2client|'s :methodname:`allocateAddress` method with
26+
an :aws-java-class:`AllocateAddressRequest <services/ec2/model/AllocateAddressRequest>` object
27+
containing the network type (classic EC2 or VPC).
28+
29+
The returned :aws-java-class:`AllocateAddressResult <services/ec2/model/AllocateAddressResult>`
30+
contains an allocation ID that you can use to associate the address with an instance, by passing the
31+
allocation ID and instance ID in a :aws-java-class:`AssociateAddressRequest
32+
<services/ec2/model/AssociateAddressRequest>` to the |ec2client|'s :methodname:`associateAddress`
33+
method.
34+
35+
**Imports**
36+
37+
.. literalinclude:: example_code/ec2/src/main/java/aws/example/ec2/AllocateAddress.java
38+
:lines: 16-22
39+
40+
**Code**
41+
42+
.. literalinclude:: example_code/ec2/src/main/java/aws/example/ec2/AllocateAddress.java
43+
:lines: 42-58
44+
:dedent: 8
45+
46+
See the :sdk-examples-java-ec2:`complete example <AllocateAddress.java>`.
47+
48+
49+
Describing Elastic IP Addresses
50+
===============================
51+
52+
To list the Elastic IP addresses assigned to your account, call the |ec2client|'s
53+
:methodname:`describeAddresses` method. It returns a :aws-java-class:`DescribeAddressesResult
54+
<services/ec2/model/DescribeAddressesResult>` which you can use to get a list of
55+
:aws-java-class:`Address <services/ec2/model/Address>` objects that represent the Elastic IP
56+
addresses on your account.
57+
58+
**Imports**
59+
60+
.. literalinclude:: example_code/ec2/src/main/java/aws/example/ec2/DescribeAddresses.java
61+
:lines: 16-19
62+
63+
**Code**
64+
65+
.. literalinclude:: example_code/ec2/src/main/java/aws/example/ec2/DescribeAddresses.java
66+
:lines: 28-42
67+
:dedent: 8
68+
69+
See the :sdk-examples-java-ec2:`complete example <DescribeAddresses.java>`.
70+
71+
72+
Releasing an Elastic IP Address
73+
===============================
74+
75+
To release an Elastic IP address, call the |ec2client|'s :methodname:`releaseAddress` method,
76+
passing it a :aws-java-class:`ReleaseAddressRequest <services/ec2/model/ReleaseAddressRequest>`
77+
containing the allocation ID of the Elastic IP address you want to release.
78+
79+
**Imports**
80+
81+
.. literalinclude:: example_code/ec2/src/main/java/aws/example/ec2/ReleaseAddress.java
82+
:lines: 16-19
83+
84+
**Code**
85+
86+
.. literalinclude:: example_code/ec2/src/main/java/aws/example/ec2/ReleaseAddress.java
87+
:lines: 39-44
88+
:dedent: 8
89+
90+
After you release an Elastic IP address, it is released to the AWS IP address pool and might be
91+
unavailable to you afterward. Be sure to update your DNS records and any servers or devices that
92+
communicate with the address. If you attempt to release an Elastic IP address that you already
93+
released, you'll get an *AuthFailure* error if the address is already allocated to another AWS
94+
account.
95+
96+
If you are using *EC2-Classic* or a *default VPC*, then releasing an Elastic IP address
97+
automatically disassociates it from any instance that it's associated with. To disassociate an
98+
Elastic IP address without releasing it, use the |ec2client|'s :methodname:`disassociateAddress`
99+
method.
100+
101+
If you are using a non-default VPC, you *must* use :methodname:`disassociateAddress` to disassociate
102+
the Elastic IP address before you try to release it. Otherwise, |EC2| returns an error
103+
(*InvalidIPAddress.InUse*).
104+
105+
See the :sdk-examples-java-ec2:`complete example <ReleaseAddress.java>`.
106+
107+
108+
More Information
109+
================
110+
111+
* :ec2-ug:`Elastic IP Addresses <elastic-ip-addresses-eip>` in the |ec2-ug|
112+
* :ec2-api:`AllocateAddress` in the |ec2-api|
113+
* :ec2-api:`DescribeAddresses` in the |ec2-api|
114+
* :ec2-api:`ReleaseAddress` in the |ec2-api|
115+
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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

Comments
 (0)