24
24
import botocore .exceptions
25
25
import os
26
26
27
- def lambda_handler (event , context ):
28
-
29
27
28
+ def lambda_handler (event , context ):
30
29
LOG_LEVEL = os .getenv ('LOG_LEVEL' )
31
30
logging .getLogger ().setLevel (LOG_LEVEL )
32
31
33
32
try :
34
33
35
- logging .info ('Event Body: ' )
34
+ logging .info (f 'Event: { event } ' )
36
35
37
36
body = json .loads (event ['Records' ][0 ]['body' ])
38
37
account_id = body ['Account' ]
@@ -87,8 +86,13 @@ def assume_role(account_id, role='AWSControlTowerExecution'):
87
86
try :
88
87
role_arn = 'arn:aws:iam::' + account_id + ':role/aws-controltower-ConfigRecorderRole'
89
88
89
+ CONFIG_RECORDER_DAILY_RESOURCE_STRING = os .getenv ('CONFIG_RECORDER_DAILY_RESOURCE_LIST' )
90
+ CONFIG_RECORDER_DAILY_RESOURCE_LIST = CONFIG_RECORDER_DAILY_RESOURCE_STRING .split (
91
+ ',' ) if CONFIG_RECORDER_DAILY_RESOURCE_STRING != '' else []
90
92
CONFIG_RECORDER_EXCLUSION_RESOURCE_STRING = os .getenv ('CONFIG_RECORDER_EXCLUDED_RESOURCE_LIST' )
91
- CONFIG_RECORDER_EXCLUSION_RESOURCE_LIST = CONFIG_RECORDER_EXCLUSION_RESOURCE_STRING .split (',' )
93
+ CONFIG_RECORDER_EXCLUSION_RESOURCE_LIST = CONFIG_RECORDER_EXCLUSION_RESOURCE_STRING .split (
94
+ ',' ) if CONFIG_RECORDER_EXCLUSION_RESOURCE_STRING != '' else []
95
+ CONFIG_RECORDER_RECORDING_FREQUENCY = os .getenv ('CONFIG_RECORDER_RECORDING_FREQUENCY' )
92
96
93
97
# Event = Delete is when stack is deleted, we rollback changed made and leave it as ControlTower Intended
94
98
if event == 'Delete' :
@@ -104,21 +108,38 @@ def assume_role(account_id, role='AWSControlTowerExecution'):
104
108
logging .info (f'Response for put_configuration_recorder :{ response } ' )
105
109
106
110
else :
107
- response = configservice .put_configuration_recorder (
108
- ConfigurationRecorder = {
109
- 'name' : 'aws-controltower-BaselineConfigRecorder' ,
110
- 'roleARN' : role_arn ,
111
- 'recordingGroup' : {
112
- 'allSupported' : False ,
113
- 'includeGlobalResourceTypes' : False ,
114
- 'exclusionByResourceTypes' : {
115
- 'resourceTypes' : CONFIG_RECORDER_EXCLUSION_RESOURCE_LIST
116
- },
117
- 'recordingStrategy' : {
118
- 'useOnly' : 'EXCLUSION_BY_RESOURCE_TYPES'
119
- }
111
+ config_recorder = {
112
+ 'name' : 'aws-controltower-BaselineConfigRecorder' ,
113
+ 'roleARN' : role_arn ,
114
+ 'recordingGroup' : {
115
+ 'allSupported' : False ,
116
+ 'includeGlobalResourceTypes' : False ,
117
+ 'exclusionByResourceTypes' : {
118
+ 'resourceTypes' : CONFIG_RECORDER_EXCLUSION_RESOURCE_LIST
119
+ },
120
+ 'recordingStrategy' : {
121
+ 'useOnly' : 'EXCLUSION_BY_RESOURCE_TYPES'
120
122
}
121
- })
123
+ },
124
+ 'recordingMode' : {
125
+ 'recordingFrequency' : CONFIG_RECORDER_RECORDING_FREQUENCY ,
126
+ 'recordingModeOverrides' : [
127
+ {
128
+ 'description' : 'DAILY_OVERRIDE' ,
129
+ 'resourceTypes' : CONFIG_RECORDER_DAILY_RESOURCE_LIST ,
130
+ 'recordingFrequency' : 'DAILY'
131
+ }
132
+ ] if CONFIG_RECORDER_DAILY_RESOURCE_LIST else []
133
+ }
134
+ }
135
+
136
+ if not CONFIG_RECORDER_EXCLUSION_RESOURCE_LIST :
137
+ config_recorder ['recordingGroup' ].pop ('exclusionByResourceTypes' )
138
+ config_recorder ['recordingGroup' ].pop ('recordingStrategy' )
139
+ config_recorder ['recordingGroup' ]['allSupported' ] = True
140
+ config_recorder ['recordingGroup' ]['includeGlobalResourceTypes' ] = True
141
+ response = configservice .put_configuration_recorder (
142
+ ConfigurationRecorder = config_recorder )
122
143
logging .info (f'Response for put_configuration_recorder :{ response } ' )
123
144
124
145
# lets describe for configuration recorder after the update
0 commit comments