DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Programming in AWS Using Visual Studio 2017

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.

Dror Helper user avatar by
Dror Helper
·
Apr. 10, 17 · Tutorial
Like (1)
Save
Tweet
Share
10.03K Views

Join the DZone community and get the full member experience.

Join For Free

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:

VS2017_AWS
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();

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” 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.

AWS Amazon Web Services

Published at DZone with permission of Dror Helper, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Select Multiple Checkboxes in Selenium WebDriver Using Java
  • Journey to Event Driven, Part 1: Why Event-First Programming Changes Everything
  • Fixing Bottlenecks in Your Microservices App Flows
  • Keep Your Application Secrets Secret

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: