lambda-runtime-types 0.6.13

Common structures for lambda architecture
Documentation
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//! This crate provides types and traits to simplify
//! the creation of lambda functions in rust. It
//! provides Event and Return types and specific
//! Runners for various lambda types.
//!
//! # Basic Lambda with no shared data
//!
//! Creating a normal lambda is very easy. First create a type which implements [`Runner`] and
//! then use it either in the [`exec`] or [`exec_tokio`] function:
//!
//! ```no_run
//! struct Runner;
//!
//! #[async_trait::async_trait]
//! impl<'a> lambda_runtime_types::Runner<'a, (), (), ()> for Runner {
//!     async fn run(shared: &'a (), event: lambda_runtime_types::LambdaEvent<'a, ()>) -> anyhow::Result<()> {
//!         // Run code on every invocation
//!         Ok(())
//!     }
//!
//!     async fn setup(_region: &'a str) -> anyhow::Result<()> {
//!         // Setup logging to make sure that errors are printed
//!         Ok(())
//!     }
//! }
//!
//! pub fn main() -> anyhow::Result<()> {
//!     lambda_runtime_types::exec_tokio::<_, _, Runner, _>()
//! }
//! ```
//!
//! # Available lambda types
//!
//! There are various modules which predefined Event and Return types and Runner traits
//! specialised for differnet lambda usages. Check out the modules for examples or their
//! usage.
//!
//! * [`rotate`]
//!
//! # Custom Event and Return types
//!
//! If the predefined types are not enough, custom types can be used as long as types for
//! events implement [`serde::Deserialize`] and return types implement [`serde::Serialize`].
//!
//! ```no_run
//! #[derive(serde::Deserialize, Debug)]
//! struct Event {
//!     #[serde(flatten)]
//!     attributes: std::collections::HashMap<String, serde_json::Value>,
//! }
//!
//! #[derive(serde::Serialize, Debug)]
//! struct Return {
//!     data: std::borrow::Cow<'static, str>,
//! }
//!
//! struct Runner;
//!
//! #[async_trait::async_trait]
//! impl<'a> lambda_runtime_types::Runner<'a, (), Event, Return> for Runner {
//!     async fn run(shared: &'a (), event: lambda_runtime_types::LambdaEvent<'a, Event>) -> anyhow::Result<Return> {
//!         println!("{:?}", event);
//!         Ok(Return {
//!             data: event
//!                 .event
//!                 .attributes
//!                 .get("test")
//!                 .and_then(|a| a.as_str())
//!                 .map(ToOwned::to_owned)
//!                 .map(Into::into)
//!                 .unwrap_or_else(|| "none".into()),
//!         })
//!     }
//!
//!     async fn setup(_region: &'a str) -> anyhow::Result<()> {
//!         // Setup logging to make sure that errors are printed
//!         Ok(())
//!     }
//! }
//!
//! pub fn main() -> anyhow::Result<()> {
//!     lambda_runtime_types::exec_tokio::<_, _, Runner, _>()
//! }
//! ```
//!
//! # Shared Data
//!
//! With AWS Lambda, its possible to share data between invocations, as long as both
//! invocations use the same runtime environment. To use this functinality, its possible
//! to define a shared data type which will persist data by using Interior Mutability:
//!
//! ```no_run
//! #[derive(Default)]
//! struct Shared  {
//!     invocations: tokio::sync::Mutex<u64>,
//! }
//!
//! struct Runner;
//!
//! #[async_trait::async_trait]
//! impl<'a> lambda_runtime_types::Runner<'a, Shared, (), ()> for Runner {
//!     async fn run(shared: &'a Shared, event: lambda_runtime_types::LambdaEvent<'a, ()>) -> anyhow::Result<()> {
//!         let mut invocations = shared.invocations.lock().await;
//!         *invocations += 1;
//!         Ok(())
//!     }
//!
//!     async fn setup(_region: &'a str) -> anyhow::Result<Shared> {
//!         // Setup logging to make sure that errors are printed
//!         Ok(Shared::default())
//!     }
//! }
//!
//! pub fn main() -> anyhow::Result<()> {
//!     lambda_runtime_types::exec_tokio::<_, _, Runner, _>()
//! }
//! ```
//!
//! Its important to know, that lambda execution evironments never run multiple invocations
//! simultaneously. Its therefore possible to keep the mutex unlocked for the whole invocation
//! as it will never block other invocations. Instead it is even recommended to do so, to
//! make sure that there are no unnessary things slowing down lambda execution time.
//!
//! # Timeout handling
//!
//! This crate implements a timeout handling logic. Normally, if a lambda runs into a timeout,
//! it will not create an error, which then does not get propagated by `on_error` destinations.
//!
//! To fix that, a timeout handler is setup, which will "fail" 100 miliseconds before the lambda
//! would run into a timeout, creating an error which then is propagated. There is, however, no
//! gurantee that this handler will fail in time. It will only work, when there are multiple
//! tokio threads or when the main lambda code is currently awaiting, giving tokio the chance
//! to switch tasks (or run them in parallel) and fail the execution.
//!
//! # Memory exhaustion
//!
//! Another thing to consider when running lambdas is memory exhaustion. Unfortunatly it is not
//! possible in rust to check the current memory usage. Therefore it is also not possible to
//! fail before running into OOF. When running lambdas, it may be necessary to setup checks to
//! verify that a lambda completed successfully, and did not run into OOF, as these errors also
//! do not get propagated to `on_error` destinations.
//!

#![deny(clippy::all, clippy::nursery)]
#![deny(nonstandard_style, rust_2018_idioms, unused_crate_dependencies)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

#[cfg(feature = "_rotate")]
#[cfg_attr(
    docsrs,
    doc(cfg(any(feature = "rotate_rusoto", feature = "rotate_aws_sdk")))
)]
pub mod rotate;

#[cfg(test)]
use native_tls as _;
#[cfg(test)]
use postgres_native_tls as _;
#[cfg(test)]
use simple_logger as _;
#[cfg(test)]
use tokio_postgres as _;

pub use lambda_runtime::{Config, Context};

/// Types which contains all the Information relevant for
/// the current invocation
#[non_exhaustive]
#[derive(Debug)]
pub struct LambdaEvent<'a, Event> {
    /// The expected Event which is being send
    /// to the lambda by AWS.
    pub event: Event,
    /// Region the lambda is running in
    pub region: &'a str,
    /// Lambda Invocation Context
    pub ctx: Context,
}

/// Defines a type which is executed every time a lambda
/// is invoced.
///
/// Types:
/// * `Shared`: Type which is shared between lambda
///             invocations. Note that lambda will
///             create multiple environments for
///             simulations invokations and environments
///             are only kept alive for a certain time.
///             It is thus not guaranteed that data
///             can be reused, but with this types
///             its possible.
/// * `Event`:  The expected Event which is being send
///             to the lambda by AWS.
/// * `Return`: Type which is the result of the lamba
///             invocation being returned to AWS
#[async_trait::async_trait]
pub trait Runner<'a, Shared, Event, Return>
where
    Shared: Send + Sync + 'a,
    Event: for<'de> serde::Deserialize<'de> + std::fmt::Debug,
    Return: serde::Serialize,
{
    /// Invoked only once before lambda runtime start. Does not get called on each
    /// lambda invocation. Can be used to setup logging and other global services,
    /// but should be short as it delays lambda startup
    async fn setup(region: &'a str) -> anyhow::Result<Shared>;

    /// Invoked for every lambda invocation. Data in `shared` is persisted between
    /// invocations as long as they are running in the same `execution environment`
    ///
    /// More Info: <https://docs.aws.amazon.com/lambda/latest/dg/runtimes-context.html>
    async fn run(shared: &'a Shared, event: LambdaEvent<'a, Event>) -> anyhow::Result<Return>;
}

/// Lambda entrypoint. This function sets up a lambda
/// multi-thread runtimes and executes [`exec`]. If you
/// already have your own runtime, use the [`exec`]
/// function.
///
/// Types:
/// * `Shared`: Type which is shared between lambda
///             invocations. Note that lambda will
///             create multiple environments for
///             simulations invokations and environments
///             are only kept alive for a certain time.
///             It is thus not guaranteed that data
///             can be reused, but with this types
///             its possible.
/// * `Event`:  The expected Event which is being send
///             to the lambda by AWS.
/// * `Run`:    Runner which is execued for each lambda
///             invocation.
/// * `Return`: Type which is the result of the lamba
///             invocation being returned to AWS
pub fn exec_tokio<Shared, Event, Run, Return>() -> anyhow::Result<()>
where
    Shared: Send + Sync,
    Event: for<'de> serde::Deserialize<'de> + std::fmt::Debug + Send,
    Run: for<'a> Runner<'a, Shared, Event, Return>,
    Return: serde::Serialize,
{
    use anyhow::Context;
    use tokio::runtime::Builder;

    Builder::new_multi_thread()
        .enable_all()
        .build()
        .context("Unable to build tokio runtime")?
        .block_on(exec::<Shared, Event, Run, Return>())
}

/// Lambda entrypoint. This function requires a
/// running tokio runtime. Alternativly use [`exec_tokio`]
/// which creates one.
///
/// Types:
/// * `Shared`: Type which is shared between lambda
///             invocations. Note that lambda will
///             create multiple environments for
///             simulations invokations and environments
///             are only kept alive for a certain time.
///             It is thus not guaranteed that data
///             can be reused, but with this types
///             its possible.
/// * `Event`:  The expected Event which is being send
///             to the lambda by AWS.
/// * `Run`:    Runner which is execued for each lambda
///             invocation.
/// * `Return`: Type which is the result of the lamba
///             invocation being returned to AWS
pub async fn exec<Shared, Event, Run, Return>() -> anyhow::Result<()>
where
    Shared: Send + Sync,
    Event: for<'de> serde::Deserialize<'de> + std::fmt::Debug + Send,
    Run: for<'a> Runner<'a, Shared, Event, Return>,
    Return: serde::Serialize,
{
    use anyhow::{anyhow, Context};
    use lambda_runtime::{service_fn, LambdaEvent};
    use std::env;

    log::info!("Starting lambda runtime");
    let region = env::var("AWS_REGION").context("Missing AWS_REGION env variable")?;
    let region_ref = &region;
    let shared = Run::setup(region_ref).await?;
    let shared_ref = &shared;
    lambda_runtime::run(service_fn(move |data: LambdaEvent<Event>| {
        log::info!("Received lambda invocation with event: {:?}", data.payload);
        let deadline: u64 = data.context.deadline;
        run::<_, Event, Run, Return>(shared_ref, data, Some(deadline), region_ref)
    }))
    .await
    .map_err(|e| anyhow!(e))
}

#[allow(clippy::unit_arg)]
async fn run<'a, Shared, Event, Run, Return>(
    shared: &'a Shared,
    event: lambda_runtime::LambdaEvent<Event>,
    deadline_in_ms: Option<u64>,
    region: &'a str,
) -> anyhow::Result<Return>
where
    Shared: Send + Sync,
    Event: for<'de> serde::Deserialize<'de> + std::fmt::Debug + Send,
    Run: Runner<'a, Shared, Event, Return>,
    Return: serde::Serialize,
{
    use anyhow::anyhow;
    use futures::FutureExt;

    let mut runner = Run::run(
        shared,
        LambdaEvent {
            event: event.payload,
            region,
            ctx: event.context,
        },
    )
    .fuse();
    let res = if let Some(deadline_in_ms) = deadline_in_ms {
        let mut timeout = Box::pin(timeout_handler(deadline_in_ms).fuse());
        futures::select! {
            res = runner => res,
            _ = timeout => Err(anyhow!("Lambda failed by running into a timeout")),
        }
    } else {
        runner.await
    };
    log::info!("Completed lambda invocation");
    match res {
        Ok(res) => Ok(res),
        Err(err) => {
            log::error!("{:?}", err);
            Err(err)
        }
    }
}

async fn timeout_handler(deadline_in_ms: u64) {
    use std::time::{Duration, SystemTime, UNIX_EPOCH};
    use tokio::time::Instant;

    let epoch = UNIX_EPOCH;
    let now = SystemTime::now();
    let now_instant = Instant::now();

    let duration_from_now = now.duration_since(epoch).expect("Time went backwards");
    let duration_from_epoch = Duration::from_millis(deadline_in_ms);
    let duration_deadline = duration_from_epoch - duration_from_now - Duration::from_millis(100);

    let deadline = now_instant + duration_deadline;
    log::info!("Setting deadline to: {:?}", deadline);
    tokio::time::sleep_until(deadline).await;
}

/// TestData which can be used to test lambda invocations
/// locally in combination with [`exec_test`].
#[derive(serde::Deserialize, Clone, Debug)]
#[cfg(feature = "test")]
#[cfg_attr(docsrs, doc(cfg(feature = "test")))]
pub struct TestData<Event> {
    region: String,
    invocations: Vec<Event>,
}

/// Lambda entrypoint. This function can be used to
/// test one or multiple lambda invocations locally.
///
/// Types:
/// * `Shared`: Type which is shared between lambda
///             invocations. Note that lambda will
///             create multiple environments for
///             simulations invokations and environments
///             are only kept alive for a certain time.
///             It is thus not guaranteed that data
///             can be reused, but with this types
///             its possible.
/// * `Event`:  The expected Event which is being send
///             to the lambda by AWS.
/// * `Run`:    Runner which is execued for each lambda
///             invocation.
/// * `Return`: Type which is the result of the lamba
///             invocation being returned to AWS
#[cfg(feature = "test")]
#[cfg_attr(docsrs, doc(cfg(feature = "test")))]
pub fn exec_test<Shared, Event, Run, Return>(test_data: &str) -> anyhow::Result<()>
where
    Shared: Send + Sync,
    Event: for<'de> serde::Deserialize<'de> + std::fmt::Debug + Send,
    Run: for<'a> Runner<'a, Shared, Event, Return>,
    Return: serde::Serialize + std::fmt::Debug,
{
    use anyhow::Context;
    use tokio::runtime::Builder;

    Builder::new_multi_thread()
        .enable_all()
        .build()
        .context("Unable to build tokio runtime")?
        .block_on(async {
            log::info!("Starting lambda test runtime");
            let test_data: TestData<Event> =
                serde_json::from_str(test_data).context("Unable to deserialize test_data")?;
            let region_ref = &test_data.region;
            let shared = Run::setup(region_ref).await?;
            let shared_ref = &shared;

            for (i, data) in test_data.invocations.into_iter().enumerate() {
                log::info!("Starting lambda invocation: {}", i);
                let res = run::<_, Event, Run, Return>(
                    shared_ref,
                    lambda_runtime::LambdaEvent {
                        payload: data,
                        context: crate::Context::default(),
                    },
                    None,
                    region_ref,
                )
                .await?;
                log::info!("{:?}", res);
            }
            Ok(())
        })
}