1
1
use std:: path:: Path ;
2
- use lambda_http :: { run , service_fn, Body , Error , Request , Response } ;
3
- use byt_runtime :: myworker :: execute_module ;
2
+ use lambda_runtime :: { service_fn, Error , LambdaEvent } ;
3
+ use serde :: { Deserialize , Serialize } ;
4
4
5
+ /// This is also a made-up example. Requests come into the runtime as unicode
6
+ /// strings in json format, which can map to any structure that implements `serde::Deserialize`
7
+ /// The runtime pays no attention to the contents of the request payload.
8
+ #[ derive( Deserialize ) ]
9
+ struct Request {
10
+ command : String ,
11
+ }
5
12
6
- /// This is the main body for the function.
7
- /// Write your code inside it.
8
- /// There are some code examples in the Runtime repository:
9
- /// - https://github.com/awslabs/aws-lambda-rust-runtime/tree/main/examples
10
- async fn function_handler ( _event : Request ) -> Result < Response < Body > , Error > {
11
- // Extract some useful information from the request
12
- let sdk_config = aws_config:: load_from_env ( ) . await ;
13
- let js_path = Path :: new ( env ! ( "CARGO_MANIFEST_DIR" ) )
14
- . join ( "examples" )
15
- . join ( "hello.js" ) ;
16
- let main_module = deno_core:: resolve_path ( & js_path. to_string_lossy ( ) ) . unwrap ( ) ;
17
- execute_module ( main_module, sdk_config, Default :: default ( ) ) . await ?;
18
-
19
- // Return something that implements IntoResponse.
20
- // It will be serialized to the right response event automatically by the runtime
21
- let resp = Response :: builder ( )
22
- . status ( 200 )
23
- . header ( "content-type" , "text/html" )
24
- . body ( "Hello AWS Lambda HTTP request" . into ( ) )
25
- . map_err ( Box :: new) ?;
26
- Ok ( resp)
13
+ /// This is a made-up example of what a response structure may look like.
14
+ /// There is no restriction on what it can be. The runtime requires responses
15
+ /// to be serialized into json. The runtime pays no attention
16
+ /// to the contents of the response payload.
17
+ #[ derive( Serialize ) ]
18
+ struct Response {
19
+ req_id : String ,
20
+ msg : String ,
27
21
}
28
22
23
+ use byt_runtime:: myworker:: execute_module;
24
+
29
25
#[ tokio:: main]
30
26
async fn main ( ) -> Result < ( ) , Error > {
31
27
tracing_subscriber:: fmt ( )
@@ -34,5 +30,29 @@ async fn main() -> Result<(), Error> {
34
30
. without_time ( )
35
31
. init ( ) ;
36
32
37
- run ( service_fn ( function_handler) ) . await
38
- }
33
+ let func = service_fn ( my_handler) ;
34
+ lambda_runtime:: run ( func) . await ?;
35
+ Ok ( ( ) )
36
+ }
37
+
38
+
39
+ pub ( crate ) async fn my_handler ( event : LambdaEvent < Request > ) -> Result < Response , Error > {
40
+ // extract some useful info from the request
41
+ let command = event. payload . command ;
42
+
43
+ let sdk_config = aws_config:: load_from_env ( ) . await ;
44
+ let js_path = Path :: new ( env ! ( "CARGO_MANIFEST_DIR" ) )
45
+ . join ( "examples" )
46
+ . join ( "hello.js" ) ;
47
+ let main_module = deno_core:: resolve_path ( & js_path. to_string_lossy ( ) ) . unwrap ( ) ;
48
+ execute_module ( main_module, sdk_config, Default :: default ( ) ) . await . unwrap ( ) ;
49
+
50
+ // prepare the response
51
+ let resp = Response {
52
+ req_id : event. context . request_id ,
53
+ msg : format ! ( "Command {} executed." , command) ,
54
+ } ;
55
+
56
+ // return `Response` (it will be serialized to JSON automatically by the runtime)
57
+ Ok ( resp)
58
+ }
0 commit comments