This topic shows how to create a Java console application to interact with Amazon EC2 by using AWS Java SDK. For more information, see Using the AWS SDK for Java. This is a command line client that eliminates unnecessary clutter and shows the basic logic to interact with Amazon EC2 . Hopefully this will help you to understand the syntax (and semantic ) of the API.
A separate project handles the creation of an EC2 authenticated client which is allowed to access the EC2 service REST API.
Application Internals
The following figure shows the application event trace:
A simple UI allows the user to perform tasks such as: create EC2 instances, list instances, assign instance attributes and so on.
The first time the user starts the application, the Main class performs the following tasks:
- Creates an authorized EC2 client
- Initializes the EC2 operations class
- Initializes the SimpleUI class
- Starts the loop to process the user’s input
The SimpleUI class loops to process the user’s input until the loop is exited. At that point, the application terminates.
Modify the pom.xml File
A key step in creating the application project is to configure the pom.xml file correctly to define the dependencies required to implement the client application. You can find the file at: pom.xml.
Application Components
This section describes the components of the application and delegates the details to the actual code implementation.
- Main. Instantiates the authenticated EC2 service client, initializes the operations and the UI classes.
- SimpleUI. Displays the menu of choices for the user. It processes the user’s input and calls the proper function based on the user’s selection. Each function calls the related AWS EC2 Java library method (which in turn calls the related REST API).
- UserInterface. Defines the attributes and methods required to implement the SimpleUI} class.
- AwsClientAuthentication. Creates an authenticated client which is allowed to use the EC2 API.
- IEC2Client. Defines fields and methods to implement the Ec2ClientAuthentication class.
- IUtility. Defines fields and methods to implement the Utility class.
- Utility. Defines utility methods and variables to support the application operations such as menu creation, regions list initialization and so on.
- EC2Operations. Performs EC2 operations selected by the user. The various methods call the related EC2 library functions that in turn call the REST APIs which interact with the EC2 service. The following example code shows how to get available instances associated with a specific key pair.
public static void getInstancesInformation(String keyName) { List<Instance> resultList = new ArrayList<Instance>(); DescribeInstancesResult describeInstancesResult = ec2Client.describeInstances(); List<Reservation> reservations = describeInstancesResult.getReservations(); for (Iterator<Reservation> iterator = reservations.iterator(); iterator.hasNext();) { Reservation reservation = iterator.next(); for (Instance instance : reservation.getInstances()) { if (instance.getKeyName().equals(keyName)) resultList.add(instance); } } displayInstancesInformation(resultList); }
Security Access Credentials
You need to set up your AWS security credentials before the sample code is able to connect to AWS. You can do this by creating a file named “credentials” at ~/.aws/ (C:\Users\USER_NAME.aws\ for Windows users) and saving the following lines in the file
[default] aws_access_key_id = <your access key id> aws_secret_access_key = <your secret key>
For more information, see Providing AWS Credentials in the AWS SDK for Java.