Skip to content

Commit b7888f9

Browse files
authored
Rust: Update localstack and dynamodb-local examples for Guide updates. (#6760)
* Rust: Update localstack and dynamodb-local examples for Guide updates.
1 parent 3291f20 commit b7888f9

File tree

6 files changed

+76
-82
lines changed

6 files changed

+76
-82
lines changed

.doc_gen/metadata/dynamodb_metadata.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2309,3 +2309,21 @@ dynamodb_MidLevelInterface:
23092309
- dynamodb.dotnetv3.MidLevelQueryAndScanExample
23102310
services:
23112311
dynamodb: {}
2312+
dynamodb_local:
2313+
title: Connect to a local &DDB; instance using an &AWS; SDK
2314+
title_abbrev: Connect to a local instance
2315+
synopsis: override an endpoint URL to connect to a local development deployment of &DDB; and an &AWS; SDK.
2316+
guide_topic:
2317+
title: DynamoDB Local
2318+
url: amazondynamodb/latest/developerguide/DynamoDBLocal.html
2319+
category: Scenarios
2320+
languages:
2321+
Rust:
2322+
versions:
2323+
- sdk_version: 1
2324+
github: rustv1/examples/dynamodb
2325+
excerpts:
2326+
- snippet_files:
2327+
- rustv1/examples/dynamodb/src/bin/list-tables-local.rs
2328+
services:
2329+
dynamodb: {}

rustv1/examples/dynamodb/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Code excerpts that show you how to call individual service functions.
4646
Code examples that show you how to accomplish a specific task by calling multiple
4747
functions within the same service.
4848

49+
- [Connect to a local instance](src/bin/list-tables-local.rs)
4950
- [Query a table using PartiQL](src/bin/partiql.rs)
5051

5152

@@ -62,6 +63,18 @@ functions within the same service.
6263

6364

6465

66+
#### Connect to a local instance
67+
68+
This example shows you how to override an endpoint URL to connect to a local development deployment of DynamoDB and an AWS SDK.
69+
70+
71+
<!--custom.scenario_prereqs.dynamodb_local.start-->
72+
<!--custom.scenario_prereqs.dynamodb_local.end-->
73+
74+
75+
<!--custom.scenarios.dynamodb_local.start-->
76+
<!--custom.scenarios.dynamodb_local.end-->
77+
6578
#### Query a table using PartiQL
6679

6780
This example shows you how to do the following:
Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,34 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
#![allow(clippy::result_large_err)]
5-
64
// snippet-start:[dynamodb.rust.list-tables-local]
7-
use aws_config::BehaviorVersion;
8-
use aws_sdk_dynamodb::{Client, Error};
9-
10-
/// Lists your tables in DynamoDB local.
5+
/// Lists your tables from a local DynamoDB instance by setting the SDK Config's
6+
/// endpoint_url and test_credentials.
117
#[tokio::main]
12-
async fn main() -> Result<(), Error> {
13-
let config = aws_config::defaults(BehaviorVersion::latest())
8+
async fn main() {
9+
tracing_subscriber::fmt::init();
10+
11+
// snippet-start:[dynamodb.rust.list-tables-local.client_config]
12+
let config = aws_config::defaults(aws_config::BehaviorVersion::latest())
1413
.test_credentials()
14+
// DynamoDB run locally uses port 8000 by default.
15+
.endpoint_url("http://localhost:8000")
1516
.load()
1617
.await;
17-
let dynamodb_local_config = aws_sdk_dynamodb::config::Builder::from(&config)
18-
// Override the endpoint in the config to use a local dynamodb server.
19-
.endpoint_url(
20-
// DynamoDB run locally uses port 8000 by default.
21-
"http://localhost:8000",
22-
)
23-
.build();
18+
let dynamodb_local_config = aws_sdk_dynamodb::config::Builder::from(&config).build();
2419

25-
let client = Client::from_conf(dynamodb_local_config);
20+
let client = aws_sdk_dynamodb::Client::from_conf(dynamodb_local_config);
21+
// snippet-end:[dynamodb.rust.list-tables-local.client_config]
2622

27-
let resp = client.list_tables().send().await?;
28-
29-
println!("Found {} tables", resp.table_names().len());
30-
for name in resp.table_names() {
31-
println!(" {}", name);
23+
let list_resp = client.list_tables().send().await;
24+
match list_resp {
25+
Ok(resp) => {
26+
println!("Found {} tables", resp.table_names().len());
27+
for name in resp.table_names() {
28+
println!(" {}", name);
29+
}
30+
}
31+
Err(err) => eprintln!("Failed to list local dynamodb tables: {err:?}"),
3232
}
33-
34-
Ok(())
3533
}
3634
// snippet-end:[dynamodb.rust.list-tables-local]

rustv1/examples/localstack/Cargo.toml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22
[package]
33
name = "localstack-example"
44
version = "0.1.0"
5-
authors = ["Doug Schwartz <[email protected]>"]
65
edition = "2021"
76

87
[dependencies]
9-
aws-config = { version = "1.0.1", features = ["behavior-version-latest"] }
10-
aws-sdk-s3 = { version = "1.4.0" }
11-
aws-sdk-sqs = { version = "1.3.0" }
12-
tokio = { version = "1.20.1", features = ["full"] }
13-
http = "0.2"
14-
tracing-subscriber = { version = "0.3.15", features = ["env-filter"] }
8+
aws-config = { version = "1", features = ["behavior-version-latest"] }
9+
aws-sdk-s3 = { version = "1" }
10+
tokio = { version = "1", features = ["full"] }
11+
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
1512
# snippet-end:[localstack.rust.use-localstack-cargo.toml]

rustv1/examples/localstack/aws_config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# snippet-start:[localstack.rust.aws_config]
2+
[profile localstack]
3+
region=us-east-1
4+
output=json
5+
endpoint_url = http://localhost:4566
6+
# snippet-end:[localstack.rust.aws_config]

rustv1/examples/localstack/src/bin/use-localstack.rs

Lines changed: 14 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,65 +11,27 @@ use std::error::Error;
1111
async fn main() -> Result<(), Box<dyn Error>> {
1212
tracing_subscriber::fmt::init();
1313

14-
let mut shared_config = aws_config::defaults(BehaviorVersion::latest());
15-
if use_localstack() {
16-
shared_config = shared_config.endpoint_url(LOCALSTACK_ENDPOINT);
17-
};
18-
let shared_config = shared_config.load().await;
14+
// snippet-start:[localstack.rust.use-localstack.config]
15+
// set the environment variable `AWS_PROFILE=localstack` when running
16+
// the application to source `endpoint_url` and point the SDK at the
17+
// localstack instance
18+
let config = aws_config::defaults(BehaviorVersion::latest()).load().await;
1919

20-
let sqs_client = sqs_client(&shared_config);
21-
let s3_client = s3_client(&shared_config);
20+
let s3_config = aws_sdk_s3::config::Builder::from(&config)
21+
.force_path_style(true)
22+
.build();
2223

23-
let resp = s3_client.list_buckets().send().await?;
24+
let s3 = aws_sdk_s3::Client::from_conf(s3_config);
25+
// snippet-end:[localstack.rust.use-localstack.config]
26+
27+
let resp = s3.list_buckets().send().await?;
2428
let buckets = resp.buckets();
25-
let num_buckets = buckets.len();
2629

27-
println!("Buckets:");
30+
println!("Found {} buckets:", buckets.len());
2831
for bucket in buckets {
29-
println!(" {}", bucket.name().unwrap_or_default());
30-
}
31-
32-
println!();
33-
println!("Found {} buckets.", num_buckets);
34-
println!();
35-
36-
let repl = sqs_client.list_queues().send().await?;
37-
let queues = repl.queue_urls();
38-
let num_queues = queues.len();
39-
40-
println!("Queue URLs:");
41-
for queue in queues {
42-
println!(" {}", queue);
43-
}
44-
45-
println!();
46-
println!("Found {} queues.", num_queues);
47-
println!();
48-
49-
if use_localstack() {
50-
println!("Using the local stack.");
32+
println!("\t{:?}", bucket.name());
5133
}
5234

5335
Ok(())
5436
}
55-
56-
/// If LOCALSTACK environment variable is true, use LocalStack endpoints.
57-
/// You can use your own method for determining whether to use LocalStack endpoints.
58-
fn use_localstack() -> bool {
59-
std::env::var("LOCALSTACK").unwrap_or_default() == "true"
60-
}
61-
62-
const LOCALSTACK_ENDPOINT: &str = "http://localhost:4566/";
63-
64-
fn sqs_client(conf: &aws_config::SdkConfig) -> aws_sdk_sqs::Client {
65-
// Copy config from aws_config::SdkConfig to aws_sdk_sqs::Config
66-
let sqs_config_builder = aws_sdk_sqs::config::Builder::from(conf);
67-
aws_sdk_sqs::Client::from_conf(sqs_config_builder.build())
68-
}
69-
70-
fn s3_client(conf: &aws_config::SdkConfig) -> aws_sdk_s3::Client {
71-
// Copy config from aws_config::SdkConfig to aws_sdk_s3::Config
72-
let s3_config_builder = aws_sdk_s3::config::Builder::from(conf);
73-
aws_sdk_s3::Client::from_conf(s3_config_builder.build())
74-
}
7537
// snippet-end:[localstack.rust.use-localstack]

0 commit comments

Comments
 (0)