fakecloud 0.30.2

Local AWS cloud emulator — free, open-source LocalStack alternative
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
//! Background poller that bridges DynamoDB Streams -> Lambda event source mappings.
//!
//! Periodically checks Lambda state for enabled event source mappings
//! pointing to DynamoDB streams, reads stream records, and invokes Lambda
//! functions with batches of records.

use std::sync::Arc;
use std::time::Duration;

use chrono::Utc;
use serde_json::json;

use fakecloud_core::delivery::LambdaDelivery;
use fakecloud_dynamodb::SharedDynamoDbState;
use fakecloud_lambda::filter::FilterSet;
use fakecloud_lambda::{LambdaInvocation, SharedLambdaState};

/// DynamoDB Streams -> Lambda event source mapping poller.
pub struct DynamoDbStreamsLambdaPoller {
    dynamodb_state: SharedDynamoDbState,
    lambda_state: SharedLambdaState,
    lambda_delivery: Option<Arc<dyn LambdaDelivery>>,
}

impl DynamoDbStreamsLambdaPoller {
    pub fn new(dynamodb_state: SharedDynamoDbState, lambda_state: SharedLambdaState) -> Self {
        Self {
            dynamodb_state,
            lambda_state,
            lambda_delivery: None,
        }
    }

    pub fn with_lambda_delivery(mut self, delivery: Arc<dyn LambdaDelivery>) -> Self {
        self.lambda_delivery = Some(delivery);
        self
    }

    pub async fn run(self: Arc<Self>) {
        let mut interval = tokio::time::interval(Duration::from_millis(500));
        loop {
            interval.tick().await;
            self.poll().await;
        }
    }

    async fn poll(&self) {
        struct DdbMapping {
            uuid: String,
            stream_arn: String,
            function_arn: String,
            batch_size: i64,
            filter: FilterSet,
            starting_position: Option<String>,
            starting_position_timestamp: Option<f64>,
        }

        // Collect enabled mappings that point to DynamoDB streams across
        // ALL accounts — a non-default/cross-account ESM must fire too, the
        // same way the Kinesis poller iterates every account.
        let mappings: Vec<DdbMapping> = {
            let lambda_accounts = self.lambda_state.read();
            lambda_accounts
                .iter()
                .flat_map(|(_, lambda)| {
                    lambda
                        .event_source_mappings
                        .values()
                        .filter(|m| {
                            m.enabled
                                && m.event_source_arn.contains(":dynamodb:")
                                && m.event_source_arn.contains("/stream/")
                        })
                        .map(|m| DdbMapping {
                            uuid: m.uuid.clone(),
                            stream_arn: m.event_source_arn.clone(),
                            function_arn: m.function_arn.clone(),
                            batch_size: m.batch_size,
                            filter: FilterSet::from_strings(m.filter_patterns.iter()),
                            starting_position: m.starting_position.clone(),
                            starting_position_timestamp: m.starting_position_timestamp,
                        })
                        .collect::<Vec<_>>()
                })
                .collect()
        };

        if mappings.is_empty() {
            return;
        }

        for DdbMapping {
            uuid: mapping_id,
            stream_arn,
            function_arn,
            batch_size,
            filter,
            starting_position,
            starting_position_timestamp,
        } in mappings
        {
            // Extract table name from stream ARN
            // Format: arn:aws:dynamodb:region:account:table/TableName/stream/timestamp
            let table_name = if let Some(table_part) = stream_arn.split(":table/").nth(1) {
                table_part.split('/').next().unwrap_or("")
            } else {
                continue;
            };
            // The DynamoDB state holding the table lives in the stream ARN's
            // own account, which is not necessarily the default account.
            let ddb_account = stream_arn.split(':').nth(4).unwrap_or("").to_string();

            // AT_TIMESTAMP isn't valid for DDB streams in real AWS;
            // suppress the field if the user supplied it.
            let _ = starting_position_timestamp;

            // Initialize the checkpoint based on StartingPosition the
            // first time we see this mapping. For DDB streams AWS only
            // accepts TRIM_HORIZON (default — replays existing records)
            // and LATEST (skip whatever is already in the stream).
            //
            // The checkpoint lives in the (persisted) DynamoDB state so it
            // survives a restart: on reload the key is already present, so
            // we resume from the last delivered sequence number instead of
            // re-seeding TRIM_HORIZON and re-invoking the target Lambda
            // with the whole retained backlog (duplicate side effects).
            let checkpoint = {
                let mut ddb_accounts = self.dynamodb_state.write();
                let dynamodb = match ddb_accounts.get_mut(&ddb_account) {
                    Some(d) => d,
                    None => continue,
                };
                if dynamodb.lambda_stream_checkpoint(&mapping_id).is_none() {
                    if let Some(table) = dynamodb.tables.get(table_name) {
                        let stream_records = table.stream_records.read();
                        let init = match starting_position.as_deref().unwrap_or("TRIM_HORIZON") {
                            "LATEST" => stream_records
                                .iter()
                                .map(|r| r.dynamodb.sequence_number.clone())
                                .max()
                                .unwrap_or_default(),
                            _ => String::new(),
                        };
                        drop(stream_records);
                        dynamodb.set_lambda_stream_checkpoint(&mapping_id, init);
                    }
                }
                dynamodb.lambda_stream_checkpoint(&mapping_id)
            };

            // Read stream records from DynamoDB
            let records = {
                let ddb_accounts = self.dynamodb_state.read();
                let dynamodb = match ddb_accounts.get(&ddb_account) {
                    Some(d) => d,
                    None => continue,
                };
                let table = match dynamodb.tables.get(table_name) {
                    Some(t) => t,
                    None => continue,
                };

                if !table.stream_enabled {
                    continue;
                }

                let stream_records = table.stream_records.read();

                // Filter records after checkpoint
                let mut filtered: Vec<_> = stream_records
                    .iter()
                    .filter(|r| match checkpoint.as_deref() {
                        Some(cp) if !cp.is_empty() => r.dynamodb.sequence_number.as_str() > cp,
                        _ => true,
                    })
                    .take(batch_size.max(0) as usize)
                    .cloned()
                    .collect();

                // Sort by sequence number to ensure order
                filtered
                    .sort_by(|a, b| a.dynamodb.sequence_number.cmp(&b.dynamodb.sequence_number));

                filtered
            };

            if records.is_empty() {
                continue;
            }

            // Build per-record Lambda event JSON, then drop any record
            // whose JSON doesn't match FilterCriteria. AWS treats
            // filtered-out records as consumed — the checkpoint always
            // advances past them.
            let last_seq = records.last().map(|r| r.dynamodb.sequence_number.clone());
            let event_records: Vec<serde_json::Value> = records
                .iter()
                .filter_map(|record| {
                    let mut event_record = json!({
                        "eventID": record.event_id,
                        "eventName": record.event_name,
                        "eventVersion": record.event_version,
                        "eventSource": record.event_source,
                        "awsRegion": record.aws_region,
                        "dynamodb": {
                            "Keys": record.dynamodb.keys,
                            "SequenceNumber": record.dynamodb.sequence_number,
                            "SizeBytes": record.dynamodb.size_bytes,
                            "StreamViewType": record.dynamodb.stream_view_type,
                        },
                        "eventSourceARN": record.event_source_arn,
                    });

                    if let Some(ref new_img) = record.dynamodb.new_image {
                        event_record["dynamodb"]["NewImage"] = json!(new_img);
                    }
                    if let Some(ref old_img) = record.dynamodb.old_image {
                        event_record["dynamodb"]["OldImage"] = json!(old_img);
                    }

                    if filter.matches(&event_record) {
                        Some(event_record)
                    } else {
                        None
                    }
                })
                .collect();

            // If the filter dropped every record, advance the
            // checkpoint past them — AWS treats filtered records as
            // consumed. Otherwise, hold the checkpoint until after a
            // successful invoke so failures retry on the next poll.
            if event_records.is_empty() {
                if let Some(seq) = last_seq.clone() {
                    self.advance_checkpoint(&ddb_account, &mapping_id, seq);
                }
                continue;
            }

            let event = json!({ "Records": &event_records });
            let payload = serde_json::to_string(&event).unwrap_or_default();

            let invoke_succeeded = match &self.lambda_delivery {
                Some(delivery) => match delivery.invoke_lambda(&function_arn, &payload).await {
                    Ok(_) => {
                        tracing::info!(
                            function_arn = %function_arn,
                            record_count = event_records.len(),
                            "DynamoDB Streams->Lambda invocation succeeded"
                        );
                        true
                    }
                    Err(e) => {
                        tracing::error!(
                            function_arn = %function_arn,
                            error = %e,
                            "DynamoDB Streams->Lambda invocation failed"
                        );
                        false
                    }
                },
                None => true,
            };

            if !invoke_succeeded {
                continue;
            }

            // Successful invoke — advance the checkpoint and record
            // the invocation when no real Lambda runtime is wired.
            if let Some(seq) = last_seq.clone() {
                self.advance_checkpoint(&ddb_account, &mapping_id, seq);
            }

            if self.lambda_delivery.is_none() {
                let fn_account = function_arn.split(':').nth(4).unwrap_or("");
                let mut lambda_accounts = self.lambda_state.write();
                let lambda = lambda_accounts.get_or_create(fn_account);
                lambda.invocations.push(LambdaInvocation {
                    function_arn: function_arn.clone(),
                    payload: payload.clone(),
                    timestamp: Utc::now(),
                    source: "dynamodb:streams".to_string(),
                });
            }
        }
    }

    /// Persist the last-delivered sequence number for a mapping into the
    /// DynamoDB state. The value rides along with the next DynamoDB
    /// snapshot save, so it survives a restart (mirrors how Kinesis lambda
    /// checkpoints persist through their snapshot).
    fn advance_checkpoint(&self, ddb_account: &str, mapping_id: &str, sequence_number: String) {
        let mut ddb_accounts = self.dynamodb_state.write();
        if let Some(dynamodb) = ddb_accounts.get_mut(ddb_account) {
            dynamodb.set_lambda_stream_checkpoint(mapping_id, sequence_number);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use fakecloud_core::multi_account::MultiAccountState;
    use fakecloud_core::service::{AwsRequest, AwsService};
    use fakecloud_dynamodb::{DynamoDbService, DynamoDbState};
    use fakecloud_lambda::{EventSourceMapping, LambdaState};
    use parking_lot::RwLock;
    use serde_json::Value;

    const DEFAULT_ACCOUNT: &str = "123456789012";
    const REGION: &str = "us-east-1";
    const ENDPOINT: &str = "http://localhost:4566";

    fn make_request(account: &str, action: &str, body: Value) -> AwsRequest {
        AwsRequest {
            service: "dynamodb".to_string(),
            action: action.to_string(),
            region: REGION.to_string(),
            account_id: account.to_string(),
            request_id: "test-id".to_string(),
            headers: axum::http::HeaderMap::new(),
            query_params: std::collections::HashMap::new(),
            body: serde_json::to_vec(&body).unwrap().into(),
            body_stream: parking_lot::Mutex::new(None),
            path_segments: vec![],
            raw_path: "/".to_string(),
            raw_query: String::new(),
            method: axum::http::Method::POST,
            is_query_protocol: false,
            access_key_id: None,
            principal: None,
        }
    }

    async fn create_streamed_table(svc: &DynamoDbService, account: &str, table: &str) {
        svc.handle(make_request(
            account,
            "CreateTable",
            serde_json::json!({
                "TableName": table,
                "KeySchema": [{ "AttributeName": "pk", "KeyType": "HASH" }],
                "AttributeDefinitions": [{ "AttributeName": "pk", "AttributeType": "S" }],
                "BillingMode": "PAY_PER_REQUEST",
                "StreamSpecification": {
                    "StreamEnabled": true,
                    "StreamViewType": "NEW_AND_OLD_IMAGES"
                }
            }),
        ))
        .await
        .expect("CreateTable should succeed");
    }

    async fn put_item(svc: &DynamoDbService, account: &str, table: &str, pk: &str) {
        svc.handle(make_request(
            account,
            "PutItem",
            serde_json::json!({
                "TableName": table,
                "Item": { "pk": { "S": pk } }
            }),
        ))
        .await
        .expect("PutItem should succeed");
    }

    fn stream_arn(state: &SharedDynamoDbState, account: &str, table: &str) -> String {
        state
            .read()
            .get(account)
            .expect("account exists")
            .tables
            .get(table)
            .expect("table exists")
            .stream_arn
            .clone()
            .expect("stream enabled")
    }

    fn esm(uuid: &str, account: &str, stream_arn: &str) -> EventSourceMapping {
        EventSourceMapping {
            uuid: uuid.to_string(),
            function_arn: format!("arn:aws:lambda:{REGION}:{account}:function:streamed-fn"),
            event_source_arn: stream_arn.to_string(),
            batch_size: 10,
            enabled: true,
            state: "Enabled".to_string(),
            last_modified: Utc::now(),
            filter_patterns: Vec::new(),
            maximum_batching_window_in_seconds: None,
            starting_position: Some("TRIM_HORIZON".to_string()),
            starting_position_timestamp: None,
            parallelization_factor: None,
            function_response_types: Vec::new(),
            kms_key_arn: None,
            metrics_config: None,
            destination_config: None,
            maximum_retry_attempts: None,
            maximum_record_age_in_seconds: None,
            bisect_batch_on_function_error: None,
            tumbling_window_in_seconds: None,
            topics: Vec::new(),
            queues: Vec::new(),
            source_access_configurations: Vec::new(),
        }
    }

    fn lambda_state_with(account: &str, mapping: EventSourceMapping) -> SharedLambdaState {
        let mut lambda: MultiAccountState<LambdaState> =
            MultiAccountState::new(DEFAULT_ACCOUNT, REGION, ENDPOINT);
        lambda
            .get_or_create(account)
            .event_source_mappings
            .insert(mapping.uuid.clone(), mapping);
        Arc::new(RwLock::new(lambda))
    }

    fn invocation_count(lambda_state: &SharedLambdaState, account: &str) -> usize {
        lambda_state
            .read()
            .get(account)
            .map(|l| l.invocations.len())
            .unwrap_or(0)
    }

    /// Deliver a batch, then "restart" by round-tripping the DynamoDB state
    /// through serde and rebuilding the poller against the restored state +
    /// a fresh Lambda state. The durable checkpoint must prevent the whole
    /// retained backlog from being re-replayed to the target Lambda.
    #[tokio::test]
    async fn checkpoint_survives_restart_no_replay() {
        let dynamodb_state: SharedDynamoDbState = Arc::new(RwLock::new(MultiAccountState::new(
            DEFAULT_ACCOUNT,
            REGION,
            ENDPOINT,
        )));
        let svc = DynamoDbService::new(dynamodb_state.clone());
        create_streamed_table(&svc, DEFAULT_ACCOUNT, "t").await;
        put_item(&svc, DEFAULT_ACCOUNT, "t", "a").await;
        put_item(&svc, DEFAULT_ACCOUNT, "t", "b").await;

        let arn = stream_arn(&dynamodb_state, DEFAULT_ACCOUNT, "t");
        let lambda_state = lambda_state_with(DEFAULT_ACCOUNT, esm("esm-1", DEFAULT_ACCOUNT, &arn));

        let poller = DynamoDbStreamsLambdaPoller::new(dynamodb_state.clone(), lambda_state.clone());
        poller.poll().await;
        assert_eq!(
            invocation_count(&lambda_state, DEFAULT_ACCOUNT),
            1,
            "first poll delivers the backlog as one batch"
        );
        // A second poll on the same process must not re-deliver.
        poller.poll().await;
        assert_eq!(
            invocation_count(&lambda_state, DEFAULT_ACCOUNT),
            1,
            "no re-delivery while checkpoint is current"
        );

        // "Restart": persist + reload the DynamoDB state, fresh Lambda state.
        let serialized = serde_json::to_string(&*dynamodb_state.read()).unwrap();
        let restored: MultiAccountState<DynamoDbState> = serde_json::from_str(&serialized).unwrap();
        let restored_state: SharedDynamoDbState = Arc::new(RwLock::new(restored));
        // The checkpoint must have survived the round-trip.
        assert!(
            restored_state
                .read()
                .get(DEFAULT_ACCOUNT)
                .unwrap()
                .lambda_stream_checkpoint("esm-1")
                .is_some(),
            "checkpoint persisted through snapshot"
        );

        let lambda_state2 = lambda_state_with(DEFAULT_ACCOUNT, esm("esm-1", DEFAULT_ACCOUNT, &arn));
        let poller2 = DynamoDbStreamsLambdaPoller::new(restored_state, lambda_state2.clone());
        poller2.poll().await;
        assert_eq!(
            invocation_count(&lambda_state2, DEFAULT_ACCOUNT),
            0,
            "restart must resume from the durable checkpoint, not re-replay TRIM_HORIZON"
        );
    }

    /// An event source mapping on a non-default / cross-account stream must
    /// still fire — the poller used to serve only the default account.
    #[tokio::test]
    async fn non_default_account_mapping_fires() {
        const OTHER: &str = "999999999999";
        let dynamodb_state: SharedDynamoDbState = Arc::new(RwLock::new(MultiAccountState::new(
            DEFAULT_ACCOUNT,
            REGION,
            ENDPOINT,
        )));
        let svc = DynamoDbService::new(dynamodb_state.clone());
        create_streamed_table(&svc, OTHER, "t").await;
        put_item(&svc, OTHER, "t", "a").await;

        let arn = stream_arn(&dynamodb_state, OTHER, "t");
        assert!(arn.contains(OTHER), "stream arn carries the other account");
        let lambda_state = lambda_state_with(OTHER, esm("esm-x", OTHER, &arn));

        let poller = DynamoDbStreamsLambdaPoller::new(dynamodb_state.clone(), lambda_state.clone());
        poller.poll().await;
        assert_eq!(
            invocation_count(&lambda_state, OTHER),
            1,
            "cross-account mapping must fire"
        );
    }
}