AWS Developer Blog

Working with Regions in the AWS SDK for .NET

by Norm Johanson | on | in .NET | Permalink | Comments |  Share

In earlier versions of the AWS SDK for .NET, using services in regions other than us-east-1 required you to

  • create a config object for the client
  • set the ServiceURL property on the config
  • construct a client using the config object

Here’s an example of what that looks like for Amazon DynamoDB:

var config = new AmazonDynamoDBConfig
{
    ServiceURL = "https://dynamodb.eu-west-1.amazonaws.com/"
};
var dynamoDBClient = new AmazonDynamoDBClient(accessKey, secretKey, config);

In version 1.5.0.0 of the SDK, this was simplified so you can easily set the region in the constructor of the clients using a region constant and remove the burden of knowing the URL to the region. For example, the preceding code can now be replaced with this:

var dynamoDBClient = new AmazonDynamoDBClient(accessKey, secretKey, RegionEndpoint.USWest2);

The previous way of using config objects still works with the SDK. The region constant also works with the config object. For example, if you still need to use the config object to set up a proxy, you can take advantage of the new regions support like this:

var config = new AmazonDynamoDBConfig()
{
    RegionEndpoint = RegionEndpoint.USWest2,
    ProxyHost = "webproxy",
    ProxyPort = 80
};
var dynamoDBClient = new AmazonDynamoDBClient(accessKey, secretKey, config);

In the recently released version 2.0 of the SDK, the region can be set in the app.config file along with the access and secret key. For example, here is an app.config file that instructs the application to use region us-west-2:

<configuration>
  <appSettings>
    <add key="AWSAccessKey" value="YOUR_ACCESS_KEY"/>
    <add key="AWSSecretKey" value="YOUR_SECRET_KEY"/>
    <add key="AWSRegion" value="us-west-2"/>
  </appSettings>
</configuration>

And by running this code, which uses the empty constructor of the Amazon EC2 client, we can see it print out all the Availability Zones in us-west-2.

var ec2Client = new AmazonEC2Client();

var response = ec2Client.DescribeAvailabilityZones();

foreach (var zone in response.AvailabilityZones)
{
    Console.WriteLine(zone.ZoneName);
}

For a list of region constants, you can check the API documentation.