Programming using AWS .NET SDK

#1: “Hello World!” in the world of Visual Studio for MacOS [using AWS SDK]

5 min readMar 6, 2020

--

Level: 100–150 (Beginner)

I am using Visual Studio Community 2019 for Mac OS. Don’t ask me why! And at the time of writing this, AWS Cloud Development Kit and AWS Toolkit for Visual Studio are not supported on Visual Studio on Mac.

Additionally, the runtime I have to use is .NET Core runtime. If you are wondering, what is .NET Core and how is it different from .NET Framework, look here. Don’t feel bad, I wondered for a long time too! .NET 5 (something new, read here) is going to be GA around Nov 2020 which will combine the goodness of .NET Core, .NET Framework, and Mono (along with other stuff).

So I am on my own to create a console based “Hello World!”-ish program on .NET using AWS SDK. I have personally struggled to get this primitive of a program working. However, this coding should not have been that hard — I later came to realize that some of the AWS documentation around this was poorly structured and that compounded the effects of my lost coding-muscle dexterity. In short, how you setup (AWS profile and credentials) using Visual Studio on Mac vs Visual Studio on Windows platform is very different. Also, how you setup AWS profile and credentials on .NET Core vs .NET Framework could also be different depending on the solution template you choose.

If you are chancing upon this due to a similar loss of adeptness or just because the internet brought you here, I hope you get some insights.

Few words on AWS SDK for .NET:

  • To review the supported target platforms view this link.
  • If you are using .NET Core, the setup and use of the configuration provider (i.e. where and how you set your AWS specific global parameters like access keys, AWS Region etc.) could be different v/s using .NET Framework depending on what kind of .NET project template you choose.
  • As of this writing, the “Configuring AWS Credentials” documentation found here, is not well structured. It does not provide a clear guidance for Visual Studio for Mac users, mixes up .NET Core and .NET Framework implementation (e.g. use of app.config which is non-existent in .NET Core web applications), and predicates most of the documentation for Windows users.

Initial setup:

Objective: Setup a .NET development environment using a secure way to store AWS Access key id and Secret access key.

Pre-condition: You need to setup a IAM user within AWS with programmatic access. Follow the video here to create the user. You will need 2 pieces of information: Access key id and Secret access key. You will get those during the final stage of the IAM user creation as shown in the video.

Step 1: Create a .NET Core Console application

Step 2: Add the AWSSDK.Core NuGet library: Right click on your solution node within the Visual Studio IDE, and click “Manage NuGet Packages”. On the pop-up window, search for “aws” and all the AWS SDK packages will be displayed. Select AWSSDK.Core and click “Add Package”.

Note that, you will not be able to download directly from the NuGet Gallery using a web browser, as it downloads a .nupkg file unusable on Mac (unless you are writing a .NET CLI command). The MSI package found here is also unusable on Mac.

Step 3: Locate the credential file. This should be at: /Users/<user>/.aws/credentials. For me, as an example, it is at: /Users/somsubhr/.aws/credentials. This is the location for the shared SDK credential file. Which means, other than using the credentials within SDK for a .NET code, this same credential can be used by the AWS CLI. This location is outside of your project directory (and should be kept that way) to avoid accidental exposure of your access key and secret key.

Add a section in the credential file as shown below. mydevelopment is the profile name. It can be anything you choose. However, if your profile name is not default, the chosen name will need to be specified in your code (more on that in Step 4 below)

[mydevelopment]
aws_access_key_id = <your_access_key_id_from_above>
aws_secret_access_key = <your_secret_access_key_from_above>

Tip: If you see another value already existing in this file, don’t be surprised. It simply means that you have performed Step 2 above or have setup AWS CLI at some point on this computer by using aws configure CLI command. You can simply append a new section with the content from above.

Tip: It may so happen, that you are not able to find the /.aws/ folder. It is likely that AWS SDK or AWS CLI is not yet installed on your MacOS. Perform Step 2 above, or download and install the AWS CLI from here and then configure the CLI as per here.

Tip: As of this writing, you cannot use SDK store option to store credentials on MacOS. Accordingly, NetSDKCredentialsFile class is not available on MacOS platform. Only SharedCredentialsFile is available. In the above step, the credentials file is the shared credential file (or sometimes referred to as being like a .ini file). Try running the following code and you will see an error on the console like “Unhandled exception. Amazon.Runtime.AmazonClientException: The encrypted store is not available. This may be due to use of a non-Windows operating system or Windows Nano Server, or the current user account may not have its profile loaded”. This is the reason you have to use a credential file.

//Try this code on a non-Windows platform and you will see the above errorvar options = new CredentialProfileOptions
{
AccessKey = "access_key",
SecretKey = "secret_key"
};
var profile = new CredentialProfile("default", options);
profile.Region = RegionEndpoint.USWest1;
NetSDKCredentialsFile file = new NetSDKCredentialsFile();
file.RegisterProfile(profile);

Step 4: In the main() class, add the below code to fetch the credentials from the shared credential file.

Needless to say, this code is something you will never write in an actual application.

var credProfileStoreChain = new CredentialProfileStoreChain();if (credProfileStoreChain.TryGetAWSCredentials("default", out AWSCredentials awsCredentials))
{
Console.WriteLine("Access Key: " + awsCredentials.GetCredentials().AccessKey);
Console.WriteLine("Secret Key: " + awsCredentials.GetCredentials().SecretKey);
}
Console.WriteLine("Hello World!");

After you run the program, you will see a similar output in the console (I have redacted the keys)

Access Key: A..................Q
Secret Key: 8.......................................p
Hello World!

Another option that you can use to fetch the credentials are by using the TryGetProfile() method. First, get the profile (like “default” or “mydevelopment”) and then get the credentials. This section of the AWS documentation shows how to do it. What you then do with these credentials will depend on the use case you are trying to solve and what AWS service you are going to use.

I will recommend reading this section to understand how profiles and credentials are resolved from files vs store and also some other elegant ways (like using federated SAML assertion) to get AWS credentials.

Note that, AWS SDK for .NET is distributed as multiple service-specific packages on NuGet (prior version was one single package). So you will need to download the package for the service you want to work on further, like AWSSDK.DynamoDBv2. AWSSDK.Core is a dependency for each of these separate packages and the Core package will get installed with other packages.

--

--

Insignificant part of a significant race which is insignificant in the grand scheme of significant things. Working @AWScloud. Views and humors are my own.