Get started with the AWS SDK for Rust - AWS SDK for Rust

Get started with the AWS SDK for Rust

This chapter describes how to get started with the AWS SDK for Rust (the SDK). It includes information about getting an AWS account, installing the SDK, and creating a "Hello world" code example that lists all of your Amazon DynamoDB (DynamoDB) tables in an AWS Region.

Prerequisites

Before you can use the SDK, you will need an AWS account and a user in IAM Identity Center with permission to use the AWS services and resources that your application will access.

Set up AWS access

Configure your environment to let your project access AWS services. This section covers creating and configuring your AWS account, preparing IAM Identity Center for use, and setting the environment variables used by the AWS SDK for Rust to fetch your access credentials.

Sign up for an AWS account

If you do not have an AWS account, complete the following steps to create one.

To sign up for an AWS account
  1. Open https://portal.aws.amazon.com/billing/signup.

  2. Follow the online instructions.

    Part of the sign-up procedure involves receiving a phone call and entering a verification code on the phone keypad.

    When you sign up for an AWS account, an AWS account root user is created. The root user has access to all AWS services and resources in the account. As a security best practice, assign administrative access to an administrative user, and use only the root user to perform tasks that require root user access.

AWS sends you a confirmation email after the sign-up process is complete. At any time, you can view your current account activity and manage your account by going to https://aws.amazon.com/ and choosing My Account.

Create an administrative user

After you sign up for an AWS account, secure your AWS account root user, enable AWS IAM Identity Center, and create an administrative user so that you don't use the root user for everyday tasks.

Secure your AWS account root user
  1. Sign in to the AWS Management Console as the account owner by choosing Root user and entering your AWS account email address. On the next page, enter your password.

    For help signing in by using root user, see Signing in as the root user in the AWS Sign-In User Guide.

  2. Turn on multi-factor authentication (MFA) for your root user.

    For instructions, see Enable a virtual MFA device for your AWS account root user (console) in the IAM User Guide.

Create an administrative user
  1. Enable IAM Identity Center.

    For instructions, see Enabling AWS IAM Identity Center in the AWS IAM Identity Center User Guide.

  2. In IAM Identity Center, grant administrative access to an administrative user.

    For a tutorial about using the IAM Identity Center directory as your identity source, see Configure user access with the default IAM Identity Center directory in the AWS IAM Identity Center User Guide.

Sign in as the administrative user
  • To sign in with your IAM Identity Center user, use the sign-in URL that was sent to your email address when you created the IAM Identity Center user.

    For help signing in using an IAM Identity Center user, see Signing in to the AWS access portal in the AWS Sign-In User Guide.

Grant programmatic AWS account access

Users need programmatic access if they want to interact with AWS outside of the AWS Management Console. The way to grant programmatic access depends on the type of user that's accessing AWS.

To grant users programmatic access, choose one of the following options.

Which user needs programmatic access? To By

Workforce identity

(Users managed in IAM Identity Center)

Use temporary credentials to sign programmatic requests to the AWS CLI, AWS SDKs, or AWS APIs.

Following the instructions for the interface that you want to use.

IAM Use temporary credentials to sign programmatic requests to the AWS CLI, AWS SDKs, or AWS APIs. Following the instructions in Using temporary credentials with AWS resources in the IAM User Guide.
IAM

(Not recommended)

Use long-term credentials to sign programmatic requests to the AWS CLI, AWS SDKs, or AWS APIs.

Following the instructions for the interface that you want to use.

For more advanced cases regarding configuring the credentials and Region, see The .aws/credentials and .aws/config files, AWS Region, and Using environment variables in the AWS SDKs and Tools Reference Guide.

Create your first SDK app

This procedure creates a Rust app that lists your DynamoDB tables.

  1. In a terminal or console window, navigate to a location on your computer where you want to create the app.

  2. Run the following command to create a hello_world directory and populate it with a skeleton Rust project:

    $ cargo new hello_world --bin
  3. 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/full

    These 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.

  4. Update main.rs in the src 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 Paginating in the SDK for Rust to learn how to handle multiple pages of results.

  5. Run the program:

    $ cargo run

    You should see a list of your table names.