Skip to content

Commit

Permalink
Update DynamoDB example to use a shared client. (#582)
Browse files Browse the repository at this point in the history
Signed-off-by: David Calavera <david.calavera@gmail.com>

Signed-off-by: David Calavera <david.calavera@gmail.com>
  • Loading branch information
calavera authored Dec 31, 2022
1 parent 9997b9d commit ab4c2b3
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions examples/http-dynamodb/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ pub struct Item {
/// Write your code inside it.
/// You can see more examples in Runtime's repository:
/// - https://github.com/awslabs/aws-lambda-rust-runtime/tree/main/examples
async fn function_handler(event: Request) -> Result<Response<Body>, Error> {
async fn handle_request(db_client: &Client, event: Request) -> Result<Response<Body>, Error> {
// Extract some useful information from the request
let body = event.body();
let s = std::str::from_utf8(&body).expect("invalid utf-8 sequence");
let s = std::str::from_utf8(body).expect("invalid utf-8 sequence");
//Log into Cloudwatch
info!(payload = %s, "JSON Payload received");

Expand All @@ -36,14 +36,9 @@ async fn function_handler(event: Request) -> Result<Response<Body>, Error> {
return Ok(resp);
}
};

//Get config from environment.
let config = aws_config::load_from_env().await;
//Create the DynamoDB client.
let client = Client::new(&config);

//Insert into the table.
add_item(&client, item.clone(), "lambda_dyno_example").await?;
add_item(db_client, item.clone(), "lambda_dyno_example").await?;

//Deserialize into json to return in the Response
let j = serde_json::to_string(&item)?;
Expand All @@ -65,7 +60,15 @@ async fn main() -> Result<(), Error> {
.without_time()
.init();

run(service_fn(function_handler)).await
//Get config from environment.
let config = aws_config::load_from_env().await;
//Create the DynamoDB client.
let client = Client::new(&config);

run(service_fn(|event: Request| async {
handle_request(&client, event).await
}))
.await
}

// Add an item to a table.
Expand Down

0 comments on commit ab4c2b3

Please sign in to comment.