Sign in

Fetch Available AWS Redshift Snapshots

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

In AWS Redshift, snapshots are backups that capture the entire system state of a cluster at a specific point in time. Users may need to fetch or list these available snapshots for various reasons, such as monitoring, auditing, or planning a recovery operation. By fetching the list of snapshots, users can view details like snapshot creation time, source cluster, and snapshot size. Retrieving this list aids in effective snapshot management and ensures informed decision-making within the AWS environment.

import boto3 creds = _get_creds(cred_label)['creds'] access_key = creds['username'] secret_key = creds['password'] def list_redshift_snapshots(region=None): snapshot_identifiers = {} try: # Get list of all AWS regions ec2_client = boto3.client('ec2',aws_access_key_id=access_key,aws_secret_access_key=secret_key,region_name='us-east-1') regions_to_check = [region] if region else [region['RegionName'] for region in ec2_client.describe_regions()['Regions']] except Exception as e: print(f"Error listing AWS regions: {e}") regions_to_check = [region] if region else [] for region in regions_to_check: try: # Initialize the boto3 Redshift client for specified region redshift = boto3.client('redshift', aws_access_key_id=access_key,aws_secret_access_key=secret_key,region_name=region) # Fetch snapshots from Redshift in the current region response = redshift.describe_cluster_snapshots() # Add snapshot identifiers to the dictionary with region as the key for snapshot in response['Snapshots']: snapshot_identifiers.setdefault(region, []).append(snapshot['SnapshotIdentifier']) # Handle exceptions specific to Redshift operations except redshift.exceptions.ClusterSnapshotNotFoundFault: print(f"No Redshift snapshots found in region {region} for the specified criteria.") except Exception as e: print(f"An error occurred in region {region}: {e}") return snapshot_identifiers # Set region to None for all regions, or specify a valid AWS region string for a specific region # target_region = None # Fetch and display available snapshots snapshots = list_redshift_snapshots(target_region) if snapshots: print("Available Redshift Snapshots:") for region, snap_list in snapshots.items(): print(f"In region {region}:") for snap in snap_list: print(f" - {snap}") else: print("No Redshift snapshots found.") context.proceed = False
copied