diff --git a/.doc_gen/metadata/dynamodb_metadata.yaml b/.doc_gen/metadata/dynamodb_metadata.yaml index fc2f262668b..10c0d001e5e 100644 --- a/.doc_gen/metadata/dynamodb_metadata.yaml +++ b/.doc_gen/metadata/dynamodb_metadata.yaml @@ -2309,3 +2309,21 @@ dynamodb_MidLevelInterface: - dynamodb.dotnetv3.MidLevelQueryAndScanExample services: dynamodb: {} +dynamodb_local: + title: Connect to a local &DDB; instance using an &AWS; SDK + title_abbrev: Connect to a local instance + synopsis: override an endpoint URL to connect to a local development deployment of &DDB; and an &AWS; SDK. + guide_topic: + title: DynamoDB Local + url: amazondynamodb/latest/developerguide/DynamoDBLocal.html + category: Scenarios + languages: + Rust: + versions: + - sdk_version: 1 + github: rustv1/examples/dynamodb + excerpts: + - snippet_files: + - rustv1/examples/dynamodb/src/bin/list-tables-local.rs + services: + dynamodb: {} diff --git a/rustv1/examples/dynamodb/README.md b/rustv1/examples/dynamodb/README.md index ac17834fd6c..f1d2f5b1ec8 100644 --- a/rustv1/examples/dynamodb/README.md +++ b/rustv1/examples/dynamodb/README.md @@ -46,6 +46,7 @@ Code excerpts that show you how to call individual service functions. Code examples that show you how to accomplish a specific task by calling multiple functions within the same service. +- [Connect to a local instance](src/bin/list-tables-local.rs) - [Query a table using PartiQL](src/bin/partiql.rs) @@ -62,6 +63,18 @@ functions within the same service. +#### Connect to a local instance + +This example shows you how to override an endpoint URL to connect to a local development deployment of DynamoDB and an AWS SDK. + + + + + + + + + #### Query a table using PartiQL This example shows you how to do the following: diff --git a/rustv1/examples/dynamodb/src/bin/list-tables-local.rs b/rustv1/examples/dynamodb/src/bin/list-tables-local.rs index 349de2761c8..213ef95cdf4 100644 --- a/rustv1/examples/dynamodb/src/bin/list-tables-local.rs +++ b/rustv1/examples/dynamodb/src/bin/list-tables-local.rs @@ -1,36 +1,34 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -#![allow(clippy::result_large_err)] - // snippet-start:[dynamodb.rust.list-tables-local] -use aws_config::BehaviorVersion; -use aws_sdk_dynamodb::{Client, Error}; - -/// Lists your tables in DynamoDB local. +/// Lists your tables from a local DynamoDB instance by setting the SDK Config's +/// endpoint_url and test_credentials. #[tokio::main] -async fn main() -> Result<(), Error> { - let config = aws_config::defaults(BehaviorVersion::latest()) +async fn main() { + tracing_subscriber::fmt::init(); + + // snippet-start:[dynamodb.rust.list-tables-local.client_config] + let config = aws_config::defaults(aws_config::BehaviorVersion::latest()) .test_credentials() + // DynamoDB run locally uses port 8000 by default. + .endpoint_url("http://localhost:8000") .load() .await; - let dynamodb_local_config = aws_sdk_dynamodb::config::Builder::from(&config) - // Override the endpoint in the config to use a local dynamodb server. - .endpoint_url( - // DynamoDB run locally uses port 8000 by default. - "http://localhost:8000", - ) - .build(); + let dynamodb_local_config = aws_sdk_dynamodb::config::Builder::from(&config).build(); - let client = Client::from_conf(dynamodb_local_config); + let client = aws_sdk_dynamodb::Client::from_conf(dynamodb_local_config); + // snippet-end:[dynamodb.rust.list-tables-local.client_config] - let resp = client.list_tables().send().await?; - - println!("Found {} tables", resp.table_names().len()); - for name in resp.table_names() { - println!(" {}", name); + let list_resp = client.list_tables().send().await; + match list_resp { + Ok(resp) => { + println!("Found {} tables", resp.table_names().len()); + for name in resp.table_names() { + println!(" {}", name); + } + } + Err(err) => eprintln!("Failed to list local dynamodb tables: {err:?}"), } - - Ok(()) } // snippet-end:[dynamodb.rust.list-tables-local] diff --git a/rustv1/examples/localstack/Cargo.toml b/rustv1/examples/localstack/Cargo.toml index 7e288f30bc2..65254851e4f 100644 --- a/rustv1/examples/localstack/Cargo.toml +++ b/rustv1/examples/localstack/Cargo.toml @@ -2,14 +2,11 @@ [package] name = "localstack-example" version = "0.1.0" -authors = ["Doug Schwartz "] 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"] } +aws-config = { version = "1", features = ["behavior-version-latest"] } +aws-sdk-s3 = { version = "1" } +tokio = { version = "1", features = ["full"] } +tracing-subscriber = { version = "0.3", features = ["env-filter"] } # snippet-end:[localstack.rust.use-localstack-cargo.toml] diff --git a/rustv1/examples/localstack/aws_config b/rustv1/examples/localstack/aws_config new file mode 100644 index 00000000000..bd2049af88d --- /dev/null +++ b/rustv1/examples/localstack/aws_config @@ -0,0 +1,6 @@ +# snippet-start:[localstack.rust.aws_config] +[profile localstack] +region=us-east-1 +output=json +endpoint_url = http://localhost:4566 +# snippet-end:[localstack.rust.aws_config] \ No newline at end of file diff --git a/rustv1/examples/localstack/src/bin/use-localstack.rs b/rustv1/examples/localstack/src/bin/use-localstack.rs index 8af03119e1a..73616b546fe 100644 --- a/rustv1/examples/localstack/src/bin/use-localstack.rs +++ b/rustv1/examples/localstack/src/bin/use-localstack.rs @@ -11,65 +11,27 @@ use std::error::Error; async fn main() -> Result<(), Box> { 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; + // snippet-start:[localstack.rust.use-localstack.config] + // set the environment variable `AWS_PROFILE=localstack` when running + // the application to source `endpoint_url` and point the SDK at the + // localstack instance + let config = aws_config::defaults(BehaviorVersion::latest()).load().await; - let sqs_client = sqs_client(&shared_config); - let s3_client = s3_client(&shared_config); + let s3_config = aws_sdk_s3::config::Builder::from(&config) + .force_path_style(true) + .build(); - let resp = s3_client.list_buckets().send().await?; + let s3 = aws_sdk_s3::Client::from_conf(s3_config); + // snippet-end:[localstack.rust.use-localstack.config] + + let resp = s3.list_buckets().send().await?; let buckets = resp.buckets(); - let num_buckets = buckets.len(); - println!("Buckets:"); + println!("Found {} buckets:", buckets.len()); 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."); + println!("\t{:?}", bucket.name()); } 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()) -} // snippet-end:[localstack.rust.use-localstack]