Integrating Amazon EC2 in Java via NetBeans IDE

Posted by Geertjan on Oracle Blogs See other posts from Oracle Blogs or by Geertjan
Published on Sun, 8 Jun 2014 14:05:29 +0000 Indexed on 2014/06/08 15:35 UTC
Read the original article Hit count: 196

Filed under:

Next, having looked at Amazon Associates services and Amazon S3, let's take a look at Amazon EC2, the elastic compute cloud which provides remote computing services. I started by launching an instance of Ubuntu Server 14.04 on Amazon EC2, which looks a bit like this in the on-line AWS Management Console, though I whitened out most of the details:

Now that I have at least one running instance available on Amazon EC2, it makes sense to use the services that are integrated into NetBeans IDE: 

I created a new application with one class, named "AmazonEC2Demo". Then I dragged the "describeInstances" service that you see above, with the mouse, into the class. Then the IDE automatically created all the other files you see below, i.e., 4 Java classes and one properties file:

In the properties file, register the access ID and secret keys. These are read by the other generated Java classes. Signing and authentication are done automatically by the code that is generated, i.e., there's nothing generic you need to do and you can immediately begin working on your domain-specific code.

Finally, you're now able to rewrite the code in "AmazonEC2Demo" to connect to Amazon EC2 and obtain information about your running instance:

public class AmazonEC2Demo {
    public static void main(String[] args) {
        String instanceId1 = "i-something";
        RestResponse result;
        try {
            result = AmazonEC2Service.describeInstances(instanceId1);
            System.out.println(result.getDataAsString());
        } catch (IOException ex) {
            Logger.getLogger(AmazonEC2Demo.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From the above, you'll receive a chunk of XML with data about the running instance, it's name, status, dates, etc. In other words, you're now ready to integrate Amazon EC2 features directly into the applications you're writing, without very much work to get started. Within about 5 minutes, you're working on your business logic, rather than on the generic code that anyone needs when integrating with Amazon EC2.

© Oracle Blogs or respective owner

Related posts about /NetBeans IDE