User guide

ArrayList<IpPermission> ipPermissions = new ArrayList<IpPermission> ();
45 IpPermission ipPermission = new IpPermission();
ipPermission.setIpProtocol("tcp");
ipPermission.setFromPort(new Integer(22));
ipPermission.setToPort(new Integer(22));
ipPermission.setIpRanges(ipRanges);
50 ipPermissions.add(ipPermission);
try {
// Authorize the ports to the used.
AuthorizeSecurityGroupIngressRequest ingressRequest =
55 new AuthorizeSecurityGroupIngressRequest("GettingStartedGroup",ip
Permissions);
ec2.authorizeSecurityGroupIngress(ingressRequest);
} catch (AmazonServiceException ase) {
// Ignore because this likely means the zone has already
// been authorized.
60 System.out.println(ase.getMessage());
}
You can view this entire code sample in the advanced.CreateSecurityGroupApp.java code sample.
Note you only need to run this application once to create a new security group.
You can also create the security group using the AWS Toolkit for Eclipse. Go to the toolkit documentation
for more information.
Detailed Spot Instance Request Creation Options
As we explained in Tutorial: Amazon EC2 Spot Instances (p. 35), you need to build your request with an
instance type, an Amazon Machine Image (AMI), and maximum bid price.
Let's start by creating a RequestSpotInstanceRequest object.The request object requires the number
of instances you want and the bid price. Additionally, we need to set the LaunchSpecification for the
request, which includes the instance type, AMI ID, and security group you want to use. After the request
is populated, we call the requestSpotInstances method on the AmazonEC2Client object. An example
of how to request a Spot instance follows.
(The following code is the same as what we used in the first tutorial.)
1
// Retrieves the credentials from an AWSCredentials.properties file.
AWSCredentials credentials = null;
try {
5 credentials = new PropertiesCredentials(
GettingStartedApp.class.getResourceAsStream("AwsCredentials.proper
ties"));
} catch (IOException e1) {
System.out.println("Credentials were not properly entered into AwsCre
dentials.properties.");
System.out.println(e1.getMessage());
10 System.exit(-1);
}
// Create the AmazonEC2Client object so we can call various APIs.
AmazonEC2 ec2 = new AmazonEC2Client(credentials);
15
Version v1.0.0
47
AWS SDK for Java Developer Guide
Tutorial: Advanced Amazon EC2 Spot Request
Management