Hello tutorial for the AWS SDK for Rust
Prerequisites
In order to use the AWS SDK for Rust, you must have Rust and Cargo installed.
-
Install the Rust toolchain: https://www.rust-lang.org/tools/install
-
Install the
cargo-component
toolby running command: cargo install cargo-component
Recommended tools:
The following optional tools can be installed in your IDE to assist with code completion and troubleshooting.
-
The rust-analyzer extension, see Rust in Visual Studio Code
. -
Amazon Q Developer, see Installing the Amazon Q Developer extension or plugin in your IDE.
Create your first SDK app
This procedure creates your first SDK for Rust application that lists your DynamoDB tables.
-
In a terminal or console window, navigate to a location on your computer where you want to create the app.
-
Run the following command to create a
hello_world
directory and populate it with a skeleton Rust project:$
cargo new hello_world --bin -
Navigate into the
hello_world
directory and use the following command to add the required dependencies to the app:$
cargo add aws-config aws-sdk-dynamodb tokio --features tokio/fullThese dependencies include the SDK crates that provide configuration features and support for DynamoDB, including the
tokio
crate, which is used to implement asynchronous I/O operations. Note
Unless you use a feature like
tokio/full
Tokio will not provide an async runtime. The SDK for Rust requires an async runtime. -
Update
main.rs
in thesrc
directory to contain the following code.use aws_config::meta::region::RegionProviderChain; use aws_config::BehaviorVersion; use aws_sdk_dynamodb::{Client, Error}; /// Lists your DynamoDB tables in the default Region or us-east-1 if a default Region isn't set. #[tokio::main] async fn main() -> Result<(), Error> { let region_provider = RegionProviderChain::default_provider().or_else("us-east-1"); let config = aws_config::defaults(BehaviorVersion::latest()) .region(region_provider) .load() .await; let client = Client::new(&config); let resp = client.list_tables().send().await?; println!("Tables:"); let names = resp.table_names(); for name in names { println!(" {}", name); } println!(); println!("Found {} tables", names.len()); Ok(()) }
Note
This example only displays the first page of results. See Pagination to learn how to handle multiple pages of results.
-
Run the program:
$
cargo runYou should see a list of your table names.