Monthly Archives: October 2015

Build AWS EC2 Client

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.

You can download the code at: aws-ec2-client and the related documentation at: aws-ec2-client-docs. Please, refer to the README file for the latest example code information. See also Import a Maven Project. You must also download the companion project at aws-auth-client and include it in the client app project. You can download the related documentation at: aws-auth-client-docs.

Application Internals

The following figure shows the application event trace:
aws ec2 client
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.

  1. Main. Instantiates the authenticated EC2 service client, initializes the operations and the UI classes.
  2. 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).
  3. UserInterface.  Defines the attributes and methods required to implement the SimpleUI} class.
  4. AwsClientAuthentication. Creates an authenticated client which is allowed to use the EC2 API.
  5. IEC2Client. Defines fields and methods to implement the Ec2ClientAuthentication class.
  6. IUtility. Defines fields and methods to implement the Utility class.
  7. Utility. Defines utility methods and variables to support the application operations such as menu creation, regions list initialization and so on.
  8. 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.

References

Build AWS S3 Client

This post shows how to create a Java console application to interact with Amazon S3 by using AWS Java SDK. For more information, see Using the AWS SDK for Java . A simple UI allows the user to perform tasks such as: list the buckets in the account, list objects in a bucket, create a bucket, create an object and so on.

This is a command line client that eliminates unnecessary clutter and shows the basic logic to interact with Amazon S3 . Hopefully this will help you to understand the syntax (and  semantic ) of the API.

A separate project handles the creation of an S3 authenticated client which is allowed to access the EC2 service REST API.

You can download the code here: aws-s3-client and the related code documentation here: aws-s3-client.zip.  Please, refer to README file for the latest example code information. See also Import a Maven Project. You must also download the companion project at aws-auth-client and include it in the client app project. You can download the related documentation at: aws-auth-client-docs.

Application Internals

Application Workflow

The following figure shows the application event trace.

aws s3 client event trace

 

The first time the user starts the application, the Main class performs the following actions:

  • Creates an authorized S3 client
  • Initializes the operations classes
  • Initializes the SimpleUI class
  • Starts the loop to process user inputs

The SimpleUI class loops to process the user’s commands 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.

  1. Main.  Gets authorization to access the S3 service, initializes the command classes. Delegates to the SimpleUI class the display of the selection menu and the processing of the user’s input.
  2. 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 S3 Java library method (which in turn calls the related REST API).
  3. AwsClientAuthentication. Creates Amazon S3 authenticated client.
  4. BucketOperations. Contains methods to perform S3 Bucket operations. The following code shows how to create a bucket, for example.
    public static void CreateBucket(String bucketName) throws IOException {        
             
      try {
            System.out.println("Creating bucket " + bucketName + "\n");
            // Create the bucket.
              s3Client.createBucket(bucketName);
          }
          catch (AmazonServiceException ase) {
            StringBuffer err = new StringBuffer();         
            err.append(("Caught an AmazonServiceException, which means your request made it "
                         + "to Amazon S3, but was rejected with an error response for some reason."));
            err.append(String.format("%n Error Message:  %s %n", ase.getMessage()));
            err.append(String.format(" HTTP Status Code: %s %n", ase.getStatusCode()));
            err.append(String.format(" AWS Error Code: %s %n", ase.getErrorCode()));
            err.append(String.format(" Error Type: %s %n", ase.getErrorType()));
            err.append(String.format(" Request ID: %s %n", ase.getRequestId()));
         }
         catch (AmazonClientException ace) {
                System.out.println("Caught an AmazonClientException, which means the client encountered "
                  + "a serious internal problem while trying to communicate with S3, "
                  + "such as not being able to access the network.");
                System.out.println("Error Message: " + ace.getMessage());
         }
    }
    
  5. ObjectOperations . Contains methods to perform S3 Object operations. The following code shows how to list objects in a bucket, for example.
       public static void listObject(String bucketName) throws IOException {          
             
            try {
                    System.out.println("Listing objects");
                    
                    ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
                        .withBucketName(bucketName)
                        .withPrefix("m");
                    ObjectListing objectListing;           
                    do {
                        objectListing = s3Client.listObjects(listObjectsRequest);
                        for (S3ObjectSummary objectSummary :
                            objectListing.getObjectSummaries()) {
                            System.out.println(" - " + objectSummary.getKey() + "  " +
                                    "(size = " + objectSummary.getSize() +
                                    ")");
                        }
                        listObjectsRequest.setMarker(objectListing.getNextMarker());
                    } while (objectListing.isTruncated());
            }
            catch (AmazonServiceException ase) {
                StringBuffer err = new StringBuffer();
             
                err.append(("Caught an AmazonServiceException, which means your request made it "
                      + "to Amazon S3, but was rejected with an error response for some reason."));
                err.append(String.format("%n Error Message:  %s %n", ase.getMessage()));
                err.append(String.format(" HTTP Status Code: %s %n", ase.getStatusCode()));
                err.append(String.format(" AWS Error Code: %s %n", ase.getErrorCode()));
                err.append(String.format(" Error Type: %s %n", ase.getErrorType()));
                err.append(String.format(" Request ID: %s %n", ase.getRequestId()));
             
            }
            catch (AmazonClientException ace) {
                System.out.println("Caught an AmazonClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with S3, "
                    + "such as not being able to access the network.");
                System.out.println("Error Message: " + ace.getMessage());
            }  
        }
       

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 = &amp;lt;your access key id&amp;gt;;
    aws_secret_access_key = &amp;lt;your secret key&amp;gt;;

For information on how to create the security credentials, see Create Access Credentials . See also Providing AWS Credentials in the AWS SDK for Java .

References