micro_lambda/
lib.rs

1pub fn lambda(handler: fn(&str) -> std::result::Result<String, String>) {
2    // Initialise one-time resources here
3    // If initialisation error, POST to /runtime/init/error
4    // Get new invocation events and pass to handler
5
6    let aws_lambda_runtime_api = std::env::var("AWS_LAMBDA_RUNTIME_API").unwrap();
7    loop {
8        let invocation = ureq::get(&format!(
9            "http://{}/2018-06-01/runtime/invocation/next",
10            aws_lambda_runtime_api
11        ))
12        .call();
13
14        let request_id = invocation
15            .header("Lambda-Runtime-Aws-Request-Id")
16            .unwrap()
17            .to_string();
18
19        let response = handler(invocation.into_string().unwrap().as_str());
20
21        match response {
22            Ok(res) => {
23                let _resp = ureq::post(&format!(
24                    "http://{}/2018-06-01/runtime/invocation/{}/response",
25                    aws_lambda_runtime_api, request_id
26                ))
27                .send_string(&res);
28            }
29
30            Err(err) => {
31                // Handle error
32                // If invocation error, POST to /runtime/invocation/AwsRequestId/error
33
34                let _resp = ureq::post(&format!(
35                    "http://{}/2018-06-01/runtime/invocation/{}/error",
36                    aws_lambda_runtime_api, request_id
37                ))
38                .send_string(&err.to_string());
39            }
40        }
41    }
42}