AWS for Games Blog

How to integrate the AWS .NET SDK for games using C#

Following on from our first post, “Game developer’s guide to setting up the AWS SDK”, we’re now going to show you how to integrate the SDK using C#, the language used in major game engines like Unity.

To help you get up and running even faster, we’ve created sample code that is available for you to download from GitHub.

Step 1: Add the AWS .NET SDK

The sample code package doesn’t really show the steps you need to take in order to add the AWS SDK to your own project, and so this is what we’ll cover here.

Note that this article assumes you are using Visual Studio 2017 or later. Steps may differ to integrate the AWS .NET SDK in older versions.

The NuGet package manager built in to Visual Studio is the best way to add the AWS SDK for .NET to your project (see below for a special note about Unity). You can add packages to your project by right clicking on your project (not the solution) in the solution explorer and selecting “Manage NuGet Packages…”.

For this demo, we added AWSSDK.CognitoIdentity and AWSSDK.Lambda. We’ve also used the Newtonsoft JSON.NET library. If you run the demo it adds all these libraries for you as we’ve already added them with NuGet (you can look at packages.config in the root folder of the project to see the libraries used) and Visual Studio will grab them the first time you build.

To add packages, click the “Browse” tab on the NuGet package manager and then search for the library. Once the library is located, click the library and hit “Install” on the right panel. For example, here’s how to add the AWS Lambda SDK:

The NuGet package manager winds up looking like this:

Step 2: Write the code

There is no need to explicitly initialize the SDK for C#. You can set configuration parameters in several ways, through data and code. There are no specific settings for this demo, but if you would like to change things like logging level or default region, you can do it in several different ways. Visit the developer guide to learn more.

To get anonymous credentials in C#, you have the benefit of having the code generated for you by Amazon Cognito. As mentioned in the previous post using Amazon Cognito is the preferred way of giving users access to your AWS resources without needing them to log in, or needing to create new IAM roles and shipping the keys.

Going through the .NET SDK, you’ll notice that there are tools available to make it “easy” to manage keys in your app. However, it can be a lot of extra work to create all the IAM roles by hand, grab the keys, input them, etc. It’s much easier to setup for anonymous users, and it works well with C++ if you want to your game to be cross platform.

Once you have the credentials, you now need to create a Lambda client to make calls to AWS. This is the typical pattern for using the AWS SDK. You create a client which makes the calls to AWS, then you create requests, which are basically the parameters used when calling through the client.

The client call will return a response, which gives you information about what happened with the call. Note that client calls presented here are blocking calls, meaning the execution of your program will wait for the client to make its call, and then wait for AWS to respond before starting to execute again. If this is unacceptable in your game (and honestly, for most it is!) you can either make the AWS calls on a thread, or use the “Async” version of the client calls to call back to your code when they have finished executing. We use the blocking calls this code to keep the sample easier to read.

Here’s the code we used to grab the Amazon Cognito credentials and create a Lambda client with them:

	using Amazon;
	using Amazon.CognitoIdentity;

	CognitoAWSCredentials credentials = new CognitoAWSCredentials(
        	    “us-east-1:xx0000x0-0x00-0000-0000-0x0000000xxx", // Identity pool ID
            RegionEndpoint.USEast1 // Region
        IAmazonLambda lambdaClient = new AmazonLambdaClient(credentials, RegionEndpoint.USEast1);

That’s really all there is to it! You can now start using the Lambda client to communicate with AWS and invoke functions.

Unity, Xamarin and UWP note

Due to challenges consuming assemblies via NuGet in Xamarin and UWP projects, and to the fact that Unity doesn’t easily work with NuGet, please see this article for instructions on how to use the AWS .NET SDK with those platforms.


That’s it!

Read the documentation if you need more information on using the AWS SDK for .NET.

For extra help setting up Amazon Cognito, read this post on ‘How to Set Up Player Authentication with Amazon Cognito’.

As always, we want to hear from you. If you have any questions or feedback on our learning resources, head over to the Amazon GameDev forums.

Chris Blackwell

Chris Blackwell

Chris Blackwell is a Senior Engineer with Amazon GameTech. Chris has three decades of experience developing games, from MMO’s to flight simulators and just about everything in between. Chris believes we’re on the cusp of a transformation of the way that we build and play games and is thrilled to be part of the revolution. When he’s not debugging his code, Chris enjoys island life with his family in the Pacific Northwest and has started playing piano way too late in life.