-
Notifications
You must be signed in to change notification settings - Fork 347
/
main.rs
58 lines (50 loc) · 1.83 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// This example demonstrates use of shared resources such as DB connections
// or local caches that can be initialized at the start of the runtime and
// reused by subsequent lambda handler calls.
// Run it with the following input:
// { "command": "do something" }
use lambda_runtime::{service_fn, tracing, Error, LambdaEvent};
use serde::{Deserialize, Serialize};
/// This is also a made-up example. Requests come into the runtime as unicode
/// strings in json format, which can map to any structure that implements `serde::Deserialize`
/// The runtime pays no attention to the contents of the request payload.
#[derive(Deserialize)]
struct Request {
command: String,
}
/// This is a made-up example of what a response structure may look like.
/// There is no restriction on what it can be. The runtime requires responses
/// to be serialized into json. The runtime pays no attention
/// to the contents of the response payload.
#[derive(Serialize)]
struct Response {
req_id: String,
msg: String,
}
struct SharedClient {
name: &'static str,
}
impl SharedClient {
fn new(name: &'static str) -> Self {
Self { name }
}
fn response(&self, req_id: String, command: String) -> Response {
Response {
req_id,
msg: format!("Command {} executed by {}.", command, self.name),
}
}
}
#[tokio::main]
async fn main() -> Result<(), Error> {
// required to enable CloudWatch error logging by the runtime
tracing::init_default_subscriber();
let client = SharedClient::new("Shared Client 1 (perhaps a database)");
let client_ref = &client;
lambda_runtime::run(service_fn(move |event: LambdaEvent<Request>| async move {
let command = event.payload.command;
Ok::<Response, Error>(client_ref.response(event.context.request_id, command))
}))
.await?;
Ok(())
}