|
| 1 | +// Example demonstrating builder pattern usage for AWS Lambda events |
| 2 | +use aws_lambda_events::event::{ |
| 3 | + dynamodb::{Event as DynamoDbEvent, EventRecord as DynamoDbEventRecord, StreamRecord}, |
| 4 | + kinesis::{KinesisEvent, KinesisEventRecord, KinesisRecord, KinesisEncryptionType}, |
| 5 | + s3::{S3Event, S3EventRecord, S3Entity, S3Bucket, S3Object, S3RequestParameters, S3UserIdentity}, |
| 6 | + secretsmanager::SecretsManagerSecretRotationEvent, |
| 7 | + sns::{SnsEvent, SnsRecord, SnsMessage}, |
| 8 | + sqs::{SqsEvent, SqsMessage}, |
| 9 | +}; |
| 10 | +use std::collections::HashMap; |
| 11 | + |
| 12 | +fn main() { |
| 13 | + // S3 Event - Object storage notifications with nested structures |
| 14 | + let s3_record = S3EventRecord::builder() |
| 15 | + .event_time(chrono::Utc::now()) |
| 16 | + .principal_id(S3UserIdentity::builder().build()) |
| 17 | + .request_parameters(S3RequestParameters::builder().build()) |
| 18 | + .response_elements(HashMap::new()) |
| 19 | + .s3(S3Entity::builder() |
| 20 | + .bucket(S3Bucket::builder().name("my-bucket".to_string()).build()) |
| 21 | + .object(S3Object::builder().key("file.txt".to_string()).size(1024).build()) |
| 22 | + .build()) |
| 23 | + .build(); |
| 24 | + let _s3_event = S3Event::builder().records(vec![s3_record]).build(); |
| 25 | + |
| 26 | + // Kinesis Event - Stream processing with data |
| 27 | + let kinesis_record = KinesisEventRecord::builder() |
| 28 | + .kinesis(KinesisRecord::builder() |
| 29 | + .data(serde_json::from_str("\"SGVsbG8gV29ybGQ=\"").unwrap()) |
| 30 | + .partition_key("key-1".to_string()) |
| 31 | + .sequence_number("12345".to_string()) |
| 32 | + .approximate_arrival_timestamp(serde_json::from_str("1234567890.0").unwrap()) |
| 33 | + .encryption_type(KinesisEncryptionType::None) |
| 34 | + .build()) |
| 35 | + .build(); |
| 36 | + let _kinesis_event = KinesisEvent::builder().records(vec![kinesis_record]).build(); |
| 37 | + |
| 38 | + // DynamoDB Event - Database change streams with item data |
| 39 | + let mut keys = HashMap::new(); |
| 40 | + keys.insert("id".to_string(), serde_dynamo::AttributeValue::S("123".to_string())); |
| 41 | + |
| 42 | + let dynamodb_record = DynamoDbEventRecord::builder() |
| 43 | + .aws_region("us-east-1".to_string()) |
| 44 | + .change(StreamRecord::builder() |
| 45 | + .approximate_creation_date_time(chrono::Utc::now()) |
| 46 | + .keys(keys.into()) |
| 47 | + .new_image(HashMap::new().into()) |
| 48 | + .old_image(HashMap::new().into()) |
| 49 | + .size_bytes(100) |
| 50 | + .build()) |
| 51 | + .event_id("event-123".to_string()) |
| 52 | + .event_name("INSERT".to_string()) |
| 53 | + .build(); |
| 54 | + let _dynamodb_event = DynamoDbEvent::builder().records(vec![dynamodb_record]).build(); |
| 55 | + |
| 56 | + // SNS Event - Pub/sub messaging with message details |
| 57 | + let sns_record = SnsRecord::builder() |
| 58 | + .event_source("aws:sns".to_string()) |
| 59 | + .event_version("1.0".to_string()) |
| 60 | + .event_subscription_arn("arn:aws:sns:us-east-1:123456789012:topic".to_string()) |
| 61 | + .sns(SnsMessage::builder() |
| 62 | + .message("Hello from SNS".to_string()) |
| 63 | + .sns_message_type("Notification".to_string()) |
| 64 | + .message_id("msg-123".to_string()) |
| 65 | + .topic_arn("arn:aws:sns:us-east-1:123456789012:topic".to_string()) |
| 66 | + .timestamp(chrono::Utc::now()) |
| 67 | + .signature_version("1".to_string()) |
| 68 | + .signature("sig".to_string()) |
| 69 | + .signing_cert_url("https://cert.url".to_string()) |
| 70 | + .unsubscribe_url("https://unsub.url".to_string()) |
| 71 | + .message_attributes(HashMap::new()) |
| 72 | + .build()) |
| 73 | + .build(); |
| 74 | + let _sns_event = SnsEvent::builder().records(vec![sns_record]).build(); |
| 75 | + |
| 76 | + // SQS Event - Queue messaging with attributes |
| 77 | + let mut attrs = HashMap::new(); |
| 78 | + attrs.insert("ApproximateReceiveCount".to_string(), "1".to_string()); |
| 79 | + attrs.insert("SentTimestamp".to_string(), "1234567890".to_string()); |
| 80 | + |
| 81 | + let sqs_message = SqsMessage::builder() |
| 82 | + .attributes(attrs) |
| 83 | + .message_attributes(HashMap::new()) |
| 84 | + .body("message body".to_string()) |
| 85 | + .message_id("msg-456".to_string()) |
| 86 | + .build(); |
| 87 | + |
| 88 | + #[cfg(feature = "catch-all-fields")] |
| 89 | + let _sqs_event = SqsEvent::builder() |
| 90 | + .records(vec![sqs_message]) |
| 91 | + .other(serde_json::Map::new()) |
| 92 | + .build(); |
| 93 | + |
| 94 | + #[cfg(not(feature = "catch-all-fields"))] |
| 95 | + let _sqs_event = SqsEvent::builder().records(vec![sqs_message]).build(); |
| 96 | + |
| 97 | + // Secrets Manager Event - Secret rotation |
| 98 | + let _secrets_event = SecretsManagerSecretRotationEvent::builder() |
| 99 | + .step("createSecret".to_string()) |
| 100 | + .secret_id("test-secret".to_string()) |
| 101 | + .client_request_token("token-123".to_string()) |
| 102 | + .build(); |
| 103 | +} |
0 commit comments