Skip to content

Commit

Permalink
Rust: Update localstack and dynamodb-local examples for Guide updates. (
Browse files Browse the repository at this point in the history
awsdocs#6760)

* Rust: Update localstack and dynamodb-local examples for Guide updates.
  • Loading branch information
DavidSouther authored Sep 6, 2024
1 parent 3291f20 commit b7888f9
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 82 deletions.
18 changes: 18 additions & 0 deletions .doc_gen/metadata/dynamodb_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}
13 changes: 13 additions & 0 deletions rustv1/examples/dynamodb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand All @@ -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.


<!--custom.scenario_prereqs.dynamodb_local.start-->
<!--custom.scenario_prereqs.dynamodb_local.end-->


<!--custom.scenarios.dynamodb_local.start-->
<!--custom.scenarios.dynamodb_local.end-->

#### Query a table using PartiQL

This example shows you how to do the following:
Expand Down
44 changes: 21 additions & 23 deletions rustv1/examples/dynamodb/src/bin/list-tables-local.rs
Original file line number Diff line number Diff line change
@@ -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]
11 changes: 4 additions & 7 deletions rustv1/examples/localstack/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
[package]
name = "localstack-example"
version = "0.1.0"
authors = ["Doug Schwartz <[email protected]>"]
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]
6 changes: 6 additions & 0 deletions rustv1/examples/localstack/aws_config
Original file line number Diff line number Diff line change
@@ -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]
66 changes: 14 additions & 52 deletions rustv1/examples/localstack/src/bin/use-localstack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,65 +11,27 @@ use std::error::Error;
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;
// 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]

0 comments on commit b7888f9

Please sign in to comment.