@@ -727,8 +727,19 @@ def deploy_metric_filters_and_alarms(region, accounts, resource_properties):
727727 LOGGER .info (f"{ filter_name } parameters: { filter_params } " )
728728 if filter_deploy is False :
729729 LOGGER .info (f"{ filter_name } filter not requested (deploy set to false). Checking to see if any need to be removed..." )
730- delete_metric_filter_alarm_topic_and_key (filter_name , acct , region , filter_params )
731-
730+ if filter_regions :
731+ LOGGER .info (f"Checking { filter_name } filter in regions: { filter_regions } ..." )
732+ if region not in filter_regions :
733+ LOGGER .info (f"Check found that { filter_name } filter was not requested for { region } . Skipping region..." )
734+ else :
735+ for acct in accounts :
736+ if filter_accounts :
737+ LOGGER .info (f"Checking filter_accounts: { filter_accounts } " )
738+ if acct not in filter_accounts :
739+ LOGGER .info (f"Check found that { filter_name } filter not requested for { acct } . Skipping account..." )
740+ else :
741+ LOGGER .info (f"Check found that { filter_name } filter was defined for { acct } in { region } ; Checking for need to be removed..." )
742+ delete_metric_filter_and_alarm (filter_name , acct , region , filter_params )
732743 continue
733744 if filter_regions :
734745 LOGGER .info (f"{ filter_name } filter regions: { filter_regions } " )
@@ -1289,7 +1300,26 @@ def delete_custom_config_iam_role(rule_name: str, acct: str):
12891300 else :
12901301 LOGGER .info (f"{ rule_name } IAM role for account { acct } in { region } does not exist." )
12911302
1292- def delete_metric_filter_alarm_topic_and_key (filter_name : str , acct : str , region : str , filter_params : str ):
1303+ def delete_sns_topic_and_key (acct : str , region : str ):
1304+ # Delete the alarm topic
1305+ sns .SNS_CLIENT = sts .assume_role (acct , sts .CONFIGURATION_ROLE , "sns" , region )
1306+ # TODO(liamschn): this will be a mypy error - need to have alarm_topic_search (sns.find_sns_topic) return string, not None
1307+ alarm_topic_search = sns .find_sns_topic (f"{ SOLUTION_NAME } -alarms" , region , acct )
1308+ if alarm_topic_search is not None :
1309+ if DRY_RUN is False :
1310+ LOGGER .info (f"Deleting { SOLUTION_NAME } -alarms SNS topic" )
1311+ LIVE_RUN_DATA ["SNSDelete" ] = f"Deleted { SOLUTION_NAME } -alarms SNS topic"
1312+ sns .delete_sns_topic (alarm_topic_search )
1313+ CFN_RESPONSE_DATA ["deployment_info" ]["action_count" ] += 1
1314+ CFN_RESPONSE_DATA ["deployment_info" ]["resources_deployed" ] -= 1
1315+ LOGGER .info (f"Deleted { SOLUTION_NAME } -alarms SNS topic" )
1316+ remove_state_table_record (alarm_topic_search )
1317+ else :
1318+ LOGGER .info (f"DRY_RUN: Delete { SOLUTION_NAME } -alarms SNS topic" )
1319+ DRY_RUN_DATA ["SNSDelete" ] = f"DRY_RUN: Delete { SOLUTION_NAME } -alarms SNS topic"
1320+ else :
1321+ LOGGER .info (f"{ SOLUTION_NAME } -alarms SNS topic does not exist." )
1322+
12931323 # Delete KMS key (schedule deletion) and delete kms alias
12941324 kms .KMS_CLIENT = sts .assume_role (acct , sts .CONFIGURATION_ROLE , "kms" , region )
12951325 search_alarm_kms_key , alarm_key_alias , alarm_key_id , alarm_key_arn = kms .check_alias_exists (kms .KMS_CLIENT , f"alias/{ ALARM_SNS_KEY_ALIAS } " )
@@ -1319,6 +1349,8 @@ def delete_metric_filter_alarm_topic_and_key(filter_name: str, acct: str, region
13191349 else :
13201350 LOGGER .info (f"{ ALARM_SNS_KEY_ALIAS } KMS key does not exist." )
13211351
1352+
1353+ def delete_metric_filter_and_alarm (filter_name : str , acct : str , region : str , filter_params : dict ):
13221354 cloudwatch .CWLOGS_CLIENT = sts .assume_role (acct , sts .CONFIGURATION_ROLE , "logs" , region )
13231355 cloudwatch .CLOUDWATCH_CLIENT = sts .assume_role (acct , sts .CONFIGURATION_ROLE , "cloudwatch" , region )
13241356 if DRY_RUN is False :
@@ -1357,26 +1389,6 @@ def delete_metric_filter_alarm_topic_and_key(filter_name: str, acct: str, region
13571389 LOGGER .info (f"DRY_RUN: Delete { filter_name } CloudWatch metric filter" )
13581390 DRY_RUN_DATA [f"{ filter_name } _CloudWatchDelete" ] = f"DRY_RUN: Delete { filter_name } CloudWatch metric filter"
13591391
1360- # Delete the alarm topic
1361- sns .SNS_CLIENT = sts .assume_role (acct , sts .CONFIGURATION_ROLE , "sns" , region )
1362- # TODO(liamschn): this will be a mypy error - need to have alarm_topic_search (sns.find_sns_topic) return string, not None
1363- alarm_topic_search = sns .find_sns_topic (f"{ SOLUTION_NAME } -alarms" , region , acct )
1364- if alarm_topic_search is not None :
1365- if DRY_RUN is False :
1366- LOGGER .info (f"Deleting { SOLUTION_NAME } -alarms SNS topic" )
1367- LIVE_RUN_DATA ["SNSDelete" ] = f"Deleted { SOLUTION_NAME } -alarms SNS topic"
1368- sns .delete_sns_topic (alarm_topic_search )
1369- CFN_RESPONSE_DATA ["deployment_info" ]["action_count" ] += 1
1370- CFN_RESPONSE_DATA ["deployment_info" ]["resources_deployed" ] -= 1
1371- LOGGER .info (f"Deleted { SOLUTION_NAME } -alarms SNS topic" )
1372- remove_state_table_record (alarm_topic_search )
1373- else :
1374- LOGGER .info (f"DRY_RUN: Delete { SOLUTION_NAME } -alarms SNS topic" )
1375- DRY_RUN_DATA ["SNSDelete" ] = f"DRY_RUN: Delete { SOLUTION_NAME } -alarms SNS topic"
1376- else :
1377- LOGGER .info (f"{ SOLUTION_NAME } -alarms SNS topic does not exist." )
1378-
1379-
13801392def delete_event (event , context ):
13811393 # TODO(liamschn): handle delete error if IAM policy is updated out-of-band - botocore.errorfactory.DeleteConflictException: An error occurred (DeleteConflict) when calling the DeletePolicy operation: This policy has more than one version. Before you delete a policy, you must delete the policy's versions. The default version is deleted with the policy.
13821394 # TODO(liamschn): move re-used delete event operation code to separate functions
@@ -1505,7 +1517,8 @@ def delete_event(event, context):
15051517 filter_deploy , filter_accounts , filter_regions , filter_params = get_filter_params (filter_name , event ["ResourceProperties" ])
15061518 for acct in filter_accounts :
15071519 for region in filter_regions :
1508- delete_metric_filter_alarm_topic_and_key (filter_name , acct , region , filter_params )
1520+ delete_metric_filter_and_alarm (filter_name , acct , region , filter_params )
1521+ delete_sns_topic_and_key (acct , region )
15091522
15101523 # 4) Delete config rules
15111524 # TODO(liamschn): deal with invalid rule names?
0 commit comments