Skip to content

Instantly share code, notes, and snippets.

@guyo13
Last active May 25, 2022 21:54
Show Gist options
  • Save guyo13/7393696043716f7cfec66be2b43840e6 to your computer and use it in GitHub Desktop.
Save guyo13/7393696043716f7cfec66be2b43840e6 to your computer and use it in GitHub Desktop.
rtlambda example echo server
use rtlambda::prelude::*;
use serde::Serialize;
#[macro_use]
extern crate rtlambda;
// Create a struct representing the lambda's response.
#[derive(Serialize, Clone)]
struct EchoMessage {
msg: String,
req_id: String,
}
// Define output and error types for berevity.
type OUT = EchoMessage;
type ERR = String;
// Implement an initialization function.
fn initialize() -> Result<
Box<dyn Fn(Option<&str>, RefLambdaContext<LambdaRuntimeEnv, UreqResponse>) -> Result<OUT, ERR>>,
ERR,
> {
// One-time initialization of reusable resources goes here
//
return Ok(Box::new(move |event, context| {
// Get the aws request id
let req_id = context.aws_request_id().unwrap();
// Unwrap the event string
let event = match event {
Some(v) => v,
None => {
return Err(format!(
"AWS should not permit empty events. Something strange must've happened."
))
}
};
if event == "\"\"" {
return Err(format!("Empty input, nothing to echo."));
}
// Echo the event back as a string.
Ok(EchoMessage {
msg: format!("ECHO: {}", event),
req_id: req_id.to_string(),
})
}));
}
fn main() {
// Create a runtime instance and run its loop.
let mut runtime = default_runtime!(OUT, ERR, LAMBDA_VER, initialize);
runtime.run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment