Skip to content

Commit c1e0596

Browse files
leandrodamascenarafaelgsr
authored andcommitted
feat(event_sources): add AWS Config Rule event data class (aws-powertools#2175)
1 parent 762f6af commit c1e0596

11 files changed

+648
-0
lines changed

aws_lambda_powertools/utilities/data_classes/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from .alb_event import ALBEvent
66
from .api_gateway_proxy_event import APIGatewayProxyEvent, APIGatewayProxyEventV2
77
from .appsync_resolver_event import AppSyncResolverEvent
8+
from .aws_config_rule_event import AWSConfigRuleEvent
89
from .cloud_watch_custom_widget_event import CloudWatchDashboardCustomWidgetEvent
910
from .cloud_watch_logs_event import CloudWatchLogsEvent
1011
from .code_pipeline_job_event import CodePipelineJobEvent
@@ -43,5 +44,6 @@
4344
"SNSEvent",
4445
"SQSEvent",
4546
"event_source",
47+
"AWSConfigRuleEvent",
4648
"VPCLatticeEvent",
4749
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,361 @@
1+
from __future__ import annotations
2+
3+
import json
4+
from typing import Any, Dict, List, Optional
5+
6+
from aws_lambda_powertools.utilities.data_classes.common import DictWrapper
7+
8+
9+
def get_invoke_event(
10+
invoking_event: dict,
11+
) -> AWSConfigConfigurationChanged | AWSConfigScheduledNotification | AWSConfigOversizedConfiguration:
12+
"""
13+
Returns the corresponding event object based on the messageType in the invoking event.
14+
15+
Parameters
16+
----------
17+
invoking_event: dict
18+
The invoking event received.
19+
20+
Returns
21+
-------
22+
AWSConfigConfigurationChanged | AWSConfigScheduledNotification | AWSConfigOversizedConfiguration:
23+
The event object based on the messageType in the invoking event.
24+
"""
25+
26+
message_type = invoking_event.get("messageType")
27+
28+
if message_type == "ScheduledNotification":
29+
return AWSConfigScheduledNotification(invoking_event)
30+
31+
if message_type == "OversizedConfigurationItemChangeNotification":
32+
return AWSConfigOversizedConfiguration(invoking_event)
33+
34+
# Default return is AWSConfigConfigurationChanged event
35+
return AWSConfigConfigurationChanged(invoking_event)
36+
37+
38+
class AWSConfigConfigurationChanged(DictWrapper):
39+
@property
40+
def configuration_item_diff(self) -> Dict:
41+
"""The configuration item diff of the ConfigurationItemChangeNotification event."""
42+
return self["configurationItemDiff"]
43+
44+
@property
45+
def configuration_item(self) -> AWSConfigConfigurationItemChanged:
46+
"""The configuration item of the ConfigurationItemChangeNotification event."""
47+
return AWSConfigConfigurationItemChanged(self["configurationItem"])
48+
49+
@property
50+
def raw_configuration_item(self) -> Dict:
51+
"""The raw configuration item of the ConfigurationItemChangeNotification event."""
52+
return self["configurationItem"]
53+
54+
@property
55+
def record_version(self) -> str:
56+
"""The record version of the ConfigurationItemChangeNotification event."""
57+
return self["recordVersion"]
58+
59+
@property
60+
def message_type(self) -> str:
61+
"""The message type of the ConfigurationItemChangeNotification event."""
62+
return self["messageType"]
63+
64+
@property
65+
def notification_creation_time(self) -> str:
66+
"""The notification creation time of the ConfigurationItemChangeNotification event."""
67+
return self["notificationCreationTime"]
68+
69+
70+
class AWSConfigConfigurationItemChanged(DictWrapper):
71+
@property
72+
def related_events(self) -> List:
73+
"""The related events of the ConfigurationItemChangeNotification event."""
74+
return self["relatedEvents"]
75+
76+
@property
77+
def relationships(self) -> List:
78+
"""The relationships of the ConfigurationItemChangeNotification event."""
79+
return self["relationships"]
80+
81+
@property
82+
def configuration(self) -> Dict:
83+
"""The configuration of the ConfigurationItemChangeNotification event."""
84+
return self["configuration"]
85+
86+
@property
87+
def supplementary_configuration(self) -> Dict:
88+
"""The supplementary configuration of the ConfigurationItemChangeNotification event."""
89+
return self["supplementaryConfiguration"]
90+
91+
@property
92+
def tags(self) -> Dict:
93+
"""The tags of the ConfigurationItemChangeNotification event."""
94+
return self["tags"]
95+
96+
@property
97+
def configuration_item_version(self) -> str:
98+
"""The configuration item version of the ConfigurationItemChangeNotification event."""
99+
return self["configurationItemVersion"]
100+
101+
@property
102+
def configuration_item_capture_time(self) -> str:
103+
"""The configuration item capture time of the ConfigurationItemChangeNotification event."""
104+
return self["configurationItemCaptureTime"]
105+
106+
@property
107+
def configuration_state_id(self) -> str:
108+
"""The configuration state id of the ConfigurationItemChangeNotification event."""
109+
return self["configurationStateId"]
110+
111+
@property
112+
def accountid(self) -> str:
113+
"""The accountid of the ConfigurationItemChangeNotification event."""
114+
return self["awsAccountId"]
115+
116+
@property
117+
def configuration_item_status(self) -> str:
118+
"""The configuration item status of the ConfigurationItemChangeNotification event."""
119+
return self["configurationItemStatus"]
120+
121+
@property
122+
def resource_type(self) -> str:
123+
"""The resource type of the ConfigurationItemChangeNotification event."""
124+
return self["resourceType"]
125+
126+
@property
127+
def resource_id(self) -> str:
128+
"""The resource id of the ConfigurationItemChangeNotification event."""
129+
return self["resourceId"]
130+
131+
@property
132+
def resource_name(self) -> str:
133+
"""The resource name of the ConfigurationItemChangeNotification event."""
134+
return self["resourceName"]
135+
136+
@property
137+
def resource_arn(self) -> str:
138+
"""The resource arn of the ConfigurationItemChangeNotification event."""
139+
return self["ARN"]
140+
141+
@property
142+
def region(self) -> str:
143+
"""The region of the ConfigurationItemChangeNotification event."""
144+
return self["awsRegion"]
145+
146+
@property
147+
def availability_zone(self) -> str:
148+
"""The availability zone of the ConfigurationItemChangeNotification event."""
149+
return self["availabilityZone"]
150+
151+
@property
152+
def configuration_state_md5_hash(self) -> str:
153+
"""The md5 hash of the state of the ConfigurationItemChangeNotification event."""
154+
return self["configurationStateMd5Hash"]
155+
156+
@property
157+
def resource_creation_time(self) -> str:
158+
"""The resource creation time of the ConfigurationItemChangeNotification event."""
159+
return self["resourceCreationTime"]
160+
161+
162+
class AWSConfigScheduledNotification(DictWrapper):
163+
@property
164+
def accountid(self) -> str:
165+
"""The accountid of the ScheduledNotification event."""
166+
return self["awsAccountId"]
167+
168+
@property
169+
def notification_creation_time(self) -> str:
170+
"""The notification creation time of the ScheduledNotification event."""
171+
return self["notificationCreationTime"]
172+
173+
@property
174+
def record_version(self) -> str:
175+
"""The record version of the ScheduledNotification event."""
176+
return self["recordVersion"]
177+
178+
@property
179+
def message_type(self) -> str:
180+
"""The message type of the ScheduledNotification event."""
181+
return self["messageType"]
182+
183+
184+
class AWSConfigOversizedConfiguration(DictWrapper):
185+
@property
186+
def configuration_item_summary(self) -> AWSConfigOversizedConfigurationItemSummary:
187+
"""The configuration item summary of the OversizedConfiguration event."""
188+
return AWSConfigOversizedConfigurationItemSummary(self["configurationItemSummary"])
189+
190+
@property
191+
def raw_configuration_item_summary(self) -> str:
192+
"""The raw configuration item summary of the OversizedConfiguration event."""
193+
return self["configurationItemSummary"]
194+
195+
@property
196+
def message_type(self) -> str:
197+
"""The message type of the OversizedConfiguration event."""
198+
return self["messageType"]
199+
200+
@property
201+
def notification_creation_time(self) -> str:
202+
"""The notification creation time of the OversizedConfiguration event."""
203+
return self["notificationCreationTime"]
204+
205+
@property
206+
def record_version(self) -> str:
207+
"""The record version of the OversizedConfiguration event."""
208+
return self["recordVersion"]
209+
210+
211+
class AWSConfigOversizedConfigurationItemSummary(DictWrapper):
212+
@property
213+
def change_type(self) -> str:
214+
"""The change type of the OversizedConfiguration event."""
215+
return self["changeType"]
216+
217+
@property
218+
def configuration_item_version(self) -> str:
219+
"""The configuration item version of the OversizedConfiguration event."""
220+
return self["configurationItemVersion"]
221+
222+
@property
223+
def configuration_item_capture_time(self) -> str:
224+
"""The configuration item capture time of the OversizedConfiguration event."""
225+
return self["configurationItemCaptureTime"]
226+
227+
@property
228+
def configuration_state_id(self) -> str:
229+
"""The configuration state id of the OversizedConfiguration event."""
230+
return self["configurationStateId"]
231+
232+
@property
233+
def accountid(self) -> str:
234+
"""The accountid of the OversizedConfiguration event."""
235+
return self["awsAccountId"]
236+
237+
@property
238+
def configuration_item_status(self) -> str:
239+
"""The configuration item status of the OversizedConfiguration event."""
240+
return self["configurationItemStatus"]
241+
242+
@property
243+
def resource_type(self) -> str:
244+
"""The resource type of the OversizedConfiguration event."""
245+
return self["resourceType"]
246+
247+
@property
248+
def resource_id(self) -> str:
249+
"""The resource id of the OversizedConfiguration event."""
250+
return self["resourceId"]
251+
252+
@property
253+
def resource_name(self) -> str:
254+
"""The resource name of the OversizedConfiguration event."""
255+
return self["resourceName"]
256+
257+
@property
258+
def resource_arn(self) -> str:
259+
"""The resource arn of the OversizedConfiguration event."""
260+
return self["ARN"]
261+
262+
@property
263+
def region(self) -> str:
264+
"""The region of the OversizedConfiguration event."""
265+
return self["awsRegion"]
266+
267+
@property
268+
def availability_zone(self) -> str:
269+
"""The availability zone of the OversizedConfiguration event."""
270+
return self["availabilityZone"]
271+
272+
@property
273+
def configuration_state_md5_hash(self) -> str:
274+
"""The state md5 hash of the OversizedConfiguration event."""
275+
return self["configurationStateMd5Hash"]
276+
277+
@property
278+
def resource_creation_time(self) -> str:
279+
"""The resource creation time of the OversizedConfiguration event."""
280+
return self["resourceCreationTime"]
281+
282+
283+
class AWSConfigRuleEvent(DictWrapper):
284+
"""Events for AWS Config Rules
285+
Documentation:
286+
--------------
287+
- https://docs.aws.amazon.com/config/latest/developerguide/evaluate-config_develop-rules_lambda-functions.html
288+
"""
289+
290+
def __init__(self, data: Dict[str, Any]):
291+
super().__init__(data)
292+
self._invoking_event: Optional[Any] = None
293+
self._rule_parameters: Optional[Any] = None
294+
295+
@property
296+
def version(self) -> str:
297+
"""The version of the event."""
298+
return self["version"]
299+
300+
@property
301+
def invoking_event(
302+
self,
303+
) -> AWSConfigConfigurationChanged | AWSConfigScheduledNotification | AWSConfigOversizedConfiguration:
304+
"""The invoking payload of the event."""
305+
if self._invoking_event is None:
306+
self._invoking_event = self["invokingEvent"]
307+
308+
return get_invoke_event(json.loads(self._invoking_event))
309+
310+
@property
311+
def raw_invoking_event(self) -> str:
312+
"""The raw invoking payload of the event."""
313+
return self["invokingEvent"]
314+
315+
@property
316+
def rule_parameters(self) -> Dict:
317+
"""The parameters of the event."""
318+
if self._rule_parameters is None:
319+
self._rule_parameters = self["ruleParameters"]
320+
321+
return json.loads(self._rule_parameters)
322+
323+
@property
324+
def result_token(self) -> str:
325+
"""The result token of the event."""
326+
return self["resultToken"]
327+
328+
@property
329+
def event_left_scope(self) -> bool:
330+
"""The left scope of the event."""
331+
return self["eventLeftScope"]
332+
333+
@property
334+
def execution_role_arn(self) -> str:
335+
"""The execution role arn of the event."""
336+
return self["executionRoleArn"]
337+
338+
@property
339+
def config_rule_arn(self) -> str:
340+
"""The arn of the rule of the event."""
341+
return self["configRuleArn"]
342+
343+
@property
344+
def config_rule_name(self) -> str:
345+
"""The name of the rule of the event."""
346+
return self["configRuleName"]
347+
348+
@property
349+
def config_rule_id(self) -> str:
350+
"""The id of the rule of the event."""
351+
return self["configRuleId"]
352+
353+
@property
354+
def accountid(self) -> str:
355+
"""The accountid of the event."""
356+
return self["accountId"]
357+
358+
@property
359+
def evalution_mode(self) -> Optional[str]:
360+
"""The evalution mode of the event."""
361+
return self.get("evaluationMode")

0 commit comments

Comments
 (0)