OWASP O2 Platform Blog

O2 Script to create an Amazon EC2 Image

If you want to automate the creation of an EC2 image  (for example the O2 Platform one) , here is a script that does that:

Gui Screenshot:

Source Code:


//var topPanel = panel.clear().add_Panel();
var topPanel = "start amazon instance".popupWindow();
var apiKey = @"C:\....\accounts.xml".credential("AmazonAWS");

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

var width = 400;
var height = 150;

var createPanel = topPanel.add_GroupBox("Create amazon EC2 Instance")
.fill(false)
.width(width).height(height)
.add_Panel();
var imageID = createPanel.add_Label("Image Id:",5,0)
.append_Control<ComboBox>()
.width(200)
.top(0).left(90);
imageID.append_Link("View Image's List",
()=>amazonEC2.show_ImagesList_In_TreeView(ec2Client))
.top(5);

var imageSize =createPanel.add_Label("Image Size:",30,0)
.append_Control<ComboBox>()
.dropDownList()
.top(25).left(90);

var securityGroup = createPanel.add_TextBox(60,"Security Group","").left(90).width(120);
var keyName = createPanel.add_TextBox(80,"Pem Key name","").left(90).width(120);

var defaultImages = new Dictionary<string,string>();
defaultImages.add("O2 Platform Image", "OWASP O2 Platform - Windows 2008")
.add("Amazon Win 2008 32bit IIS+SQL", "Windows-Server2008-i386-SqlExpress-v103")
.add("Amazon Win 2008 64bit IIS+SQL", "Windows-2008R2-SP1-English-SQLExpress-v101")
.add("Amazon Win 2008 64bit IIS+SQL", "Windows-Server2003R2-i386-SqlExpress-v109")
.add("Microsoft Win 2008 64bit IIS+SQL", "Optimized-SQL-Server-2008R2-3-2-11");

imageID.add_Items(defaultImages.keys());

imageSize.add_Items(new List<string> {"m1.small", "t1.micro", "c1.medium","m1.large", "m1.xlarge"});
imageID.select_Item(0);
imageSize.select_Item(0);

securityGroup.set_Text("O2 Boxes");
keyName.set_Text("O2 Key");

var statusMessage = createPanel.add_Label("...", 90,230);
createPanel.add_Button("Launch Image", 60,230)
.align_Right(createPanel).widthAdd(-10)
.onClick(()=>{
statusMessage.set_Text("...launching image").textColor(Color.DarkBlue);
try
{
var imageId = imageID.get_Text();
if (defaultImages.hasKey(imageId))
imageId = amazonEC2.getImagesList(ec2Client).name(defaultImages[imageId]).ImageId;
"using imageId: {0}".info(imageId);
var runningInstance = ec2Client.launchInstance(imageId,
imageSize.get_Text(),
securityGroup.get_Text(),
keyName.get_Text());
statusMessage.set_Text("done, created instance: {0}".format(runningInstance.InstanceId)).textColor(Color.DarkGreen);
}
catch(Exception ex)
{
statusMessage.set_Text(ex.Message).textColor(Color.Red);
ex.log();
}

});
return "done";

//here are a number of code snippets that show the multiple methods available in the API_AmazonEC2

//using Amazon.EC2.Model
//using Amazon.EC2

//return images.description("WebMatrix Hosting Server provided by Microsoft");
//return images.description_Contains("Amazon Linux");
//return images.amazon().description_RegEx("windows.*32");

//return images.name_Contains("amzn-ami");
//return images.name_RegEx("amzn.*i386");

//return images.imageId(".....");
//return images.imageId("ami-9d1043d8");
//return <a href="mailto:images.@public().amazon().windows">images.@public().amazon().windows</a>();
//return <a href="mailto:images.@public().amazon()._32Bit">images.@public().amazon()._32Bit</a>();
//return <a href="mailto:images.@public().amazon()._64Bit">images.@public().amazon()._64Bit</a>();
//return <a href="mailto:images.@public().amazon">images.@public().amazon</a>();
//return <a href="mailto:images.@public().microsoft">images.@public().microsoft</a>();
//return <a href="mailto:images.@public()._32Bit">images.@public()._32Bit</a>();
//return <a href="mailto:images.@public()._64Bit">images.@public()._64Bit</a>();
//return <a href="mailto:images.@private">images.@private</a>();
//return <a href="mailto:images.@private">images.@private</a>();
//amazonEC2.show_ImagesList_In_TreeView(ec2Client,topPanel);

return "ok";
//O2File:API_AmazonEC2.cs
//O2Ref:AWSSDK.dll

O2 Platform EC2 Image

Note: if you want to use the Amazon Web Interface to start the O2 AMI, here is how to find it (NOTE that this is in US West):

November 23, 2011 Posted by | EC2 | Leave a comment