Over a million developers have joined DZone.

Programming in AWS Using Visual Studio 2017

DZone 's Guide to

Programming in AWS Using Visual Studio 2017

Behold, Visual Studio 2017 has come to the cloud! Well, AWS at least. Get a glimpse at the provider's most recent, improved support for C#, VS, and .NET Core.

· Cloud Zone ·
Free Resource

Amazon Web Services (AWS) was always supportive of Java, Node.js, and Python. But 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 preview 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:

There str 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. I copied the keys and was ready to go.

The project opens with a simple sample that queries 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, 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();

 $"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” methods 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 at 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 DynamoDB and C# to write Lambdas, and it just works — and it still amazes me.

c# ,cloud ,aws ,visual studio 2017 ,.net core ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}