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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
//! Lambda Runtime API HTTP endpoints implementation.
//!
//! Implements the Lambda Runtime API as documented at:
//! <https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html>

use crate::extension_readiness::ExtensionReadinessTracker;
use crate::freeze::FreezeState;
use crate::invocation::{InvocationError, InvocationResponse};
use crate::simulator::SimulatorConfig;
use crate::state::{RecordResult, RuntimeState};
use crate::telemetry::{
    InitReportMetrics, InitializationType, Phase, PlatformInitReport, PlatformInitRuntimeDone,
    PlatformReport, PlatformRuntimeDone, PlatformStart, ReportMetrics, RuntimeDoneMetrics,
    RuntimeStatus, TelemetryEvent, TelemetryEventType, TraceContext,
};
use crate::telemetry_state::TelemetryState;
use axum::{
    Json, Router,
    extract::{DefaultBodyLimit, Path, State},
    http::{HeaderMap, HeaderValue, StatusCode},
    response::{IntoResponse, Response},
    routing::{get, post},
};
use chrono::Utc;
use serde_json::{Value, json};
use std::sync::Arc;

/// Maximum Lambda response payload size (6 MB).
///
/// AWS Lambda enforces this limit for synchronous invocations. Responses
/// exceeding this limit will be rejected with a 413 Payload Too Large error.
///
/// See: <https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-limits.html>
const MAX_RESPONSE_PAYLOAD_BYTES: usize = 6 * 1024 * 1024;

/// Shared state for Runtime API endpoints.
#[derive(Clone)]
pub(crate) struct RuntimeApiState {
    pub runtime: Arc<RuntimeState>,
    pub telemetry: Arc<TelemetryState>,
    pub freeze: Arc<FreezeState>,
    pub readiness: Arc<ExtensionReadinessTracker>,
    pub config: Arc<SimulatorConfig>,
}

/// Creates the Runtime API router.
///
/// # Arguments
///
/// * `state` - Shared runtime API state
///
/// # Returns
///
/// An axum router configured with all Runtime API endpoints.
pub(crate) fn create_runtime_api_router(state: RuntimeApiState) -> Router {
    Router::new()
        .route("/2018-06-01/runtime/invocation/next", get(next_invocation))
        .route(
            "/2018-06-01/runtime/invocation/{request_id}/response",
            post(invocation_response),
        )
        .route(
            "/2018-06-01/runtime/invocation/{request_id}/error",
            post(invocation_error),
        )
        .route("/2018-06-01/runtime/init/error", post(init_error))
        .layer(DefaultBodyLimit::max(MAX_RESPONSE_PAYLOAD_BYTES + 1024))
        .with_state(state)
}

/// Helper function to safely insert a header value.
#[allow(clippy::result_large_err)]
fn safe_header_insert(
    headers: &mut HeaderMap,
    name: &'static str,
    value: impl AsRef<str>,
) -> Result<(), Response> {
    match HeaderValue::from_str(value.as_ref()) {
        Ok(header_value) => {
            headers.insert(name, header_value);
            Ok(())
        }
        Err(_) => Err((
            StatusCode::INTERNAL_SERVER_ERROR,
            format!("Failed to create header {}", name),
        )
            .into_response()),
    }
}

/// GET /2018-06-01/runtime/invocation/next
///
/// Retrieves the next invocation. This is a long-poll endpoint that blocks
/// until an invocation is available.
///
/// On the first call, this endpoint:
/// - Marks the runtime as initialized, ending the extension registration phase
/// - Emits `platform.initRuntimeDone` and `platform.initReport` telemetry events
///
/// Process freezing happens after all extensions signal readiness (via polling
/// their /next endpoint) following the runtime's response submission.
async fn next_invocation(State(state): State<RuntimeApiState>) -> Response {
    // Mark initialized on first call to /next - this ends the extension registration phase
    let was_first_call = !state.runtime.is_initialized().await;
    state.runtime.mark_initialized().await;

    if was_first_call {
        tracing::info!(target: "lambda_lifecycle", "🚀 Runtime ready (first /next call)");
        tracing::info!(target: "lambda_lifecycle", "⏳ Runtime polling /next (waiting for invocation)");
    }

    // Emit init telemetry on first call to /next
    if !state.runtime.mark_init_telemetry_emitted() {
        let now = Utc::now();
        let init_started_at = state.runtime.init_started_at();
        let init_duration_ms = (now - init_started_at).num_milliseconds() as f64;

        // Emit platform.initRuntimeDone
        let init_runtime_done = PlatformInitRuntimeDone {
            initialization_type: InitializationType::OnDemand,
            phase: Phase::Init,
            status: RuntimeStatus::Success,
            spans: None,
            tracing: None,
        };

        let init_runtime_done_event = TelemetryEvent {
            time: now,
            event_type: "platform.initRuntimeDone".to_string(),
            record: serde_json::json!(init_runtime_done),
        };

        state
            .telemetry
            .broadcast_event(init_runtime_done_event, TelemetryEventType::Platform)
            .await;

        // Emit platform.initReport
        let init_report = PlatformInitReport {
            initialization_type: InitializationType::OnDemand,
            phase: Phase::Init,
            status: RuntimeStatus::Success,
            metrics: InitReportMetrics {
                duration_ms: init_duration_ms,
            },
            spans: None,
            tracing: None,
        };

        let init_report_event = TelemetryEvent {
            time: now,
            event_type: "platform.initReport".to_string(),
            record: serde_json::json!(init_report),
        };

        state
            .telemetry
            .broadcast_event(init_report_event, TelemetryEventType::Platform)
            .await;

        tracing::info!(target: "lambda_lifecycle", "📋 platform.initRuntimeDone (duration: {:.1}ms)", init_duration_ms);
        tracing::info!(target: "lambda_lifecycle", "📋 platform.initReport");
    }

    let invocation = state.runtime.next_invocation().await;

    tracing::info!(target: "lambda_lifecycle", "📥 Runtime received invocation (request_id: {})", &invocation.aws_request_id[..8]);

    // Emit platform.start when the runtime receives an invocation
    let trace_context = TraceContext {
        trace_type: "X-Amzn-Trace-Id".to_string(),
        value: invocation.trace_id.clone(),
        span_id: None,
    };

    let platform_start = PlatformStart {
        request_id: invocation.aws_request_id.clone(),
        version: Some(state.config.function_version.clone()),
        tracing: Some(trace_context),
    };

    let platform_start_event = TelemetryEvent {
        time: Utc::now(),
        event_type: "platform.start".to_string(),
        record: serde_json::json!(platform_start),
    };

    state
        .telemetry
        .broadcast_event(platform_start_event, TelemetryEventType::Platform)
        .await;

    let mut headers = HeaderMap::new();

    if let Err(e) = safe_header_insert(
        &mut headers,
        "Lambda-Runtime-Aws-Request-Id",
        &invocation.aws_request_id,
    ) {
        return e;
    }

    if let Err(e) = safe_header_insert(
        &mut headers,
        "Lambda-Runtime-Deadline-Ms",
        invocation.deadline_ms().to_string(),
    ) {
        return e;
    }

    if let Err(e) = safe_header_insert(
        &mut headers,
        "Lambda-Runtime-Invoked-Function-Arn",
        &invocation.invoked_function_arn,
    ) {
        return e;
    }

    if let Err(e) = safe_header_insert(
        &mut headers,
        "Lambda-Runtime-Trace-Id",
        &invocation.trace_id,
    ) {
        return e;
    }

    if let Some(client_context) = &invocation.client_context
        && let Err(e) = safe_header_insert(
            &mut headers,
            "Lambda-Runtime-Client-Context",
            client_context,
        )
    {
        return e;
    }

    if let Some(cognito_identity) = &invocation.cognito_identity
        && let Err(e) = safe_header_insert(
            &mut headers,
            "Lambda-Runtime-Cognito-Identity",
            cognito_identity,
        )
    {
        return e;
    }

    let body_str = match serde_json::to_string(&invocation.payload) {
        Ok(s) => s,
        Err(e) => {
            tracing::error!("Failed to serialize invocation payload: {}", e);
            return (
                StatusCode::INTERNAL_SERVER_ERROR,
                "Failed to serialize invocation payload",
            )
                .into_response();
        }
    };

    (StatusCode::OK, headers, body_str).into_response()
}

/// POST /2018-06-01/runtime/invocation/:request_id/response
///
/// Reports a successful invocation response.
///
/// After recording the response and emitting `platform.runtimeDone`, this
/// spawns a background task to wait for all extensions to signal readiness
/// before emitting `platform.report`. The HTTP response is returned immediately.
///
/// Returns 413 if the response payload exceeds 6 MB.
/// Returns 404 if the request ID is not found.
/// Returns 400 if a response or error has already been recorded for this invocation.
async fn invocation_response(
    State(state): State<RuntimeApiState>,
    Path(request_id): Path<String>,
    body: String,
) -> Response {
    if body.len() > MAX_RESPONSE_PAYLOAD_BYTES {
        return (
            StatusCode::PAYLOAD_TOO_LARGE,
            format!(
                "Response payload size ({} bytes) exceeds Lambda's 6 MB limit",
                body.len()
            ),
        )
            .into_response();
    }

    // Check if the invocation exists
    let inv_state = match state.runtime.get_invocation_state(&request_id).await {
        Some(s) => s,
        None => {
            return (
                StatusCode::NOT_FOUND,
                format!("Unknown request ID: {}", request_id),
            )
                .into_response();
        }
    };

    let payload: Value = match serde_json::from_str(&body) {
        Ok(p) => p,
        Err(e) => {
            return (
                StatusCode::BAD_REQUEST,
                format!("Invalid JSON payload: {}", e),
            )
                .into_response();
        }
    };

    let received_at = Utc::now();
    let response = InvocationResponse {
        request_id: request_id.clone(),
        payload,
        received_at,
    };

    match state.runtime.record_response(response).await {
        RecordResult::Recorded => {}
        RecordResult::AlreadyCompleted => {
            return (
                StatusCode::BAD_REQUEST,
                "Response already submitted for this invocation",
            )
                .into_response();
        }
        RecordResult::NotFound => {
            return (StatusCode::NOT_FOUND, "Unknown request ID").into_response();
        }
    }

    // Proceed with telemetry emission since we successfully recorded
    {
        let duration_ms = if let Some(started_at) = inv_state.started_at {
            (received_at - started_at).num_milliseconds() as f64
        } else {
            0.0
        };

        let trace_context = TraceContext {
            trace_type: "X-Amzn-Trace-Id".to_string(),
            value: inv_state.invocation.trace_id.clone(),
            span_id: None,
        };

        let runtime_done = PlatformRuntimeDone {
            request_id: request_id.clone(),
            status: RuntimeStatus::Success,
            metrics: Some(RuntimeDoneMetrics {
                duration_ms,
                produced_bytes: None,
            }),
            spans: None,
            tracing: Some(trace_context.clone()),
        };

        let runtime_done_event = TelemetryEvent {
            time: Utc::now(),
            event_type: "platform.runtimeDone".to_string(),
            record: json!(runtime_done),
        };

        state
            .telemetry
            .broadcast_event(runtime_done_event, TelemetryEventType::Platform)
            .await;

        tracing::info!(target: "lambda_lifecycle", "✅ platform.runtimeDone (status: success, duration: {:.1}ms)", duration_ms);

        state.readiness.mark_runtime_done(&request_id).await;

        spawn_report_task(
            state.clone(),
            request_id.clone(),
            inv_state.invocation.created_at,
            received_at,
            RuntimeStatus::Success,
            trace_context,
        );
    }

    StatusCode::ACCEPTED.into_response()
}

/// Spawns a background task to wait for extension readiness, emit platform.report,
/// and freeze the process.
fn spawn_report_task(
    state: RuntimeApiState,
    request_id: String,
    invocation_created_at: chrono::DateTime<Utc>,
    runtime_done_at: chrono::DateTime<Utc>,
    status: RuntimeStatus,
    trace_context: TraceContext,
) {
    let timeout_ms = state.config.extension_ready_timeout_ms;
    let freeze_epoch = state.freeze.current_epoch();

    tokio::spawn(async move {
        let timeout = std::time::Duration::from_millis(timeout_ms);

        tokio::select! {
            _ = state.readiness.wait_for_all_ready(&request_id) => {
                tracing::debug!("All extensions ready for {}", request_id);
            }
            _ = tokio::time::sleep(timeout) => {
                tracing::warn!(
                    "Extension readiness timeout for {}; proceeding with report",
                    request_id
                );
            }
        }

        let extensions_ready_at = Utc::now();
        let extension_overhead_ms = state
            .readiness
            .get_extension_overhead_ms(&request_id)
            .await
            .unwrap_or_else(|| (extensions_ready_at - runtime_done_at).num_milliseconds() as f64);

        let total_duration_ms =
            (extensions_ready_at - invocation_created_at).num_milliseconds() as f64;
        let billed_duration_ms = total_duration_ms.ceil() as u64;

        let report = PlatformReport {
            request_id: request_id.clone(),
            status,
            metrics: ReportMetrics {
                duration_ms: total_duration_ms,
                billed_duration_ms,
                memory_size_mb: state.config.memory_size_mb as u64,
                max_memory_used_mb: (state.config.memory_size_mb / 2) as u64,
                init_duration_ms: None,
                restore_duration_ms: None,
                billed_restore_duration_ms: None,
            },
            spans: None,
            tracing: Some(trace_context),
        };

        if extension_overhead_ms >= 1.0 {
            tracing::info!(
                target: "lambda_lifecycle",
                "📊 platform.report (billed: {}ms, extension overhead: {:.0}ms)",
                billed_duration_ms,
                extension_overhead_ms
            );
        } else {
            tracing::info!(
                target: "lambda_lifecycle",
                "📊 platform.report (billed: {}ms)",
                billed_duration_ms
            );
        }

        let report_event = TelemetryEvent {
            time: Utc::now(),
            event_type: "platform.report".to_string(),
            record: json!(report),
        };

        state
            .telemetry
            .broadcast_event(report_event, TelemetryEventType::Platform)
            .await;

        state.readiness.cleanup_invocation(&request_id).await;

        match state.freeze.freeze_at_epoch(freeze_epoch) {
            Ok(true) => {
                tracing::info!(target: "lambda_lifecycle", "🧊 Environment frozen (SIGSTOP)");
            }
            Ok(false) => {
                // Epoch mismatch - new work arrived before freeze, which is expected behaviour
            }
            Err(e) => {
                tracing::error!(
                    "Failed to freeze processes after invocation: {}. \
                     Freeze simulation may be inaccurate.",
                    e
                );
            }
        }
    });
}

/// POST /2018-06-01/runtime/invocation/:request_id/error
///
/// Reports an invocation error.
///
/// After recording the error and emitting `platform.runtimeDone`, this
/// spawns a background task to wait for all extensions to signal readiness
/// before emitting `platform.report`. The HTTP response is returned immediately.
///
/// Returns 404 if the request ID is not found.
/// Returns 400 if a response or error has already been recorded for this invocation.
async fn invocation_error(
    State(state): State<RuntimeApiState>,
    Path(request_id): Path<String>,
    body: String,
) -> Response {
    // Parse the error payload manually since lambda_runtime doesn't send Content-Type header
    let error_payload: Value = match serde_json::from_str(&body) {
        Ok(v) => v,
        Err(e) => {
            return (StatusCode::BAD_REQUEST, format!("Invalid JSON: {}", e)).into_response();
        }
    };
    // Check if the invocation exists
    let inv_state = match state.runtime.get_invocation_state(&request_id).await {
        Some(s) => s,
        None => {
            return (
                StatusCode::NOT_FOUND,
                format!("Unknown request ID: {}", request_id),
            )
                .into_response();
        }
    };

    let error_type = error_payload
        .get("errorType")
        .and_then(|v| v.as_str())
        .unwrap_or("UnknownError")
        .to_string();

    let error_message = error_payload
        .get("errorMessage")
        .and_then(|v| v.as_str())
        .unwrap_or("Unknown error")
        .to_string();

    let stack_trace = error_payload
        .get("stackTrace")
        .and_then(|v| v.as_array())
        .map(|arr| {
            arr.iter()
                .filter_map(|v| v.as_str().map(|s| s.to_string()))
                .collect()
        });

    let received_at = Utc::now();
    let error = InvocationError {
        request_id: request_id.clone(),
        error_type: error_type.clone(),
        error_message,
        stack_trace,
        received_at,
    };

    match state.runtime.record_error(error).await {
        RecordResult::Recorded => {}
        RecordResult::AlreadyCompleted => {
            return (
                StatusCode::BAD_REQUEST,
                "Response already submitted for this invocation",
            )
                .into_response();
        }
        RecordResult::NotFound => {
            return (StatusCode::NOT_FOUND, "Unknown request ID").into_response();
        }
    }

    // Proceed with telemetry emission since we successfully recorded
    {
        let duration_ms = if let Some(started_at) = inv_state.started_at {
            (received_at - started_at).num_milliseconds() as f64
        } else {
            0.0
        };

        let trace_context = TraceContext {
            trace_type: "X-Amzn-Trace-Id".to_string(),
            value: inv_state.invocation.trace_id.clone(),
            span_id: None,
        };

        let runtime_done = PlatformRuntimeDone {
            request_id: request_id.clone(),
            status: RuntimeStatus::Error,
            metrics: Some(RuntimeDoneMetrics {
                duration_ms,
                produced_bytes: None,
            }),
            spans: None,
            tracing: Some(trace_context.clone()),
        };

        let runtime_done_event = TelemetryEvent {
            time: Utc::now(),
            event_type: "platform.runtimeDone".to_string(),
            record: json!(runtime_done),
        };

        state
            .telemetry
            .broadcast_event(runtime_done_event, TelemetryEventType::Platform)
            .await;

        tracing::info!(target: "lambda_lifecycle", "❌ platform.runtimeDone (status: error, type: {})", error_type);

        state.readiness.mark_runtime_done(&request_id).await;

        spawn_report_task(
            state.clone(),
            request_id.clone(),
            inv_state.invocation.created_at,
            received_at,
            RuntimeStatus::Error,
            trace_context,
        );
    }

    StatusCode::ACCEPTED.into_response()
}

/// POST /2018-06-01/runtime/init/error
///
/// Reports an initialization error.
async fn init_error(
    State(state): State<RuntimeApiState>,
    Json(error_payload): Json<Value>,
) -> Response {
    let error_type = error_payload
        .get("errorType")
        .and_then(|v| v.as_str())
        .unwrap_or("UnknownError");

    let error_message = error_payload
        .get("errorMessage")
        .and_then(|v| v.as_str())
        .unwrap_or("Unknown error");

    let error_string = format!("{}: {}", error_type, error_message);
    state.runtime.record_init_error(error_string).await;

    StatusCode::OK.into_response()
}