@@ -16,10 +16,10 @@ pub struct Item {
16
16
/// Write your code inside it.
17
17
/// You can see more examples in Runtime's repository:
18
18
/// - 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 > {
20
20
// Extract some useful information from the request
21
21
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" ) ;
23
23
//Log into Cloudwatch
24
24
info ! ( payload = %s, "JSON Payload received" ) ;
25
25
@@ -36,14 +36,9 @@ async fn function_handler(event: Request) -> Result<Response<Body>, Error> {
36
36
return Ok ( resp) ;
37
37
}
38
38
} ;
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) ;
44
39
45
40
//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 ?;
47
42
48
43
//Deserialize into json to return in the Response
49
44
let j = serde_json:: to_string ( & item) ?;
@@ -65,7 +60,15 @@ async fn main() -> Result<(), Error> {
65
60
. without_time ( )
66
61
. init ( ) ;
67
62
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
69
72
}
70
73
71
74
// Add an item to a table.
0 commit comments