Use a service client in the AWS SDK for Rust
Create a client
This section describes how to create a client, including one in a specific Region.
In most cases, you'll want to create a client that uses the default search path, which looks for credentials and Region in the order described in Specify your credentials and default Region. After it finds a value for an access key, secret key, or Region, it stops searching for that value.
You can also supply a Region with an argument to the client object.
Most of the SDK for Rust code examples use the following construct, which
searches for the Region in the search path described previously. If a
Region isn't found, this sets the Region to us-east-1
.
SERVICENAME
is the name of the service, such as
s3
for Amazon Simple Storage Service (Amazon S3).
use aws_config::meta::region::RegionProviderChain; use aws_sdk_SERVICENAME::Client; let region_provider = RegionProviderChain::default_provider().or_else("us-east-1"); let config = aws_config::from_env_with_version(aws_config::BehaviorVersion::latest()) .region(region_provider) .load() .await; let client = Client::new(&config);
Specify default configuration behavior
Rust developers expect and rely upon the robust and predictable
behavior the language and its major libraries offer. To help developers
using the AWS SDK for Rust get the expected behavior from the SDK, SDK and
client configurations are required to include a
BehaviorVersion
. The BehaviorVersion
specifies
the version of the SDK whose defaults are expected. This lets the SDK
evolve over time, changing best practices to match new standards and
support new features without unexpected adverse impact on your
application's behavior.
Note
If you try to configure the SDK or create a client without
explicitly specifying a BehaviorVersion
, the constructor
will panic.
For example, imagine that a new version of the SDK is released using a
new default retry policy. If your application uses a
BehaviorVersion
matching a previous version of the SDK that
used a different default configuration, that configuration is used instead
of the new default configuration.
Each time a new version of the AWS SDK for Rust is released, the previous
version in BehaviorVersion
is marked with the Rust
deprecated
attribute and the new version is added. This
causes warnings to occur at compile time, but otherwise lets the build
continue as usual. BehaviorVersion::latest()
is also updated
to indicate the new version's default behavior.
In most cases, you should use BehaviorVersion::latest()
in code or the feature flag behavior-version-latest
in the
Cargo.toml
file.
Set the behavior version in Cargo.toml
You can specify the behavior version for the SDK and individual
modules (such as aws-sdk-s3 or
aws-sdk-iam) by including an
appropriate feature flag in the Cargo.toml
file. At
this time, only the latest
version of the SDK is supported
in Cargo.toml
:
[dependencies] aws-config = { version = "1", features = ["behavior-version-latest"] } aws-sdk-s3 = { version = "1", features = ["behavior-version-latest"] }
This snippet from Cargo.toml
indicates that the project depends on
the aws-config and aws-sdk-s3 crates, which both need to be version 1, and should use the latest
defaults.
Set the behavior version in code
Your code can change the behavior version as needed by specifying it when configuring the SDK or a client:
let config = aws_config::from_env_with_version(aws_config::BehaviorVersion::v2023_11_09());
This example creates a configuration that uses the environment to
configure the SDK but sets the BehaviorVersion
to
v2023_11_09()
.