Setting the Amazon Web Services Region for the the Amazon SDK for Rust - Amazon SDK for Rust
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Setting the Amazon Web Services Region for the the Amazon SDK for Rust

You can access Amazon Web Services services that operate in a specific geographic area by using Amazon Web Services Regions. This can be useful both for redundancy and to keep your data and applications running close to where you and your users access them. For more information on how Regions are used, see Amazon Web Services Region in the Amazon SDKs and Tools Reference Guide.

Important

Most resources reside in a specific Amazon Web Services Region and you must supply the correct Region for the resource when using the SDK.

You must set a default Amazon Web Services Region for the SDK for Rust to use for Amazon requests. This default is used for any SDK service method calls that aren't specified with a Region.

For examples on how to set the default region through the shared Amazon config file or environment variables, see Amazon Web Services Region in the Amazon SDKs and Tools Reference Guide.

Amazon Web Services Region provider chain

The following lookup process is used when loading a service client's configuration from the execution environment. The first value that the SDK finds set is used in the configuration of the client. For more information on creating service clients, see Configure a client from the environment.

  1. Any explicit Region set programmatically.

  2. The AWS_REGION environment variable is checked.

    • If you are using the Amazon Lambda service, this environment variable is set automatically by the Amazon Lambda container.

  3. The region property in the shared Amazon config file is checked.

    • The AWS_CONFIG_FILE environment variable can be used to change the location of the shared config file. To learn more about where this file is kept, see Location of the shared config and credentials files in the Amazon SDKs and Tools Reference Guide.

    • The AWS_PROFILE environment variable can be used to select a named profile instead of the default. To learn more about configuring different profiles, see Shared config and credentials files in the Amazon SDKs and Tools Reference Guide.

  4. The SDK attempts to use the Amazon EC2 Instance Metadata Service to determine the Region of the currently running Amazon EC2 instance.

    • The Amazon SDK for Rust only supports IMDSv2.

The RegionProviderChain is automatically used with no additional code when creating a basic configuration to use with a service client:

let config = aws_config::defaults(BehaviorVersion::latest()) .load() .await;

Setting the Amazon Web Services Region in code

Explicitly setting the Region in code

Use Region::new() directly in your configuration when you want to explicitly set the Region.

The Region provider chain is not used - it does not check the environment, shared config file, or Amazon EC2 Instance Metadata Service.

use aws_config::{defaults, BehaviorVersion}; use aws_sdk_s3::config::Region; #[tokio::main] async fn main() { let config = defaults(BehaviorVersion::latest()) .region(Region::new("us-west-2")) .load() .await; println!("Using Region: {}", config.region().unwrap()); }

Be sure you are entering a valid string for an Amazon Web Services Region; the value provided is not validated.

Customizing the RegionProviderChain

Use the Amazon Web Services Region provider chain when you want to inject a Region conditionally, override it, or customize the resolution chain.

use aws_config::{defaults, BehaviorVersion}; use aws_config::meta::region::RegionProviderChain; use aws_sdk_s3::config::Region; use std::env; #[tokio::main] async fn main() { let region_provider = RegionProviderChain::first_try(env::var("CUSTOM_REGION").ok().map(Region::new)) .or_default_provider() .or_else(Region::new("us-east-2")); let config = aws_config::defaults(BehaviorVersion::latest()) .region(region_provider) .load() .await; println!("Using Region: {}", config.region().unwrap()); }

The previous configuration will:

  1. First see if there is a string set in the CUSTOM_REGION environment variable.

  2. If that's not available, fall back to the default Region provider chain.

  3. If that fails, use "us-east-2" as the final fallback.