Sign in
agent:

List All AWS CloudWatch Dashboards

There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.

This task involves retrieving and displaying a comprehensive overview of all AWS CloudWatch dashboards available across different AWS regions. This includes details such as the dashboard names, the regions they are hosted in, and the last modified dates. The goal is to provide visibility into all existing dashboards to manage them effectively or audit their usage.

import boto3 from datetime import datetime, timedelta # AWS credentials creds = _get_creds(cred_label)['creds'] access_key = creds['username'] secret_key = creds['password'] def get_regions_for_service(): """Retrieve all regions where a specific AWS service is available.""" ec2 = boto3.client('ec2', aws_access_key_id=access_key, aws_secret_access_key=secret_key, region_name="us-east-1") regions = [region['RegionName'] for region in ec2.describe_regions()['Regions']] return regions def get_cloudwatch_client(region_name=None): """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 list_cloudwatch_dashboards(region_name): """List all CloudWatch dashboards in a specific region.""" cloudwatch_client = get_cloudwatch_client(region_name) dashboards = [] paginator = cloudwatch_client.get_paginator('list_dashboards') try: for page in paginator.paginate(): for dashboard in page['DashboardEntries']: dashboard['Region'] = region_name # Add region information to each dashboard # Convert LastModified datetime to string if present if 'LastModified' in dashboard: dashboard['LastModified'] = dashboard['LastModified'].strftime('%Y-%m-%d %H:%M:%S') dashboards.append(dashboard) except Exception as e: print(f"An error occurred in {region_name}: {e}") return [] return dashboards def display_dashboards(dashboards): """Display all CloudWatch dashboards in a formatted table.""" # Initialize table with the desired structure and headers table = context.newtable() table.title = "AWS CloudWatch Dashboard Details" table.num_cols = 3 # Number of columns according to headers table.num_rows = 1 # Starts with one row for headers table.has_header_row = True headers = ["Dashboard Name", "Region", "Last Modified"] # Set headers in the first row for col_num, header in enumerate(headers): table.setval(0, col_num, header) # Populate the table with dashboard data for row_num, dashboard in enumerate(dashboards, start=1): # Starting from the second row table.num_rows += 1 # Add a row for each dashboard values = [ dashboard['DashboardName'], dashboard['Region'], dashboard.get('LastModified', 'N/A') # Not all responses may have 'LastModified' ] for col_num, value in enumerate(values): table.setval(row_num, col_num, value) def process_dashboards(region_name=None): if region_name: regions = [region_name] # Process only the specified region else: regions = get_regions_for_service() # Process all available regions all_dashboards = [] for region in regions: dashboards = list_cloudwatch_dashboards(region) # Fetch dashboards for each region if dashboards: all_dashboards.extend(dashboards) # Extend the list with dashboards from the current region if all_dashboards: display_dashboards(all_dashboards) # Display all found dashboards in a formatted table else: print("No CloudWatch dashboards found.") return all_dashboards # Return the list of all dashboards for use in downstream tasks # Example usage #region_name = None # Set to None for all regions, or specify like 'us-east-1' all_dashboards = process_dashboards(region_name) #print(all_dashboards)
copied