aws_lambda_events 1.1.3

AWS Lambda event definitions
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
#[cfg(feature = "builders")]
use bon::Builder;
use std::collections::HashMap;

use chrono::{DateTime, Utc};
use serde::{
    de::{DeserializeOwned, Visitor},
    ser::Error as SerError,
    Deserialize, Serialize,
};
use serde_json::Value;

/// `CloudWatchAlarm` is the generic outer structure of an event triggered by a CloudWatch Alarm.
/// You probably want to use `CloudWatchMetricAlarm` or `CloudWatchCompositeAlarm` if you know which kind of alarm your function is receiving.
/// For examples of events that come via CloudWatch Alarms,
/// see <https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/AlarmThatSendsEmail.html#Lambda-action-payload>
#[non_exhaustive]
#[cfg_attr(feature = "builders", derive(Builder))]
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CloudWatchAlarm<C = Value, R = CloudWatchAlarmStateReasonData>
where
    C: DeserializeOwned,
    C: Serialize,
    R: DeserializeOwned,
    R: Serialize,
{
    #[serde(default)]
    pub account_id: Option<String>,
    #[serde(default)]
    pub alarm_arn: Option<String>,
    #[serde(default)]
    pub source: Option<String>,
    #[serde(default)]
    pub region: Option<String>,
    pub time: DateTime<Utc>,

    #[serde(default, bound = "")]
    pub alarm_data: CloudWatchAlarmData<C, R>,
    /// Catchall to catch any additional fields that were present but not explicitly defined by this struct.
    /// Enabled with Cargo feature `catch-all-fields`.
    /// If `catch-all-fields` is disabled, any additional fields that are present will be ignored.
    #[cfg(feature = "catch-all-fields")]
    #[cfg_attr(docsrs, doc(cfg(feature = "catch-all-fields")))]
    #[serde(flatten)]
    #[cfg_attr(feature = "builders", builder(default))]
    pub other: serde_json::Map<String, Value>,
}

/// `CloudWatchMetricAlarm` is the structure of an event triggered by CloudWatch metric alarms.
pub type CloudWatchMetricAlarm<R = CloudWatchAlarmStateReasonData> =
    CloudWatchAlarm<CloudWatchMetricAlarmConfiguration, R>;

/// `CloudWatchCompositeAlarm` is the structure of an event triggered by CloudWatch composite alarms.
pub type CloudWatchCompositeAlarm<R = CloudWatchAlarmStateReasonData> =
    CloudWatchAlarm<CloudWatchCompositeAlarmConfiguration, R>;

#[non_exhaustive]
#[cfg_attr(feature = "builders", derive(Builder))]
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CloudWatchAlarmData<C = Value, R = CloudWatchAlarmStateReasonData>
where
    C: DeserializeOwned,
    C: Serialize,
    R: DeserializeOwned,
    R: Serialize,
{
    pub alarm_name: String,
    #[serde(default, bound = "")]
    pub state: Option<CloudWatchAlarmState<R>>,
    #[serde(default, bound = "")]
    pub previous_state: Option<CloudWatchAlarmState<R>>,
    #[serde(bound = "")]
    pub configuration: C,
    /// Catchall to catch any additional fields that were present but not explicitly defined by this struct.
    /// Enabled with Cargo feature `catch-all-fields`.
    /// If `catch-all-fields` is disabled, any additional fields that are present will be ignored.
    #[cfg(feature = "catch-all-fields")]
    #[cfg_attr(docsrs, doc(cfg(feature = "catch-all-fields")))]
    #[serde(flatten)]
    #[cfg_attr(feature = "builders", builder(default))]
    pub other: serde_json::Map<String, Value>,
}

#[non_exhaustive]
#[cfg_attr(feature = "builders", derive(Builder))]
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CloudWatchAlarmState<R = Value>
where
    R: DeserializeOwned,
    R: Serialize,
{
    #[serde(default)]
    pub value: CloudWatchAlarmStateValue,
    pub reason: String,
    #[serde(default, bound = "")]
    pub reason_data: Option<R>,
    pub timestamp: DateTime<Utc>,
    pub actions_suppressed_by: Option<String>,
    pub actions_suppressed_reason: Option<String>,
    /// Catchall to catch any additional fields that were present but not explicitly defined by this struct.
    /// Enabled with Cargo feature `catch-all-fields`.
    /// If `catch-all-fields` is disabled, any additional fields that are present will be ignored.
    #[cfg(feature = "catch-all-fields")]
    #[cfg_attr(docsrs, doc(cfg(feature = "catch-all-fields")))]
    #[serde(flatten)]
    #[cfg_attr(feature = "builders", builder(default))]
    pub other: serde_json::Map<String, Value>,
}

#[non_exhaustive]
#[cfg_attr(feature = "builders", derive(Builder))]
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CloudWatchMetricAlarmConfiguration {
    #[serde(default)]
    pub description: Option<String>,
    #[serde(default)]
    pub metrics: Vec<CloudWatchMetricDefinition>,
    /// Catchall to catch any additional fields that were present but not explicitly defined by this struct.
    /// Enabled with Cargo feature `catch-all-fields`.
    /// If `catch-all-fields` is disabled, any additional fields that are present will be ignored.
    #[cfg(feature = "catch-all-fields")]
    #[cfg_attr(docsrs, doc(cfg(feature = "catch-all-fields")))]
    #[serde(flatten)]
    #[cfg_attr(feature = "builders", builder(default))]
    pub other: serde_json::Map<String, Value>,
}

#[non_exhaustive]
#[cfg_attr(feature = "builders", derive(Builder))]
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CloudWatchMetricDefinition {
    pub id: String,
    #[serde(default)]
    pub return_data: bool,
    pub metric_stat: CloudWatchMetricStatDefinition,
    /// Catchall to catch any additional fields that were present but not explicitly defined by this struct.
    /// Enabled with Cargo feature `catch-all-fields`.
    /// If `catch-all-fields` is disabled, any additional fields that are present will be ignored.
    #[cfg(feature = "catch-all-fields")]
    #[cfg_attr(docsrs, doc(cfg(feature = "catch-all-fields")))]
    #[serde(flatten)]
    #[cfg_attr(feature = "builders", builder(default))]
    pub other: serde_json::Map<String, Value>,
}

#[non_exhaustive]
#[cfg_attr(feature = "builders", derive(Builder))]
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CloudWatchMetricStatDefinition {
    #[serde(default)]
    pub unit: Option<String>,
    #[serde(default)]
    pub stat: Option<String>,
    pub period: u16,
    pub metric: CloudWatchMetricStatMetricDefinition,
    /// Catchall to catch any additional fields that were present but not explicitly defined by this struct.
    /// Enabled with Cargo feature `catch-all-fields`.
    /// If `catch-all-fields` is disabled, any additional fields that are present will be ignored.
    #[cfg(feature = "catch-all-fields")]
    #[cfg_attr(docsrs, doc(cfg(feature = "catch-all-fields")))]
    #[serde(flatten)]
    #[cfg_attr(feature = "builders", builder(default))]
    pub other: serde_json::Map<String, Value>,
}

#[non_exhaustive]
#[cfg_attr(feature = "builders", derive(Builder))]
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CloudWatchMetricStatMetricDefinition {
    #[serde(default)]
    pub namespace: Option<String>,
    pub name: String,
    pub dimensions: HashMap<String, String>,
    /// Catchall to catch any additional fields that were present but not explicitly defined by this struct.
    /// Enabled with Cargo feature `catch-all-fields`.
    /// If `catch-all-fields` is disabled, any additional fields that are present will be ignored.
    #[cfg(feature = "catch-all-fields")]
    #[cfg_attr(docsrs, doc(cfg(feature = "catch-all-fields")))]
    #[serde(flatten)]
    #[cfg_attr(feature = "builders", builder(default))]
    pub other: serde_json::Map<String, Value>,
}

#[non_exhaustive]
#[cfg_attr(feature = "builders", derive(Builder))]
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CloudWatchCompositeAlarmConfiguration {
    pub alarm_rule: String,
    pub actions_suppressor: String,
    pub actions_suppressor_wait_period: u16,
    pub actions_suppressor_extension_period: u16,
    /// Catchall to catch any additional fields that were present but not explicitly defined by this struct.
    /// Enabled with Cargo feature `catch-all-fields`.
    /// If `catch-all-fields` is disabled, any additional fields that are present will be ignored.
    #[cfg(feature = "catch-all-fields")]
    #[cfg_attr(docsrs, doc(cfg(feature = "catch-all-fields")))]
    #[serde(flatten)]
    #[cfg_attr(feature = "builders", builder(default))]
    pub other: serde_json::Map<String, Value>,
}

#[non_exhaustive]
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum CloudWatchAlarmStateValue {
    #[default]
    Ok,
    Alarm,
    InsufficientData,
}

#[non_exhaustive]
#[derive(Clone, Debug, PartialEq)]
pub enum CloudWatchAlarmStateReasonData {
    Metric(CloudWatchAlarmStateReasonDataMetric),
    Composite(ClodWatchAlarmStateReasonDataComposite),
    Generic(Value),
}

impl Default for CloudWatchAlarmStateReasonData {
    fn default() -> Self {
        Self::Generic(Value::String(String::new()))
    }
}

#[non_exhaustive]
#[cfg_attr(feature = "builders", derive(Builder))]
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CloudWatchAlarmStateReasonDataMetric {
    pub version: String,
    #[serde(default)]
    pub query_date: Option<DateTime<Utc>>,
    #[serde(default)]
    pub start_date: Option<DateTime<Utc>>,
    #[serde(default)]
    pub unit: Option<String>,
    #[serde(default)]
    pub statistic: Option<String>,
    pub period: u16,
    #[serde(default)]
    pub recent_datapoints: Vec<f64>,
    #[serde(default)]
    pub recent_lower_thresholds: Vec<f64>,
    #[serde(default)]
    pub recent_upper_thresholds: Vec<f64>,
    pub threshold: f64,
    #[serde(default)]
    pub evaluated_datapoints: Vec<CloudWatchAlarmStateEvaluatedDatapoint>,
    /// Catchall to catch any additional fields that were present but not explicitly defined by this struct.
    /// Enabled with Cargo feature `catch-all-fields`.
    /// If `catch-all-fields` is disabled, any additional fields that are present will be ignored.
    #[cfg(feature = "catch-all-fields")]
    #[cfg_attr(docsrs, doc(cfg(feature = "catch-all-fields")))]
    #[serde(flatten)]
    #[cfg_attr(feature = "builders", builder(default))]
    pub other: serde_json::Map<String, Value>,
}

#[non_exhaustive]
#[cfg_attr(feature = "builders", derive(Builder))]
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CloudWatchAlarmStateEvaluatedDatapoint {
    pub timestamp: DateTime<Utc>,
    #[serde(default)]
    pub sample_count: Option<f64>,
    #[serde(default)]
    pub value: Option<f64>,
    #[serde(default)]
    pub threshold: Option<f64>,
    /// Catchall to catch any additional fields that were present but not explicitly defined by this struct.
    /// Enabled with Cargo feature `catch-all-fields`.
    /// If `catch-all-fields` is disabled, any additional fields that are present will be ignored.
    #[cfg(feature = "catch-all-fields")]
    #[cfg_attr(docsrs, doc(cfg(feature = "catch-all-fields")))]
    #[serde(flatten)]
    #[cfg_attr(feature = "builders", builder(default))]
    pub other: serde_json::Map<String, Value>,
}

#[non_exhaustive]
#[cfg_attr(feature = "builders", derive(Builder))]
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ClodWatchAlarmStateReasonDataComposite {
    #[serde(default)]
    pub triggering_alarms: Vec<CloudWatchAlarmStateTriggeringAlarm>,
    /// Catchall to catch any additional fields that were present but not explicitly defined by this struct.
    /// Enabled with Cargo feature `catch-all-fields`.
    /// If `catch-all-fields` is disabled, any additional fields that are present will be ignored.
    #[cfg(feature = "catch-all-fields")]
    #[cfg_attr(docsrs, doc(cfg(feature = "catch-all-fields")))]
    #[serde(flatten)]
    #[cfg_attr(feature = "builders", builder(default))]
    pub other: serde_json::Map<String, Value>,
}

#[non_exhaustive]
#[cfg_attr(feature = "builders", derive(Builder))]
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CloudWatchAlarmStateTriggeringAlarm {
    pub arn: String,
    pub state: CloudWatchAlarmStateTriggeringAlarmState,
    /// Catchall to catch any additional fields that were present but not explicitly defined by this struct.
    /// Enabled with Cargo feature `catch-all-fields`.
    /// If `catch-all-fields` is disabled, any additional fields that are present will be ignored.
    #[cfg(feature = "catch-all-fields")]
    #[cfg_attr(docsrs, doc(cfg(feature = "catch-all-fields")))]
    #[serde(flatten)]
    #[cfg_attr(feature = "builders", builder(default))]
    pub other: serde_json::Map<String, Value>,
}

#[non_exhaustive]
#[cfg_attr(feature = "builders", derive(Builder))]
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CloudWatchAlarmStateTriggeringAlarmState {
    pub timestamp: DateTime<Utc>,
    #[serde(default)]
    pub value: CloudWatchAlarmStateValue,
    /// Catchall to catch any additional fields that were present but not explicitly defined by this struct.
    /// Enabled with Cargo feature `catch-all-fields`.
    /// If `catch-all-fields` is disabled, any additional fields that are present will be ignored.
    #[cfg(feature = "catch-all-fields")]
    #[cfg_attr(docsrs, doc(cfg(feature = "catch-all-fields")))]
    #[serde(flatten)]
    #[cfg_attr(feature = "builders", builder(default))]
    pub other: serde_json::Map<String, Value>,
}

impl<'de> Deserialize<'de> for CloudWatchAlarmStateReasonData {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        deserializer.deserialize_any(ReasonDataVisitor)
    }
}

impl Serialize for CloudWatchAlarmStateReasonData {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let r = match self {
            Self::Metric(m) => serde_json::to_string(m),
            Self::Composite(m) => serde_json::to_string(m),
            Self::Generic(m) => serde_json::to_string(m),
        };
        let s = r.map_err(|e| SerError::custom(format!("failed to serialize struct as string {e}")))?;

        serializer.serialize_str(&s)
    }
}

struct ReasonDataVisitor;

impl Visitor<'_> for ReasonDataVisitor {
    type Value = CloudWatchAlarmStateReasonData;

    fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.write_str("a string with the alarm state reason data")
    }

    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        if let Ok(metric) = serde_json::from_str::<CloudWatchAlarmStateReasonDataMetric>(v) {
            return Ok(CloudWatchAlarmStateReasonData::Metric(metric));
        }
        if let Ok(aggregate) = serde_json::from_str::<ClodWatchAlarmStateReasonDataComposite>(v) {
            return Ok(CloudWatchAlarmStateReasonData::Composite(aggregate));
        }
        Ok(CloudWatchAlarmStateReasonData::Generic(Value::String(v.to_owned())))
    }
}

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

    #[test]
    #[cfg(feature = "cloudwatch_alarms")]
    fn example_cloudwatch_alarm_metric() {
        let data = include_bytes!("../../fixtures/example-cloudwatch-alarm-metric.json");
        let parsed: CloudWatchMetricAlarm = serde_json::from_slice(data).unwrap();
        let state = parsed.alarm_data.previous_state.clone().unwrap();
        let data = state.reason_data.unwrap();
        match &data {
            CloudWatchAlarmStateReasonData::Metric(d) => {
                assert_eq!("1.0", d.version);
                assert_eq!(5, d.evaluated_datapoints.len());
            }
            _ => panic!("unexpected reason data {data:?}"),
        }

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

    #[test]
    #[cfg(feature = "cloudwatch_alarms")]
    fn example_cloudwatch_alarm_composite() {
        let data = include_bytes!("../../fixtures/example-cloudwatch-alarm-composite.json");
        let parsed: CloudWatchCompositeAlarm = serde_json::from_slice(data).unwrap();

        let state = parsed.alarm_data.state.clone().unwrap();
        let data = state.reason_data.unwrap();
        match &data {
            CloudWatchAlarmStateReasonData::Composite(d) => {
                assert_eq!(1, d.triggering_alarms.len());
                assert_eq!(
                    CloudWatchAlarmStateValue::Alarm,
                    d.triggering_alarms.first().unwrap().state.value
                );
            }
            _ => panic!("unexpected reason data {data:?}"),
        }

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

    #[test]
    #[cfg(feature = "cloudwatch_alarms")]
    fn example_cloudwatch_alarm_composite_with_suppressor_alarm() {
        let data = include_bytes!("../../fixtures/example-cloudwatch-alarm-composite-with-suppressor-alarm.json");
        let parsed: CloudWatchCompositeAlarm = serde_json::from_slice(data).unwrap();
        let state = parsed.alarm_data.state.clone().unwrap();
        assert_eq!("WaitPeriod", state.actions_suppressed_by.unwrap());
        assert_eq!(
            "Actions suppressed by WaitPeriod",
            state.actions_suppressed_reason.unwrap()
        );

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