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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
#![deny(clippy::all, clippy::cargo)] #![warn(missing_docs, nonstandard_style, rust_2018_idioms)] //! The official Rust runtime for AWS Lambda. //! //! There are two mechanisms available for defining a Lambda function: //! 1. The `lambda` attribute maco, which generates the boilerplate to //! to launch and run a Lambda function. //! //! The [`#[lambda]`] attribute _must_ be placed on an asynchronous main function. //! However, as asynchronous main functions are not legal valid Rust //! this means that the main function must also be decorated using a //! [`#[tokio::main]`] attribute macro. This is available from //! the [Tokio] crate. //! //! 2. A type that conforms to the [`Handler`] trait. This type can then be passed //! to the the `netlify_lambda::run` function, which launches and runs the Lambda runtime. //! //! An asynchronous function annotated with the `#[lambda]` attribute must //! accept an argument of type `A` which implements [`serde::Deserialize`], a [`lambda::Context`] and //! return a `Result<B, E>`, where `B` implements [`serde::Serializable`]. `E` is //! any type that implements `Into<Box<dyn std::error::Error + Send + Sync + 'static>>`. //! //! ```no_run //! use netlify_lambda::{lambda, Context}; //! use serde_json::Value; //! //! type Error = Box<dyn std::error::Error + Send + Sync + 'static>; //! //! #[lambda] //! #[tokio::main] //! async fn main(event: Value, _: Context) -> Result<Value, Error> { //! Ok(event) //! } //! ``` //! //! [`Handler`]: trait.Handler.html //! [`lambda::Context`]: struct.Context.html //! [`lambda`]: attr.lambda.html //! [`#[tokio::main]`]: https://docs.rs/tokio/0.2.21/tokio/attr.main.html //! [Tokio]: https://docs.rs/tokio/ pub use crate::types::Context; use client::Client; pub use netlify_lambda_attributes::lambda; use serde::{Deserialize, Serialize}; use std::{ convert::{TryFrom, TryInto}, env, fmt, future::Future, }; use tokio::stream::{Stream, StreamExt}; use tracing::trace; mod client; mod requests; #[cfg(test)] mod simulated; /// Types available to a Lambda function. mod types; use requests::{EventCompletionRequest, EventErrorRequest, IntoRequest, NextEventRequest}; use types::Diagnostic; /// Error type that lambdas may result in pub(crate) type Error = Box<dyn std::error::Error + Send + Sync + 'static>; /// Configuration derived from environment variables. #[derive(Debug, Default, Clone, PartialEq)] pub struct Config { /// The host and port of the [runtime API](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html). pub endpoint: String, /// The name of the function. pub function_name: String, /// The amount of memory available to the function in MB. pub memory: i32, /// The version of the function being executed. pub version: String, /// The name of the Amazon CloudWatch Logs stream for the function. pub log_stream: String, /// The name of the Amazon CloudWatch Logs group for the function. pub log_group: String, } impl Config { /// Attempts to read configuration from environment variables. pub fn from_env() -> Result<Self, Error> { let conf = Config { endpoint: env::var("AWS_LAMBDA_RUNTIME_API")?, function_name: env::var("AWS_LAMBDA_FUNCTION_NAME")?, memory: env::var("AWS_LAMBDA_FUNCTION_MEMORY_SIZE")?.parse::<i32>()?, version: env::var("AWS_LAMBDA_FUNCTION_VERSION")?, log_stream: env::var("AWS_LAMBDA_LOG_STREAM_NAME")?, log_group: env::var("AWS_LAMBDA_LOG_GROUP_NAME")?, }; Ok(conf) } } /// A trait describing an asynchronous function `A` to `B`. pub trait Handler<A, B> { /// Errors returned by this handler. type Error; /// Response of this handler. type Fut: Future<Output = Result<B, Self::Error>>; /// Handle the incoming event. fn call(&mut self, event: A, context: Context) -> Self::Fut; } /// Returns a new [`HandlerFn`] with the given closure. /// /// [`HandlerFn`]: struct.HandlerFn.html pub fn handler_fn<F>(f: F) -> HandlerFn<F> { HandlerFn { f } } /// A [`Handler`] implemented by a closure. /// /// [`Handler`]: trait.Handler.html #[derive(Clone, Debug)] pub struct HandlerFn<F> { f: F, } impl<F, A, B, Error, Fut> Handler<A, B> for HandlerFn<F> where F: Fn(A, Context) -> Fut, Fut: Future<Output = Result<B, Error>> + Send, Error: Into<Box<dyn std::error::Error + Send + Sync + 'static>> + fmt::Display, { type Error = Error; type Fut = Fut; fn call(&mut self, req: A, ctx: Context) -> Self::Fut { (self.f)(req, ctx) } } /// Starts the Lambda Rust runtime and begins polling for events on the [Lambda /// Runtime APIs](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html). /// /// # Example /// ```no_run /// use netlify_lambda::{handler_fn, Context}; /// use serde_json::Value; /// /// type Error = Box<dyn std::error::Error + Send + Sync + 'static>; /// /// #[tokio::main] /// async fn main() -> Result<(), Error> { /// let func = handler_fn(func); /// netlify_lambda::run(func).await?; /// Ok(()) /// } /// /// async fn func(event: Value, _: Context) -> Result<Value, Error> { /// Ok(event) /// } /// ``` pub async fn run<A, B, F>(handler: F) -> Result<(), Error> where F: Handler<A, B>, <F as Handler<A, B>>::Error: fmt::Debug, A: for<'de> Deserialize<'de>, B: Serialize, { trace!("Loading config from env"); let mut handler = handler; let config = Config::from_env()?; let uri = config.endpoint.try_into().expect("Unable to convert to URL"); let client = Client::with(uri, hyper::Client::new()); let incoming = incoming(&client); run_inner(&client, incoming, &mut handler).await?; Ok(()) } /// Runs the lambda function almost entirely in-memory. This is meant for testing. pub async fn run_simulated<A, B, F>(handler: F, url: &str) -> Result<(), Error> where F: Handler<A, B>, <F as Handler<A, B>>::Error: fmt::Debug, A: for<'de> Deserialize<'de>, B: Serialize, { let mut handler = handler; let uri = url.try_into().expect("Unable to convert to URL"); let client = Client::with(uri, hyper::Client::new()); let incoming = incoming(&client).take(1); run_inner(&client, incoming, &mut handler).await?; Ok(()) } fn incoming(client: &Client) -> impl Stream<Item = Result<http::Response<hyper::Body>, Error>> + '_ { async_stream::stream! { loop { let req = NextEventRequest.into_req().expect("Unable to construct request"); let res = client.call(req).await; yield res; } } } async fn run_inner<A, B, F>( client: &Client, incoming: impl Stream<Item = Result<http::Response<hyper::Body>, Error>>, handler: &mut F, ) -> Result<(), Error> where F: Handler<A, B>, <F as Handler<A, B>>::Error: fmt::Debug, A: for<'de> Deserialize<'de>, B: Serialize, { tokio::pin!(incoming); while let Some(event) = incoming.next().await { let event = event?; let (parts, body) = event.into_parts(); let mut ctx: Context = Context::try_from(parts.headers)?; ctx.env_config = Config::from_env()?; let body = hyper::body::to_bytes(body).await?; let body = serde_json::from_slice(&body)?; let request_id = &ctx.request_id.clone(); let f = handler.call(body, ctx); let req = match f.await { Ok(res) => EventCompletionRequest { request_id, body: res }.into_req()?, Err(e) => EventErrorRequest { request_id, diagnostic: Diagnostic { error_message: format!("{:?}", e), error_type: type_name_of_val(e).to_owned(), }, } .into_req()?, }; client.call(req).await?; } Ok(()) } fn type_name_of_val<T>(_: T) -> &'static str { std::any::type_name::<T>() }