lambda-simulator 0.1.5

High-fidelity AWS Lambda Runtime API simulator for testing Lambda runtimes and extensions locally
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//! Demo test showcasing Lambda process freeze/thaw with real processes.
//!
//! This test demonstrates the full Lambda lifecycle with actual SIGSTOP/SIGCONT
//! process freezing, showing:
//! - Real process spawning (extension and runtime binaries)
//! - Cold start initialisation
//! - Invocation processing with trace context
//! - Process freezing between invocations (like real Lambda)
//! - Process thawing for subsequent invocations
//! - Warm start behaviour
//! - Graceful shutdown
//! - Telemetry metrics collection
//!
//! ## Platform Support
//!
//! This test requires Unix-like systems with SIGSTOP/SIGCONT signal support.
//! It will not work on Windows.
//!
//! ## Prerequisites
//!
//! Build workspace binaries before running:
//! ```sh
//! cargo build --workspace
//! ```
//!
//! ## Running the Demo
//!
//! ```sh
//! cargo test -p lambda-simulator --test freeze_demo_test -- --nocapture --ignored
//! ```
//!
//! ## Interactive Mode (for VHS recordings)
//!
//! The test can wait for stdin input between phases, allowing VHS to control pacing:
//! ```sh
//! DEMO_INTERACTIVE=1 cargo test -p lambda-simulator --test freeze_demo_test -- --nocapture --ignored
//! ```

use lambda_simulator::process::{ProcessConfig, ProcessRole};
use lambda_simulator::{
    FreezeMode, InvocationBuilder, InvocationStatus, ShutdownReason, Simulator,
};
use mock_collector::{MockServer, Protocol as MockProtocol};
use std::io::{self, BufRead};
use std::time::Duration;

mod common;
use common::{is_process_stopped, truncate_id, wait_for_http_ready};

const CYAN: &str = "\x1b[36m";
const GREEN: &str = "\x1b[32m";
const YELLOW: &str = "\x1b[33m";
const BLUE: &str = "\x1b[34m";
const MAGENTA: &str = "\x1b[35m";
const RESET: &str = "\x1b[0m";
const BOLD: &str = "\x1b[1m";
const DIM: &str = "\x1b[2m";

const EXPECTED_MIN_SPANS: usize = 9;
const EXPECTED_MIN_LOGS: usize = 1;

fn is_interactive() -> bool {
    std::env::var("DEMO_INTERACTIVE").is_ok()
}

fn wait_for_input() {
    if is_interactive() {
        let stdin = io::stdin();
        let _ = stdin.lock().lines().next();
    }
}

fn find_binary(name: &str) -> String {
    let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| ".".to_string());

    let workspace_root = std::path::Path::new(&manifest_dir)
        .parent()
        .and_then(|p| p.parent())
        .map(|p| p.to_path_buf())
        .unwrap_or_else(|| std::path::PathBuf::from("."));

    let candidates = [
        workspace_root.join("target/debug").join(name),
        workspace_root.join("target/release").join(name),
        std::path::PathBuf::from("target/debug").join(name),
        std::path::PathBuf::from("target/release").join(name),
    ];

    for candidate in &candidates {
        if candidate.exists() {
            return candidate.to_string_lossy().to_string();
        }
    }

    panic!(
        "Binary '{}' not found. Run `cargo build --workspace` first.",
        name
    );
}

fn print_header(text: &str) {
    let bar = "".repeat(60);
    println!();
    println!("{BOLD}{CYAN}{bar}{RESET}");
    println!("{BOLD}{CYAN}  {text}{RESET}");
    println!("{BOLD}{CYAN}{bar}{RESET}");
}

fn print_step(icon: &str, text: &str) {
    println!("{BOLD}{GREEN}{icon}{RESET} {text}");
}

fn print_info(text: &str) {
    println!("{DIM}   {text}{RESET}");
}

fn print_event(phase: &str, text: &str) {
    println!("{YELLOW}[{phase}]{RESET} {text}");
}

fn print_metric(name: &str, value: &str) {
    println!("   {BLUE}{RESET} {name}: {MAGENTA}{value}{RESET}");
}

#[tokio::test]
#[ignore = "requires pre-built binaries: cargo build --workspace"]
async fn demo_freeze_thaw_with_real_processes() {
    print_header("Lambda Process Freeze/Thaw Demo");
    println!();
    println!("{DIM}This demo shows real Lambda behaviour with SIGSTOP/SIGCONT{RESET}");
    println!("{DIM}process freezing between invocations.{RESET}");
    println!();

    // =========================================================================
    // Start Mock Collector
    // =========================================================================
    print_step("📡", "Starting OTLP collector...");
    let collector = MockServer::builder()
        .protocol(MockProtocol::HttpBinary)
        .start()
        .await
        .expect("Failed to start mock collector");

    let collector_endpoint = format!("http://{}", collector.addr());
    print_info(&format!("Collector listening at {}", collector_endpoint));

    // =========================================================================
    // Start Simulator with Freeze Mode
    // =========================================================================
    print_step(
        "🚀",
        "Starting Lambda simulator with FreezeMode::Process...",
    );
    let simulator = Simulator::builder()
        .function_name("freeze-demo")
        .memory_size_mb(256)
        .freeze_mode(FreezeMode::Process)
        .extension_ready_timeout(Duration::from_secs(10))
        .build()
        .await
        .expect("Failed to start simulator");

    simulator.enable_telemetry_capture().await;
    let runtime_api = simulator.runtime_api_url();
    let runtime_api_base = runtime_api.replace("http://", "");
    print_info(&format!("Runtime API at {}", runtime_api));

    // =========================================================================
    // Spawn Extension Process
    // =========================================================================
    print_step("🔌", "Spawning OpenTelemetry extension process...");
    let extension_binary = find_binary("opentelemetry-lambda-extension");
    let extension_config = ProcessConfig::new(&extension_binary, ProcessRole::Extension)
        .env("AWS_LAMBDA_RUNTIME_API", &runtime_api_base)
        .env("LAMBDA_OTEL_EXPORTER_ENDPOINT", &collector_endpoint)
        .env("LAMBDA_OTEL_EXPORTER_PROTOCOL", "http")
        .env("LAMBDA_OTEL_EXPORTER_COMPRESSION", "none")
        .env("LAMBDA_OTEL_FLUSH_STRATEGY", "end")
        .env("LAMBDA_OTEL_RECEIVER_HTTP_ENABLED", "true")
        .env("LAMBDA_OTEL_TELEMETRY_API_ENABLED", "true")
        .env("RUST_LOG", "info,opentelemetry_lambda_extension=info")
        .inherit_stdio(true);

    let mut extension = simulator
        .spawn_process_with_config(extension_config)
        .expect("Failed to spawn extension");

    print_info(&format!("Extension PID: {}", extension.pid()));

    simulator
        .wait_for(
            || async { simulator.extension_count().await >= 1 },
            Duration::from_secs(10),
        )
        .await
        .expect("Extension did not register");

    wait_for_http_ready(4318, Duration::from_secs(5))
        .await
        .expect("Extension OTLP receiver not ready");

    print_event("INIT", "Extension registered and ready");

    // =========================================================================
    // Spawn Runtime Process
    // =========================================================================
    print_step("", "Spawning instrumented runtime process...");
    let runtime_binary = find_binary("http_runtime");
    let runtime_config = ProcessConfig::new(&runtime_binary, ProcessRole::Runtime)
        .env("AWS_LAMBDA_RUNTIME_API", &runtime_api_base)
        .env("AWS_LAMBDA_FUNCTION_NAME", "freeze-demo")
        .env("AWS_LAMBDA_FUNCTION_VERSION", "$LATEST")
        .env("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", "256")
        .env("AWS_REGION", "us-east-1")
        .env("OTEL_EXPORTER_OTLP_ENDPOINT", "http://127.0.0.1:4318")
        .env("OTEL_EXPORTER_OTLP_PROTOCOL", "http/protobuf")
        .env("RUST_LOG", "info,opentelemetry=debug")
        .inherit_stdio(true);

    let runtime = simulator
        .spawn_process_with_config(runtime_config)
        .expect("Failed to spawn runtime");

    print_info(&format!("Runtime PID: {}", runtime.pid()));
    print_event("INIT", "Runtime initialised (cold start)");

    tokio::time::sleep(Duration::from_millis(300)).await;
    wait_for_input();

    // =========================================================================
    // First Invocation (Cold Start)
    // =========================================================================
    println!();
    print_step("1️⃣", "First invocation (cold start)...");

    let invocation_1 = InvocationBuilder::new()
        .payload(serde_json::json!({
            "version": "2.0",
            "routeKey": "POST /hello",
            "rawPath": "/hello",
            "headers": {"content-type": "application/json"},
            "body": "{\"message\":\"Hello from first invocation\",\"delay_ms\":10}",
            "requestContext": {"http": {"method": "POST", "path": "/hello"}}
        }))
        .build()
        .expect("Failed to build first invocation");

    let request_id_1 = invocation_1.request_id.clone();
    simulator.enqueue(invocation_1).await;

    let state_1 = simulator
        .wait_for_invocation_complete(&request_id_1, Duration::from_secs(15))
        .await
        .expect("First invocation timed out");

    assert_eq!(state_1.status, InvocationStatus::Success);
    print_event(
        "INVOKE",
        &format!("Request {} completed", truncate_id(&request_id_1, 8)),
    );

    simulator
        .wait_for_extensions_ready(&request_id_1, Duration::from_secs(5))
        .await
        .expect("Extensions should be ready after first invocation");

    wait_for_input();

    // =========================================================================
    // Freeze Phase
    // =========================================================================
    println!();
    print_step("❄️", "Freezing processes (SIGSTOP)...");
    print_info("Runtime and extension processes suspended");
    print_info("This simulates Lambda's behaviour between invocations");

    let runtime_pid = runtime.pid();
    let extension_pid = extension.pid();

    tokio::time::sleep(Duration::from_millis(100)).await;

    assert!(
        is_process_stopped(runtime_pid),
        "Runtime process should be frozen (SIGSTOP)"
    );
    assert!(
        is_process_stopped(extension_pid),
        "Extension process should be frozen (SIGSTOP)"
    );
    print_event("FREEZE", "Verified: Both processes are stopped");

    tokio::time::sleep(Duration::from_millis(400)).await;
    wait_for_input();

    // =========================================================================
    // Second Invocation (Warm Start - Thaw)
    // =========================================================================
    // The fact that this invocation completes successfully proves thaw worked.
    // We don't check process state here because by the time we could check,
    // the process may already be frozen again after completing.
    println!();
    print_step("2️⃣", "Second invocation (warm start, thawing processes)...");
    print_info("Processes receiving SIGCONT to resume");

    let invocation_2 = InvocationBuilder::new()
        .payload(serde_json::json!({
            "version": "2.0",
            "routeKey": "POST /hello",
            "rawPath": "/hello",
            "headers": {"content-type": "application/json"},
            "body": "{\"message\":\"Second invocation after thaw\",\"delay_ms\":5}",
            "requestContext": {"http": {"method": "POST", "path": "/hello"}}
        }))
        .build()
        .expect("Failed to build second invocation");

    let request_id_2 = invocation_2.request_id.clone();
    simulator.enqueue(invocation_2).await;

    let state_2 = simulator
        .wait_for_invocation_complete(&request_id_2, Duration::from_secs(15))
        .await
        .expect("Second invocation timed out");

    assert_eq!(state_2.status, InvocationStatus::Success);
    print_event(
        "INVOKE",
        &format!("Request {} completed (warm)", truncate_id(&request_id_2, 8)),
    );

    simulator
        .wait_for_extensions_ready(&request_id_2, Duration::from_secs(5))
        .await
        .expect("Extensions should be ready after second invocation");

    wait_for_input();

    // =========================================================================
    // Third Invocation (Warm Start)
    // =========================================================================
    println!();
    print_step("3️⃣", "Third invocation (warm start)...");

    let invocation_3 = InvocationBuilder::new()
        .payload(serde_json::json!({
            "version": "2.0",
            "routeKey": "GET /status",
            "rawPath": "/status",
            "headers": {},
            "body": "{\"message\":\"Status check\",\"delay_ms\":3}",
            "requestContext": {"http": {"method": "GET", "path": "/status"}}
        }))
        .build()
        .expect("Failed to build third invocation");

    let request_id_3 = invocation_3.request_id.clone();
    simulator.enqueue(invocation_3).await;

    let state_3 = simulator
        .wait_for_invocation_complete(&request_id_3, Duration::from_secs(15))
        .await
        .expect("Third invocation timed out");

    assert_eq!(state_3.status, InvocationStatus::Success);
    print_event(
        "INVOKE",
        &format!("Request {} completed (warm)", truncate_id(&request_id_3, 8)),
    );

    simulator
        .wait_for_extensions_ready(&request_id_3, Duration::from_secs(5))
        .await
        .expect("Extensions should be ready after third invocation");

    // =========================================================================
    // Capture telemetry before shutdown
    // =========================================================================
    let telemetry_events = simulator.get_telemetry_events().await;

    wait_for_input();

    // =========================================================================
    // Shutdown
    // =========================================================================
    println!();
    print_step("🛑", "Initiating graceful shutdown...");

    // In real Lambda, the platform terminates the runtime before the extension exits.
    // We need to drop the runtime first so its OTel providers flush to the extension,
    // then let the extension export to the collector before it exits.
    print_info("Terminating runtime process (like Lambda platform does)");
    drop(runtime);

    // Give the extension time to receive any final telemetry from the runtime
    tokio::time::sleep(Duration::from_millis(500)).await;

    // Now trigger the graceful shutdown for the extension
    simulator.graceful_shutdown(ShutdownReason::Spindown).await;
    print_event("SHUTDOWN", "Extension received shutdown signal");

    // Wait for extension process to exit
    match tokio::time::timeout(Duration::from_secs(2), async {
        loop {
            if extension.try_wait().ok().flatten().is_some() {
                break;
            }
            tokio::time::sleep(Duration::from_millis(50)).await;
        }
    })
    .await
    {
        Ok(()) => print_event("SHUTDOWN", "Extension exited cleanly"),
        Err(_) => print_event("SHUTDOWN", "Extension exit timed out (will be killed)"),
    }

    // Wait for telemetry to arrive at collector
    collector
        .wait_for_spans(EXPECTED_MIN_SPANS, Duration::from_secs(5))
        .await
        .expect("Telemetry spans should arrive at collector");
    collector
        .wait_for_logs(EXPECTED_MIN_LOGS, Duration::from_secs(2))
        .await
        .expect("Telemetry logs should arrive at collector");

    // =========================================================================
    // Telemetry Summary
    // =========================================================================
    println!();
    print_header("Telemetry Summary");
    print_info(&format!(
        "{} platform events captured",
        telemetry_events.len()
    ));

    collector
        .with_collector(|c| {
            println!();
            print_info(&format!("{} spans exported", c.span_count()));
            print_info(&format!("{} metrics exported", c.metric_count()));
            print_info(&format!("{} logs exported", c.log_count()));

            if c.span_count() > 0 {
                println!();
                println!("{BOLD}   Spans:{RESET}");
                for span in c.spans() {
                    let span_name = &span.span().name;
                    print_metric(span_name, "");
                }
            }

            if c.metric_count() > 0 {
                println!();
                println!("{BOLD}   Metrics:{RESET}");
                for metric in c.metrics() {
                    let name = &metric.metric().name;
                    print_metric(name, "");
                }
            }

            if c.log_count() > 0 {
                println!();
                println!("{BOLD}   Logs:{RESET}");

                c.expect_log_with_body("Hello from first invocation")
                    .assert_exists();
                print_metric("Cold start log received", "");

                c.expect_log_with_body("Second invocation after thaw")
                    .assert_exists();
                print_metric("Warm start (post-thaw) log received", "");

                c.expect_log_with_body("Status check").assert_exists();
                print_metric("Third invocation log received", "");

                c.expect_log_with_body("Request completed")
                    .assert_at_least(3);
                print_metric("Handler completion logs (3+)", "");
            }
        })
        .await;

    // =========================================================================
    // Cleanup
    // =========================================================================
    drop(extension);
    collector
        .shutdown()
        .await
        .expect("Collector shutdown failed");

    println!();
    print_header("Demo Complete!");
    println!();
    println!("{DIM}The simulator accurately reproduces Lambda's freeze/thaw{RESET}");
    println!("{DIM}behaviour using real SIGSTOP/SIGCONT signals.{RESET}");
    println!();
}