User guide
AWSCredentials credentials = new PropertiesCredentials(
AwsConsoleApp.class.getResourceAsStream("AwsCredentials.proper
ties"));
2. Use the AWSCredentials object to create a new AmazonEC2Client instance, as follows:
amazonEC2Client = new AmazonEC2Client(credentials);
3. By default, the service endpoint is ec2.us-east-1.amazonaws.com.To specify a different endpoint,
use the setEndpoint method. For example:
amazonEC2Client.setEndpoint("ec2.us-west-2.amazonaws.com");
For more information, see Regions and Endpoints.
The AWS SDK for Java uses US East (N. Virginia) as the default region. However, the AWS
Management Console uses US West (Oregon) as its default region.Therefore, when using the AWS
Management Console in conjunction with the SDK for Java, be sure to use the same region in both
your code and the console.
Create an Amazon EC2 Security Group
Create a security group, which acts as a virtual firewall that controls the network traffic for one or more
EC2 instances. By default, Amazon EC2 associates your instances with a security group that allows no
inbound traffic.You can create a security group that allows your EC2 instances to accept certain traffic.
For example, if you need to connect to a Linux instance, you must configure the security group to allow
SSH traffic.You can create a security group using the Amazon EC2 console or the SDK for Java.
You create a security group for use in either EC2-Classic or EC2-VPC. For more information about
EC2-Classic and EC2-VPC, see Supported Platforms in the Amazon EC2 User Guide for Linux Instances.
For more information about creating a security group using the Amazon EC2 console, see Amazon EC2
Security Groups in the Amazon EC2 User Guide for Linux Instances.
To create a security group
1. Create and initialize a CreateSecurityGroupRequest instance. Use the withGroupName method to
set the security group name, and the withDescription method to set the security group description,
as follows:
CreateSecurityGroupRequest csgr = new CreateSecurityGroupRequest();
csgr.withGroupName("JavaSecurityGroup").withDescription("My security group");
The security group name must be unique within the AWS region in which you initialize your Amazon
EC2 client.You must use US-ASCII characters for the security group name and description.
2. Pass the request object as a parameter to the createSecurityGroup method. The method returns a
CreateSecurityGroupResult object, as follows:
CreateSecurityGroupResult createSecurityGroupResult =
amazonEC2Client.createSecurityGroup(createSecurityGroupRequest);
Version v1.0.0
27
AWS SDK for Java Developer Guide
Tutorial: Starting an EC2 Instance