Programming AWS using Visual Studio 2017

Amazon Web Services (AWS) was always supportive of Java, Node.js and Python. But with the lately C# support has been added as well.

For the last few months I’ve been using C# to develop tools and services for the AWS cloud, and so I was happy to discover last month that a prview for AWS Toolkit for VS2017 was just released.

The new toolkit helps develop AWS tools using C# and Visual Studio, create cloud formation templates and even write AWS Lambdas using .NET Core (1.0 but still impressive).

First you’ll need to download and install Visual Studio 2017 (any edition will do) then download the Preview of the AWS Toolkit for Visual Studio 2017 and you’ll be ready to go.

Creating a new project is easy just use File->New Project and you’ll see the new AWS project templates:

VS2017_AWSThere’s several project templates that would get you started using AWS services. For this post I’ll use AWS Console Project.

Once opened you’ll need to supply your access keys for your AWS IAM user, I’ve created a specific use using IAM that has read permissions for the relevant services, copied the keys and I was ready to go.

The project opens with a simple sample that query AWS and prints all of the Region’s EC2 instances (that’s AWS speak for virtual machines). At work I needed to find out the subnet of each virtual machine and so instead of using the AWS CLI I’ve decided to write a quick and dirty method(s) to do just that:

IAmazonEC2 ec2 = new AmazonEC2Client();
var describeVpcsResponse = ec2.DescribeVpcs();
var describeSubnetResponse = ec2.DescribeSubnets();
var describeEc2Response = ec2.DescribeInstances();

sr.WriteLine(
 $"You have {describeVpcsResponse.Vpcs.Count} VPCs running in the {ConfigurationManager.AppSettings["AWSRegion"]} region.");

foreach (var vpc in describeVpcsResponse.Vpcs)
{
    PrintVpcInformation(sr, vpc, describeSubnetResponse, describeEc2Response);
}

Those of you who have used the AWS CLI (Command Line Interface) would feel right at home. Most of the EC2 related functionality starts with creating AmazonEC2Client just like calling cli ec2 from the command line. from there we can use the “Describe” emthods to get information with or without various filters. The nice thing is that when I was looking for specific information and could not find how to do it using the C# AWS SDK instead I’ve looked how to perform that action using the CLI and it mapped exactly to C# objects and methods.

The rest of the code is pretty straightforward:

private static void PrintSubnetInformation(StringWriter sr, Subnet subnet, DescribeInstancesResponse describeEc2Response)
{
    sr.WriteLine($"\t{subnet.Tags.GetName()} : {subnet.SubnetId} : {subnet.AvailabilityZone} : {subnet.CidrBlock}");
    var ec2Instaces = describeEc2Response.Reservations
                       .SelectMany(reservation => reservation.Instances)
                       .Where(instance => instance.SubnetId == subnet.SubnetId);

    foreach (var ec2Instace in ec2Instaces)
    {
        sr.WriteLine($"\t\t{ec2Instace.Tags.GetName()} : {ec2Instace.InstanceId}");
    }
}

One thing I wish I had was the ability to get the resource name, I wish that subnets, VPCs and EC2s had the same interface but they don’t and in any case getting the resource name requires finding if they have a tag with key “Name” – which some don’t. So I wrote another quick and dirty (extension) method:

static class AwsExtensions
{
    public static string GetName(this IEnumerable<Tag> tags)
    {
        var name = "";
        var nameTag = tags.SingleOrDefault(tag => tag.Key == "Name");
        if (nameTag != null)
        {
            name = nameTag.Value;
        }

        return name;
    }
}

And that’s it.

I really appreciate the fact that AWS support C#, but there’s more to that, right now we use C# to write Lambdas and use DynamoDB and it just works – and it’s still amazes me.

One thought on “Programming AWS using Visual Studio 2017

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