OWASP O2 Platform Blog

Using the O2’s Amazon EC2 API

Here are a number of code snippets that show how to use the O2’s Amazon EC2 API.

All scripts should be executed from an O2 Script Editor (ideally a ‘Quick Development Gui’ since we will be using the provided panel in a couple examples:

 

Getting a fully logged-in AmazonEC2Client instance

var apiKey = @"C:\O2\_USERDATA\accounts.xml".credential("AmazonAWS");
   
var amazonEC2 = new API_AmazonEC2(apiKey);
var ec2Client = amazonEC2.getEC2Client();
return ec2Client.typeName();
//O2File:API_AmazonEC2.cs
//O2Ref:AWSSDK.dll

 

Getting a fully logged-in AmazonEC2Client instance in a specific region

The previous example connects to the default region on this API, which is  set to

 DefaultRegion = "us-west-1"; 

If you want to open a specific region, you can specify it as a parameter to the getEC2Client method. This next example returns is equivalent to the previous one

var amazonEC2 = new API_AmazonEC2(apiKey);
var ec2Client = amazonEC2.getEC2Client(amazonEC2.DefaultRegion);
return ec2Client;

and this one will connect to the europan EC2 data center:

var amazonEC2 = new API_AmazonEC2(apiKey);
var ec2Client = amazonEC2.getEC2Client("eu-west-1");
return ec2Client;

 

Retrieve list of Instances in default region

var amazonEC2 = new API_AmazonEC2(apiKey);
return amazonEC2.getEC2Instances(); 

 

Retrieve list of Instances in all regions

var amazonEC2 = new API_AmazonEC2(apiKey);
return amazonEC2.getEC2Instances(false);  

 

Visualize in a TreeView the data received from the getEC2Instances

var amazonEC2 = new API_AmazonEC2(apiKey);
var instancesData =  amazonEC2.getEC2Instances();
var instancesViewer = topPanel.add_TreeView();
foreach(var instanceMapping in instancesData)
    instancesViewer.add_Node(instanceMapping.Key)                 
                   .add_Nodes(instanceMapping.Value ,
                                (runningInstance)=>amazonEC2.getRunningInstanceSignature(runningInstance));

  

 

Get an Instance Object

To get an instance object you will need to drill the object returned from the getEC2Instances method

var amazonEC2 = new API_AmazonEC2(apiKey);
var instance =  amazonEC2.getEC2Instances().Values.First()[0];
return instance;

  

View Instance details – Mode 1

var amazonEC2 = new API_AmazonEC2(apiKey);
var instancesData =  amazonEC2.getEC2Instances();
var instance =  amazonEC2.getEC2Instances().Values.First()[0];
instance.showInfo();

 

View instance details – Mode 2

var amazonEC2 = new API_AmazonEC2(apiKey);
var instancesData =  amazonEC2.getEC2Instances();
var instance =  amazonEC2.getEC2Instances().Values.First()[0];
instance.details();

 

Starting and Stopping Instances

var amazonEC2 = new API_AmazonEC2(apiKey);
var instancesData =  amazonEC2.getEC2Instances();
var instance =  amazonEC2.getEC2Instances().Values.First()[0];
amazonEC2.startInstance(instance);
amazonEC2.stopInstance(instance); 

 

Viewing Console Out in separate window

var amazonEC2 = new API_AmazonEC2(apiKey);
var instancesData =  amazonEC2.getEC2Instances();
var instance =  amazonEC2.getEC2Instances()["SI Boxes"][1];
amazonEC2.showConsoleOut(instance);

Retrieving Instance Password

var amazonEC2 = new API_AmazonEC2(apiKey);
var instancesData =  amazonEC2.getEC2Instances();
var instance =  amazonEC2.getEC2Instances()["SI Boxes"][2];
amazonEC2.ApiRsa = new API_RSA();
return amazonEC2.getPassword(instance);


 If you don’t provide the path of a PEM file in the API_RSA(…) constructor you will be prompt for it

once you provide the PEM key, you can see the fetched data in the Log Viewer

or in the case of the script above, in the Invoke and Result output TextArea

 

April 16, 2011 - Posted by | EC2

1 Comment »

  1. […] just added a couple new methods to the O2′s Amazon EC2 API which provide details about the available AMIs (Amazon Machine […]

    Pingback by Script to view Amazon EC2 Images List « O2Platform.com for Developers | April 16, 2011 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: