Skip to content

Instantly share code, notes, and snippets.

@rosscanning
Created December 6, 2021 20:51
Show Gist options
  • Save rosscanning/d1a24b57b3db08c2b7f2d4ab3fb3d641 to your computer and use it in GitHub Desktop.
Save rosscanning/d1a24b57b3db08c2b7f2d4ab3fb3d641 to your computer and use it in GitHub Desktop.
Use local dynamodb with AWS Rust SDK
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
use aws_config::meta::region::RegionProviderChain;
use aws_sdk_dynamodb::model::{
AttributeDefinition, KeySchemaElement, KeyType, ProvisionedThroughput, ScalarAttributeType,
};
use aws_sdk_dynamodb::Config;
use aws_sdk_dynamodb::{Client, Error, Region, PKG_VERSION, Endpoint};
use http::uri::Uri;
use structopt::StructOpt;
#[derive(Debug, StructOpt)]
struct Opt {
/// The AWS Region.
#[structopt(short, long)]
region: Option<String>,
/// Whether to display additional information.
#[structopt(short, long)]
verbose: bool,
}
#[tokio::main]
async fn main() -> Result<(), Error> {
//tracing_subscriber::fmt::init();
let Opt { region, verbose } = Opt::from_args();
let region_provider = RegionProviderChain::first_try(region.map(Region::new))
.or_default_provider()
.or_else(Region::new("ap-southeast-2"));
let shared_config = aws_config::from_env().region(region_provider).load().await;
if verbose {
println!("DynamoDB client version: {}", PKG_VERSION);
println!(
"Region: {}",
shared_config.region().unwrap()
);
println!();
}
let config: Config = aws_sdk_dynamodb::config::Builder::from(&shared_config)
.endpoint_resolver(
Endpoint::immutable(Uri::from_static("http://localhost:8000"))
)
.build();
let client = Client::from_conf(config);
let tables = client.list_tables().send().await?;
println!("Current DynamoDB tables: {:?}", tables);
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment