Hello world!

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!

Posted in Uncategorized | 1 Comment

Using Powershell with RightScale’s API

Here’s a quick example how you can consume RightScale’s API via Powershell to start a server:

function StartServer ([string] $serverid)

{

      # Create httpClient with the REST command

      $restCommand = $BaseAPIURL + "/servers/" + $serverid + "/start";

      $httpClient = [System.Net.WebRequest]::Create($restCommand);

     

     

      # Add Authorization headers

      $authbytes = [System.Text.Encoding]::ASCII.GetBytes($username + ":" + $password)

      $base64 = [System.Convert]::ToBase64String($authbytes) 

      $authorization = "Authorization: Basic " + $base64

      $httpClient.Headers.Add($authorization)

     

      # Set Method and API Version

      $httpClient.Method="POST"

      $httpClient.Headers.Add("X-API-VERSION: 1.0")

     

      # Execute the REST command and output the response headers

      $response = $httpClient.GetResponse()

      $response

     

}

 

 

$Username = "<username@domain.com>"

$Password = "<password>"

$Account  = "<accountid>"

$API_VERSION = "1.0"

 

$BaseURL = "https://my.rightscale.com/api&quot;

$BaseAPIURL = $BaseURL + "/acct/" + $Account;

 

StartServer("<serverid>")

Posted in Uncategorized | Leave a comment

Using Amazon EC2 Metadata as a Simple DNS – C# Version

 

Every time AWS launches an EC2 instance, the instance is arbitrarily assigned an IP address and host name. I found this article by Tim Dysinger where he uses an interesting technique to allow the hosts in his EC2 environment to discover each other when new instances are launched and assigned a new host names and IP Addresses.

AWS exposes the details of all your launched instance via the AWS EC2 API.  One particular piece of the meta-data that is also exposed per instance is the "KeyName" used by Amazon to launch your instance.  While launching an instance,  set the KeyName to be the same as the desired hostname of the launching instance:

image

Once your instance is launched with this arrangement, you should be able to use the Amazon EC2 API to query its metadata. This now includes, the hostname that you’ve specified via the Keyname meta-data field when launching the instance. All that remains is to query this data and generate a hosts file from this.

The following is a sample C# program that does exactly that using the AWS SDK for .NET

Program Listing

using System;

using System.Configuration;

using System.Collections.Specialized;

using System.IO;

using System.Text;

 

using Amazon;

using Amazon.EC2;

using Amazon.EC2.Model;

using System.Net;

 

class EC2Hosts

{

  public static void Main(string[] args)

  {

      NameValueCollection appConfig = ConfigurationManager.AppSettings;

 

      // Print the number of Amazon EC2 instances.

      AmazonEC2 ec2 = AWSClientFactory.CreateAmazonEC2Client(

        appConfig["AWSAccessKey"],

        appConfig["AWSSecretKey"]

        );

      DescribeInstancesRequest ec2Request = new DescribeInstancesRequest();

 

      try

      {

        DescribeInstancesResponse ec2Response = ec2.DescribeInstances(ec2Request);

        int numInstances = 0;

        numInstances = ec2Response.DescribeInstancesResult.Reservation.Count;

        foreach (var Reservation in ec2Response.DescribeInstancesResult.Reservation)

        {

          foreach (var Instance  in Reservation.RunningInstance)

          {

            if (Instance.InstanceState.Name.ToLower() == "running")

              Console.WriteLine(Instance.PrivateIpAddress + "t" + Instance.KeyName + ".ec2");

          }

        }

      }

      catch (AmazonEC2Exception ex)

      {

        Console.WriteLine("Exception:" + ex.Source + " " + ex.StackTrace);

      }

  }

}

 

App.config:

 

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

      <appSettings>

            <add key="AWSAccessKey" value="<Enter AWS Access Key here>"/>

            <add key="AWSSecretKey" value="<Enter AWS Secret Key here>"/>

      </appSettings>

</configuration>

Posted in Uncategorized | 1 Comment