Skip to content

Commit f984602

Browse files
authored
Merge pull request #13 from drem-darios/feature/support-daily-periodic-recording
Added the ability to set the recording frequency for individual resources
2 parents f0c5996 + 4edc5ff commit f984602

File tree

2 files changed

+57
-20
lines changed

2 files changed

+57
-20
lines changed

ct_configrecorder_override_consumer.py

+39-18
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,14 @@
2424
import botocore.exceptions
2525
import os
2626

27-
def lambda_handler(event, context):
28-
2927

28+
def lambda_handler(event, context):
3029
LOG_LEVEL = os.getenv('LOG_LEVEL')
3130
logging.getLogger().setLevel(LOG_LEVEL)
3231

3332
try:
3433

35-
logging.info('Event Body:')
34+
logging.info(f'Event: {event}')
3635

3736
body = json.loads(event['Records'][0]['body'])
3837
account_id = body['Account']
@@ -87,8 +86,13 @@ def assume_role(account_id, role='AWSControlTowerExecution'):
8786
try:
8887
role_arn = 'arn:aws:iam::' + account_id + ':role/aws-controltower-ConfigRecorderRole'
8988

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 []
9092
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')
9296

9397
# Event = Delete is when stack is deleted, we rollback changed made and leave it as ControlTower Intended
9498
if event == 'Delete':
@@ -104,21 +108,38 @@ def assume_role(account_id, role='AWSControlTowerExecution'):
104108
logging.info(f'Response for put_configuration_recorder :{response} ')
105109

106110
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'
120122
}
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)
122143
logging.info(f'Response for put_configuration_recorder :{response} ')
123144

124145
# lets describe for configuration recorder after the update

template.yaml

+18-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ Parameters:
1414
Default: "AWS::HealthLake::FHIRDatastore,AWS::Pinpoint::Segment,AWS::Pinpoint::ApplicationSettings"
1515
Type: String
1616

17+
ConfigRecorderDailyResourceTypes:
18+
Description: List of all resource types to be set to a daily cadence
19+
Default: "AWS::HealthLake::FHIRDatastore,AWS::Pinpoint::Segment,AWS::Pinpoint::ApplicationSettings"
20+
Type: String
21+
22+
ConfigRecorderRecordingFrequency:
23+
Description: Frequency of recording configuration changes.
24+
Default: CONTINUOUS
25+
Type: String
26+
AllowedValues:
27+
- CONTINUOUS
28+
- DAILY
29+
1730
CloudFormationVersion:
1831
Type: String
1932
Default: 2
@@ -43,6 +56,7 @@ Resources:
4356
Bool:
4457
aws:SecureTransport: false
4558

59+
4660
ProducerLambda:
4761
Type: AWS::Lambda::Function
4862
DeletionPolicy: Retain
@@ -54,7 +68,7 @@ Resources:
5468
S3Key: ct-blogs-content/ct_configrecorder_override_producer.zip
5569
Handler: ct_configrecorder_override_producer.lambda_handler
5670
Role: !GetAtt ProducerLambdaExecutionRole.Arn
57-
Runtime: python3.10
71+
Runtime: python3.11
5872
MemorySize: 128
5973
Timeout: 300
6074
Architectures:
@@ -86,7 +100,7 @@ Resources:
86100
S3Key: ct-blogs-content/ct_configrecorder_override_consumer_v2.zip
87101
Handler: ct_configrecorder_override_consumer.lambda_handler
88102
Role: !GetAtt ConsumerLambdaExecutionRole.Arn
89-
Runtime: python3.10
103+
Runtime: python3.11
90104
MemorySize: 128
91105
Timeout: 180
92106
Architectures:
@@ -95,7 +109,9 @@ Resources:
95109
Environment:
96110
Variables:
97111
LOG_LEVEL: INFO
112+
CONFIG_RECORDER_DAILY_RESOURCE_LIST: !Ref ConfigRecorderDailyResourceTypes
98113
CONFIG_RECORDER_EXCLUDED_RESOURCE_LIST: !Ref ConfigRecorderExcludedResourceTypes
114+
CONFIG_RECORDER_RECORDING_FREQUENCY: !Ref ConfigRecorderRecordingFrequency
99115

100116
ConsumerLambdaEventSourceMapping:
101117
Type: AWS::Lambda::EventSourceMapping

0 commit comments

Comments
 (0)