LocalStack with the AWS SDK for Rust - AWS SDK for Rust

LocalStack with the AWS SDK for Rust

This section describes how you can use LocalStack with the SDK.

LocalStack is a cloud service emulator that runs in a single container on your computer. You can use the Rust SDK with LocalStack by setting a custom endpoint, as shown in the following code example. The example configures clients for Amazon Simple Queue Service (Amazon SQS) and Amazon Simple Storage Service (Amazon S3) to use the LocalStack endpoint if the LOCALSTACK environment variable is true.

Cargo.toml

Specify the SDK crates in Cargo.toml. Note that this specifies to use the developer preview version of the crates. Check crates.io for the latest version.

[package] name = "localstack-example" version = "0.1.0" authors = ["Doug Schwartz <dougsch@amazon.com>"] edition = "2021" [dependencies] aws-config = { version = "1.0.1", features = ["behavior-version-latest"] } aws-sdk-s3 = { version = "1.4.0" } aws-sdk-sqs = { version = "1.3.0" } tokio = { version = "1.20.1", features = ["full"] } http = "0.2" tracing-subscriber = { version = "0.3.15", features = ["env-filter"] }

src/main.rs

use aws_config::BehaviorVersion; use std::error::Error; #[tokio::main] async fn main() -> Result<(), Box<dyn Error>> { tracing_subscriber::fmt::init(); let mut shared_config = aws_config::defaults(BehaviorVersion::latest()); if use_localstack() { shared_config = shared_config.endpoint_url(LOCALSTACK_ENDPOINT); }; let shared_config = shared_config.load().await; let sqs_client = sqs_client(&shared_config); let s3_client = s3_client(&shared_config); let resp = s3_client.list_buckets().send().await?; let buckets = resp.buckets(); let num_buckets = buckets.len(); println!("Buckets:"); for bucket in buckets { println!(" {}", bucket.name().unwrap_or_default()); } println!(); println!("Found {} buckets.", num_buckets); println!(); let repl = sqs_client.list_queues().send().await?; let queues = repl.queue_urls(); let num_queues = queues.len(); println!("Queue URLs:"); for queue in queues { println!(" {}", queue); } println!(); println!("Found {} queues.", num_queues); println!(); if use_localstack() { println!("Using the local stack."); } Ok(()) } /// If LOCALSTACK environment variable is true, use LocalStack endpoints. /// You can use your own method for determining whether to use LocalStack endpoints. fn use_localstack() -> bool { std::env::var("LOCALSTACK").unwrap_or_default() == "true" } const LOCALSTACK_ENDPOINT: &str = "http://localhost:4566/"; fn sqs_client(conf: &aws_config::SdkConfig) -> aws_sdk_sqs::Client { // Copy config from aws_config::SdkConfig to aws_sdk_sqs::Config let sqs_config_builder = aws_sdk_sqs::config::Builder::from(conf); aws_sdk_sqs::Client::from_conf(sqs_config_builder.build()) } fn s3_client(conf: &aws_config::SdkConfig) -> aws_sdk_s3::Client { // Copy config from aws_config::SdkConfig to aws_sdk_s3::Config let s3_config_builder = aws_sdk_s3::config::Builder::from(conf); aws_sdk_s3::Client::from_conf(s3_config_builder.build()) }