Front-End Web & Mobile
Using Different AWS Regions with the AWS Mobile SDKs
Version 2 of the AWS Mobile SDK
- This article and sample apply to Version 1 of the AWS Mobile SDK. If you are building new apps, we recommend you use Version 2. For details, please visit the AWS Mobile SDK page.
- This content is being maintained for historical reference.
The services supported by the AWS Mobile SDKs are available in multiple regions world-wide. The purpose of this post is to demonstrate how you can get the SDKs to access these different AWS regions. To learn more about AWS’ regions, please refer to the Regions and Endpoints document, which details the regions where each AWS service is accessible and provides the endpoint necessary for accessing the service in a particular region. By default, service classes in either of the AWS Mobile SDKs will point to the US East (Northern Virginia), or us-east-1, region. The code samples below demonstrate how to get a service class to access the region you prefer.
Regions with the AWS SDK for iOS
For the AWS SDK for iOS, using a service in a specific region is accomplished by setting the endpoint
in the service client. The following code snippet demonstrates how to do this:
AmazonSNSClient *snsClient = [[AmazonSNSClient alloc] initWithAccessKey:ACCESS_KEY_ID withSecretKey:SECRET_KEY]; snsClient.endpoint = @"https://sns.us-west-2.amazonaws.com";
To simplify this process, the AWS SDK for iOS contains a helper class, AmazonEndpoints. This class provides a method for each service and an enumeration for each region. So the code for specifying the region with the helper class looks as follows:
AmazonSNSClient *snsClient = [[AmazonSNSClient alloc] initWithAccessKey:ACCESS_KEY_ID withSecretKey:SECRET_KEY]; snsClient.endpoint = [AmazonEndpoints snsEndpoint:US_WEST_2];
Here’s a sample of the code using a different region.
AmazonSNSClient *snsClient = [[AmazonSNSClient alloc] initWithAccessKey:ACCESS_KEY_ID withSecretKey:SECRET_KEY]; snsClient.endpoint = [AmazonEndpoints snsEndpoint:US_EAST_1];
Regions with the AWS SDK for Android
For the AWS SDK for Android, specifying a specific region is similarly done by setting the endpoint in the service class as follows:
AmazonSNSClient snsClient = new AmazonSNSClient( credentials ); snsClient.setEndpoint( "https://sns.us-west-2.amazonaws.com" );
Another way to set the region for the Android SDK is to utilize the Region class as follows:
AmazonSNSClient snsClient = new AmazonSNSClient( credentials ); snsClient.setRegion( Region.getRegion( Regions.US_WEST_2 ) );
Selecting a different region for a service can be trivially done as shown here:
AmazonSNSClient snsClient = new AmazonSNSClient( credentials ); snsClient.setRegion( Region.getRegion( Regions.US_EAST_1 ) );
Regions and Amazon S3
For Amazon S3, creating a bucket requires an additional step as noted in the Buckets and Regions documentation for Amazon S3. Additionally, although Amazon S3 will automatically redirect requests to the appropriate region, using the endpoint will avoid this redirect and improve latency as noted in Amazon S3’s documentation.
Summary
We hope this blog post makes it clear how to use the various AWS regions with the AWS Mobile SDKs. As always, please let us know what you think by leaving a comment below.