User guide
IpPermission ipPermission =
new IpPermission();
ipPermission.withIpRanges("111.111.111.111/32", "150.150.150.150/32")
.withIpProtocol("tcp")
.withFromPort(22)
.withToPort(22);
All the conditions that you specify in the IpPermission object must be met in order for ingress to
be allowed.
Specify the IP address using CIDR notation. If you specify the protocol as TCP/UDP, you must
provide a source port and a destination port.You can authorize ports only if you specify TCP or UDP.
2. Create and initialize an AuthorizeSecurityGroupIngressRequest instance. Use the withGroupName
method to specify the security group name, and pass the IpPermission object you initialized earlier
to the withIpPermissions method, as follows:
AuthorizeSecurityGroupIngressRequest authorizeSecurityGroupIngressRequest
=
new AuthorizeSecurityGroupIngressRequest();
authorizeSecurityGroupIngressRequest.withGroupName("JavaSecurityGroup")
.withIpPermissions(ipPermission);
3. Pass the request object into the authorizeSecurityGroupIngress method, as follows:
amazonEC2Client.authorizeSecurityGroupIngress(authorizeSecurityGroupIngress
Request);
If you call authorizeSecurityGroupIngress with IP addresses for which ingress is already
authorized, the method throws an exception. Create and initialize a new IpPermission object to
authorize ingress for different IPs, ports, and protocols before calling
AuthorizeSecurityGroupIngress.
Whenever you call the authorizeSecurityGroupIngress or authorizeSecurityGroupEgress methods,
a rule is added to your security group.You can add up to 100 rules per security group. For more information
about security groups, go to Security Group Concepts.
Before starting an Amazon EC2 instance, you need to create a key pair to allow you to log into your
instance. For information about creating a key pair, see Create a Key Pair (p. 26).
Create a Key Pair
Public AMI instances have no default password.To log into your Amazon EC2 instance, you must generate
an Amazon EC2 key pair. The key pair consists of a public key and a private key, and is not the same
as your AWS access credentials. For more information about Amazon EC2 key pairs, go to Getting an
SSH Key Pair.
Version v1.0.0
26
AWS SDK for Java Developer Guide
Starting an Amazon EC2 Instance