Skip to content

Powershell: Launching Amazon EC2 instance as Web UI test server

March 31, 2011

Hi.

As I mentioned in previous post how to deploy .net application on remote machine. So there is a way how to launch temporary Amazon EC2 instance for Web UI testing. Firstly you need to configure Amazon EC2 instance and save it as image(AMIs). Then apply script below:


Add-Type -Path "%path-to-amazon-api-dll%\AWSSDK.dll"

$AWSSecretKey = "%aws-secret-key%"
$AWSAccessKey = "%aws-access-key%"

# web ui image description
$maxCount = 1
$minCount = 1
$imageId = "ami-xxxxxx" #your preconfigured AMI ID
$instType = "m1.small"

# file name to save launched server instance-id
$saveFileName = "%path-to-save%\instanceid.txt"
$instanceFileName = "%path-to-save%\instance.txt"

# ==================================================

$client = [Amazon.AWSClientFactory]::CreateAmazonEC2Client($AWSAccessKey, $AWSSecretKey)

echo 'Launching temporary Web UI server.'

$runRequest = new-object Amazon.EC2.Model.RunInstancesRequest
$runRequest.ImageId = $imageId
$runRequest.MaxCount = $maxCount
$runRequest.MinCount = $minCount
$runRequest.InstanceType = $instType

$runResp = $client.RunInstances($runRequest)
$runResult = $runResp.RunInstancesResult.Reservation.RunningInstance[0].InstanceId

$runResult > $instanceFileName

$tmp = 1
$ip = ""

# awaiting for Public DNS
while ($tmp -eq 1)
{
 try
 {
 sleep(5)
 $ipReq = New-Object Amazon.EC2.Model.DescribeInstancesRequest
 $ipReq.InstanceId.Add($runResult)
 $ipResp = $client.DescribeInstances($ipReq)
 $ip = $ipResp.DescribeInstancesResult.Reservation[0].RunningInstance[0].PublicDnsName

 if($ip.Length -gt 0)
 {
 $tmp = 0
 }
 }
 catch{}
}

echo "Web UI server Amazon instance Public DNS: $ip"
echo 'Saving to file...'
$ip > $saveFileName
echo 'Saved.'

$status = ""
# awaiting for instance to launch
while ($status -ne "Success")
{
 sleep(30)
 $ping = New-Object System.Net.NetworkInformation.Ping
 $reply = $ping.Send($ip)
 $status = $reply.Status
}

echo "Web UI Server is ready to use."
sleep(180)

Later on I’m going to describe how I applied CI for our latest project (getting sources;building;unit testing;remote ui testing and so on).

Advertisements

From → Automation

Leave a Comment

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: