Skip to content

Commit ab4c2b3

Browse files
authored
Update DynamoDB example to use a shared client. (#582)
Signed-off-by: David Calavera <david.calavera@gmail.com> Signed-off-by: David Calavera <david.calavera@gmail.com>
1 parent 9997b9d commit ab4c2b3

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

examples/http-dynamodb/src/main.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ pub struct Item {
1616
/// Write your code inside it.
1717
/// You can see more examples in Runtime's repository:
1818
/// - https://github.com/awslabs/aws-lambda-rust-runtime/tree/main/examples
19-
async fn function_handler(event: Request) -> Result<Response<Body>, Error> {
19+
async fn handle_request(db_client: &Client, event: Request) -> Result<Response<Body>, Error> {
2020
// Extract some useful information from the request
2121
let body = event.body();
22-
let s = std::str::from_utf8(&body).expect("invalid utf-8 sequence");
22+
let s = std::str::from_utf8(body).expect("invalid utf-8 sequence");
2323
//Log into Cloudwatch
2424
info!(payload = %s, "JSON Payload received");
2525

@@ -36,14 +36,9 @@ async fn function_handler(event: Request) -> Result<Response<Body>, Error> {
3636
return Ok(resp);
3737
}
3838
};
39-
40-
//Get config from environment.
41-
let config = aws_config::load_from_env().await;
42-
//Create the DynamoDB client.
43-
let client = Client::new(&config);
4439

4540
//Insert into the table.
46-
add_item(&client, item.clone(), "lambda_dyno_example").await?;
41+
add_item(db_client, item.clone(), "lambda_dyno_example").await?;
4742

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

68-
run(service_fn(function_handler)).await
63+
//Get config from environment.
64+
let config = aws_config::load_from_env().await;
65+
//Create the DynamoDB client.
66+
let client = Client::new(&config);
67+
68+
run(service_fn(|event: Request| async {
69+
handle_request(&client, event).await
70+
}))
71+
.await
6972
}
7073

7174
// Add an item to a table.

0 commit comments

Comments
 (0)