Skip to content

Eventbridge Event Processor #704

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be an alias for EventBridgeEventObj<Option<String>> to avoid code duplication?

Copy link
Contributor Author

@nichmorgan nichmorgan Oct 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are having some problems creating this Alias due to the use of this setting in the "detail" field:

#[serde_as(as = "serde_with::json::JsonString")]
#[serde(bound(deserialize = "T: DeserializeOwned"))]
pub detail: T,

As the input of this field will always be a String, we expect it to be a JsonString as an EventBridgeEventObj, but if you create an alias EventBridgeEventObj<Option> and its contents are a simple String, not a JsonString, the test will fail.

We're still working on solutions to avoid duplication, but so far we've had no success. Any suggestions?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok with this duplication. We can change it later if we find a way.

#[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;