in opensearch/setupCWSubscriptionFilter.py [0:0]
def destroy(cwLogGroupPrefix):
# Delete subscription filter from all CW Logs across regions for Amazon OpenSearch Service
for region in json.loads(REGIONS_TO_MONITOR):
print("Starting to delete CW Log filters for", region)
# Create CW Logs client
cw_logs_client = boto3.client('logs', region_name=region)
response = cw_logs_client.describe_log_groups(
logGroupNamePrefix=cwLogGroupPrefix
)
# Read response which is dict, and change that to json with "
json_response = json.dumps(response)
# Parse JSON data to extract logGroups
log_groups = json.loads(json_response)["logGroups"]
# Traverse each log group to list cw logs filter and delete the one starting with 'subscriptionFilterNamePrefix'
for log_group in log_groups:
print("Processing logGroups:", log_group["arn"])
filter_response = cw_logs_client.describe_subscription_filters(
logGroupName=log_group["logGroupName"],
filterNamePrefix=subscriptionFilterNamePrefix
)
# Read response which is dict, and change that to json with quotes "
filter_json_response = json.dumps(filter_response)
subscription_filters = json.loads(filter_json_response)["subscriptionFilters"]
# Iterate subscriptionFilter to delete
for filter in subscription_filters:
print("Deleting subscriptionFilter:", filter["filterName"])
cw_logs_client.delete_subscription_filter(
logGroupName=log_group["logGroupName"],
filterName=filter["filterName"]
)