agent: |
zaXjou6Wa8HPVB5INfDpDelete AWS CloudWatch Dashboard
Delete AWS CloudWatch Dashboard
There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.
This task involves removing specific AWS CloudWatch dashboards that are no longer needed or in use. This helps streamline monitoring resources and reduce clutter, ensuring that only relevant and actively used dashboards remain operational within AWS environments.
inputs
outputs
import boto3
from datetime import datetime, timedelta
creds = _get_creds(cred_label)['creds']
access_key = creds['username']
secret_key = creds['password']
def get_cloudwatch_client(region_name):
"""Create a CloudWatch client for the specified region."""
return boto3.client('cloudwatch', aws_access_key_id=access_key, aws_secret_access_key=secret_key, region_name=region_name)
def delete_dashboard(dashboard, cloudwatch_client):
"""Delete a specific CloudWatch dashboard."""
try:
cloudwatch_client.delete_dashboards(DashboardNames=[dashboard['DashboardName']])
return True
except Exception as e:
print(f"Error deleting dashboard {dashboard['DashboardName']} in {dashboard['Region']}: {e}")
return False
def process_and_delete_dashboards(unused_dashboards):
"""Process and delete unused dashboards while displaying relevant details."""
print(f"{'Dashboard Name':<30} {'Region':<15} {'Days Old':<10} {'ARN':<60} {'Deleted':<10}")
for dashboard in unused_dashboards:
cloudwatch_client = get_cloudwatch_client(dashboard['Region'])
deletion_success = delete_dashboard(dashboard, cloudwatch_client)
deletion_status = 'Success' if deletion_success else 'Failed'
print(f"{dashboard['DashboardName']:<30} {dashboard['Region']:<15} {dashboard['DaysOld']:<10} {dashboard['DashboardArn']:<60} {deletion_status:<10}")
# Example usage
process_and_delete_dashboards(unused_dashboards)
copied