Skip to content

Commit

Permalink
Eventbridge Event Processor (#704)
Browse files Browse the repository at this point in the history
* Eventbridge Event Processor

* cfg feature fix

* feature comment

* Removed whitespace

* makefile fix

---------

Co-authored-by: nich.morgan <[email protected]>
Co-authored-by: erso <[email protected]>
  • Loading branch information
3 people authored Oct 19, 2023
1 parent 1cbe34b commit bcd3f97
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ check-event-features:
cargo test --package aws_lambda_events --no-default-features --features sns
cargo test --package aws_lambda_events --no-default-features --features sqs
cargo test --package aws_lambda_events --no-default-features --features streams
cargo test --package aws_lambda_events --no-default-features --features eventbridge

fmt:
cargo +nightly fmt --all
2 changes: 2 additions & 0 deletions lambda-events/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ default = [
"sns",
"sqs",
"streams",
"eventbridge",
]

activemq = []
Expand Down Expand Up @@ -117,3 +118,4 @@ ses = ["chrono"]
sns = ["chrono", "serde_with"]
sqs = ["serde_with"]
streams = []
eventbridge = ["chrono", "serde_with"]
87 changes: 87 additions & 0 deletions lambda-events/src/event/eventbridge/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use chrono::{DateTime, Utc};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use serde_with::serde_as;

#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub struct EventBridgeEvent {
#[serde(default)]
pub version: Option<String>,
#[serde(default)]
pub id: Option<String>,
pub detail_type: String,
pub source: String,
#[serde(default)]
pub account: Option<String>,
#[serde(default)]
pub time: Option<DateTime<Utc>>,
#[serde(default)]
pub region: Option<String>,
#[serde(default)]
pub resources: Option<Vec<String>>,
#[serde(default)]
pub detail: Option<String>,
}

#[serde_with::serde_as]
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(bound(deserialize = "T: DeserializeOwned"))]
#[serde(rename_all = "kebab-case")]
pub struct EventBridgeEventObj<T: Serialize> {
#[serde(default)]
pub version: Option<String>,
#[serde(default)]
pub id: Option<String>,
pub detail_type: String,
pub source: String,
#[serde(default)]
pub account: Option<String>,
#[serde(default)]
pub time: Option<DateTime<Utc>>,
#[serde(default)]
pub region: Option<String>,
#[serde(default)]
pub resources: Option<Vec<String>>,
#[serde_as(as = "serde_with::json::JsonString")]
#[serde(bound(deserialize = "T: DeserializeOwned"))]
pub detail: T,
}

#[cfg(test)]
#[cfg(feature = "eventbridge")]
mod test {
use super::*;

use serde_json;

#[test]
fn example_eventbridge_obj_event() {
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
struct CustomStruct {
a: String,
b: String,
}

let data = include_bytes!("../../fixtures/example-eventbridge-event-obj.json");
let parsed: EventBridgeEventObj<CustomStruct> = serde_json::from_slice(data).unwrap();

assert_eq!(parsed.detail.a, "123");
assert_eq!(parsed.detail.b, "456");

let output: String = serde_json::to_string(&parsed).unwrap();
let reparsed: EventBridgeEventObj<CustomStruct> = serde_json::from_slice(output.as_bytes()).unwrap();
assert_eq!(parsed, reparsed);
}

#[test]
fn example_eventbridge_event() {
let data = include_bytes!("../../fixtures/example-eventbridge-event.json");
let parsed: EventBridgeEvent = serde_json::from_slice(data).unwrap();
assert_eq!(parsed.detail, Some(String::from("String Message")));

let output: String = serde_json::to_string(&parsed).unwrap();
let reparsed: EventBridgeEvent = serde_json::from_slice(output.as_bytes()).unwrap();
assert_eq!(parsed, reparsed);
}
}
4 changes: 4 additions & 0 deletions lambda-events/src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,7 @@ pub mod sqs;
/// AWS Lambda event definitions for streams.
#[cfg(feature = "streams")]
pub mod streams;

/// AWS Lambda event definitions for EventBridge.
#[cfg(feature = "eventbridge")]
pub mod eventbridge;
13 changes: 13 additions & 0 deletions lambda-events/src/fixtures/example-eventbridge-event-obj.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "0",
"id": "6a7e8feb-b491-4cf7-a9f1-bf3703467718",
"detail-type": "EC2 Instance State-change Notification",
"source": "aws.ec2",
"account": "111122223333",
"time": "2017-12-22T18:43:48Z",
"region": "us-west-1",
"resources": [
"arn:aws:ec2:us-west-1:123456789012:instance/i-1234567890abcdef0"
],
"detail": "{\"a\":\"123\",\"b\":\"456\"}"
}
13 changes: 13 additions & 0 deletions lambda-events/src/fixtures/example-eventbridge-event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "0",
"id": "6a7e8feb-b491-4cf7-a9f1-bf3703467718",
"detail-type": "EC2 Instance State-change Notification",
"source": "aws.ec2",
"account": "111122223333",
"time": "2017-12-22T18:43:48Z",
"region": "us-west-1",
"resources": [
"arn:aws:ec2:us-west-1:123456789012:instance/i-1234567890abcdef0"
],
"detail": "String Message"
}
4 changes: 4 additions & 0 deletions lambda-events/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,7 @@ pub use event::sqs;
/// AWS Lambda event definitions for streams.
#[cfg(feature = "streams")]
pub use event::streams;

/// AWS Lambda event definitions for EventBridge.
#[cfg(feature = "eventbridge")]
pub use event::eventbridge;

0 comments on commit bcd3f97

Please sign in to comment.