Forecast Cloudy – Getting Started with AWS using Microsoft.NET

aws_logo

Switching off my usual Azure and Google Cloud hang outs I decided to check out AWS. Moreover as AWS offers a nice ,NET SDK and toolkit for Visual Studio,  I decided to start with .NET as technology for AWS.

First thing you need to do if you decided to develop with .NET for AWS is to create AWS account. To sign up for AWS account:

  • Go to http://aws.amazon.com/  and click Sign Up.
  • Follow the on-screen instructions
  • As part of sign up they will call you and you will enter PIN using phone keypad

AWS sends you a confirmation email after the sign-up process is complete. At any time, you can view your current account activity and manage your account by going to http://aws.amazon.com and clicking My Account/Console. To use the AWS SDK for .NET, you must have a set of valid AWS credentials, which consist of an access key and a secret key. These keys are used to sign programmatic web service requests and enable AWS to verify the request comes from an authorized source. You can obtain a set of account credentials when you create your account.However, AWS docs recommend that you do not use these credentials with AWS SDK for .NET. Instead, they want you to create one or more IAM users, and use those credentials. For applications that run on EC2 instances, you can use IAM roles to provide temporary credentials.

To use the AWS SDK for .NET, you must have the following installed:

  • Microsoft .NET Framework 3.5 or later
  • Visual Studio 2010 or later
  • AWS SDK for .NET
  • AWS Toolkit for Visual Studio, plugin that provides a user interface for managing your AWS resources from Visual Studio, and includes the AWS SDK for .NET

You can install AWS SDK for .NET from NuGet. NuGet always has the most recent versions of the AWSSDK assemblies, and also enables you to install previous versions. NuGet is aware of dependencies between assemblies and installs all required assemblies automatically. Assemblies installed with NuGet are stored with your solution rather than in a central location, such as Program Files. This enables you to install assembly versions specific to a given application without creating compatibility issues for other applications.

To use NuGet from Visual Studio Solution Explorer, right-click on your project and choose Manage NuGet Packages from the context menu.

image

From the Package Manager Console, you can install the desired AWSSDK assemblies using the Install-Package command. For example, to install the AWSSDK.AutoScaling assembly, use the following command:

PM> Install-Package AWSSDK.AutoScaling -Version 3.0.0.1

NuGet will also install any dependencies like AWSCore.

Now lets start a new project in Visual Studio. Since I have installed AWS VS Toolkit things are really simple here:

image

Toolkit provides some basic templates:

  • AWS Console Project –A  console application that makes basic requests to Amazon S3, Amazon SimpleDB, and Amazon EC2. Great for my learning.
  • AWS Empty Project – A console application that does not include any code.
  • AWS Web Project – An ASP.NET application that makes basic requests to Amazon S3, Amazon SimpleDB, and Amazon EC2.

Actually I can also use basic VS built in templates and just add AWSSDK via NuGet as I show above as well. But the toolkit is really nice and polished.

Before I start on anything in VS I really would like to see my AWS items in VS AWS Explorer. To do so do following:

  • In Visual Studio, open AWS Explorer by clicking the View menu and choosing AWS Explorer. You can also open AWS Explorer by typing Ctrl+K, and then pressing the A key.
  • Click the New Account Profile icon to the right of the Profile list.
  • Enter your profile name and access keys into the dialog. As stated previously I actually went to AWS IAM (Identity and Access Management) and setup special user so I don’t use root keys:

image

      Now I can see my items in VS AWS Explorer:

      image

For the sake of simplicity I will pick a AWS Console Application here.

image

Once I pick my console application I am presented with dialog like this one:

Credentials Dialog Box

All it really does is allows me to choose profile and writes keys into App.config appsettings section.The sample SDK Code AWS provides just lists your AWS instances to the command window:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;

using Amazon;
using Amazon.EC2;
using Amazon.EC2.Model;
using Amazon.SimpleDB;
using Amazon.SimpleDB.Model;
using Amazon.S3;
using Amazon.S3.Model;

namespace AWSConsoleFirst
{
    class Program
    {
        public static void Main(string[] args)
        {
            Console.Write(GetServiceOutput());
            Console.Read();
        }

        public static string GetServiceOutput()
        {
            StringBuilder sb = new StringBuilder(1024);
            using (StringWriter sr = new StringWriter(sb))
            {
                sr.WriteLine("===========================================");
                sr.WriteLine("Welcome to the AWS .NET SDK!");
                sr.WriteLine("===========================================");

                // Print the number of Amazon EC2 instances.
                IAmazonEC2 ec2 = new AmazonEC2Client();
                DescribeInstancesRequest ec2Request = new DescribeInstancesRequest();

                try
                {
                    DescribeInstancesResponse ec2Response = ec2.DescribeInstances(ec2Request);
                    int numInstances = 0;
                    numInstances = ec2Response.Reservations.Count;
                    sr.WriteLine(string.Format("You have {0} Amazon EC2 instance(s) running in the {1} region.",
                                               numInstances, ConfigurationManager.AppSettings["AWSRegion"]));
                }
                catch (AmazonEC2Exception ex)
                {
                    if (ex.ErrorCode != null && ex.ErrorCode.Equals("AuthFailure"))
                    {
                        sr.WriteLine("The account you are using is not signed up for Amazon EC2.");
                        sr.WriteLine("You can sign up for Amazon EC2 at http://aws.amazon.com/ec2");
                    }
                    else
                    {
                        sr.WriteLine("Caught Exception: " + ex.Message);
                        sr.WriteLine("Response Status Code: " + ex.StatusCode);
                        sr.WriteLine("Error Code: " + ex.ErrorCode);
                        sr.WriteLine("Error Type: " + ex.ErrorType);
                        sr.WriteLine("Request ID: " + ex.RequestId);
                    }
                }
                sr.WriteLine();

                // Print the number of Amazon SimpleDB domains.
                IAmazonSimpleDB sdb = new AmazonSimpleDBClient();
                ListDomainsRequest sdbRequest = new ListDomainsRequest();

                try
                {
                    ListDomainsResponse sdbResponse = sdb.ListDomains(sdbRequest);

                    int numDomains = 0;
                    numDomains = sdbResponse.DomainNames.Count;
                    sr.WriteLine(string.Format("You have {0} Amazon SimpleDB domain(s) in the {1} region.",
                                               numDomains, ConfigurationManager.AppSettings["AWSRegion"]));
                }
                catch (AmazonSimpleDBException ex)
                {
                    if (ex.ErrorCode != null && ex.ErrorCode.Equals("AuthFailure"))
                    {
                        sr.WriteLine("The account you are using is not signed up for Amazon SimpleDB.");
                        sr.WriteLine("You can sign up for Amazon SimpleDB at http://aws.amazon.com/simpledb");
                    }
                    else
                    {
                        sr.WriteLine("Caught Exception: " + ex.Message);
                        sr.WriteLine("Response Status Code: " + ex.StatusCode);
                        sr.WriteLine("Error Code: " + ex.ErrorCode);
                        sr.WriteLine("Error Type: " + ex.ErrorType);
                        sr.WriteLine("Request ID: " + ex.RequestId);
                    }
                }
                sr.WriteLine();

                // Print the number of Amazon S3 Buckets.
                IAmazonS3 s3Client = new AmazonS3Client();

                try
                {
                    ListBucketsResponse response = s3Client.ListBuckets();
                    int numBuckets = 0;
                    if (response.Buckets != null &&
                        response.Buckets.Count > 0)
                    {
                        numBuckets = response.Buckets.Count;
                    }
                    sr.WriteLine("You have " + numBuckets + " Amazon S3 bucket(s).");
                }
                catch (AmazonS3Exception ex)
                {
                    if (ex.ErrorCode != null && (ex.ErrorCode.Equals("InvalidAccessKeyId") ||
                        ex.ErrorCode.Equals("InvalidSecurity")))
                    {
                        sr.WriteLine("Please check the provided AWS Credentials.");
                        sr.WriteLine("If you haven't signed up for Amazon S3, please visit http://aws.amazon.com/s3");
                    }
                    else
                    {
                        sr.WriteLine("Caught Exception: " + ex.Message);
                        sr.WriteLine("Response Status Code: " + ex.StatusCode);
                        sr.WriteLine("Error Code: " + ex.ErrorCode);
                        sr.WriteLine("Request ID: " + ex.RequestId);
                    }
                }
                sr.WriteLine("Press any key to continue...");
            }
            return sb.ToString();
        }
    }
}

Result is quite humbling, but to me it beats usual Hello World statement:

image

My next job is to build something more useful, but now that I am setup its just a matter of finding a bit of time to spare.

For more information see – http://docs.aws.amazon.com/AWSSdkDocsNET/latest/V3/DeveloperGuide/net-dg-start-new-project.html, http://aws.amazon.com/net/, http://docs.aws.amazon.com/AWSSdkDocsNET/latest/V3/DeveloperGuide/net-dg-programming-techniques.html

Advertisements

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