From b35d1c1d5419ffa46402b4f3ef8225db0e281eec Mon Sep 17 00:00:00 2001 From: David Calavera Date: Fri, 30 Dec 2022 08:47:26 -0800 Subject: [PATCH] Update DynamoDB example to use a shared client. Signed-off-by: David Calavera --- examples/http-dynamodb/src/main.rs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/examples/http-dynamodb/src/main.rs b/examples/http-dynamodb/src/main.rs index 2733e65d..4496ddf4 100644 --- a/examples/http-dynamodb/src/main.rs +++ b/examples/http-dynamodb/src/main.rs @@ -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, Error> { +async fn handle_request(db_client: &Client, event: Request) -> Result, 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"); @@ -36,14 +36,9 @@ async fn function_handler(event: Request) -> Result, 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)?; @@ -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.