Sign in
agent:

List All AWS DynamoDB tables

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

This task retrieves names of all AWS DynamoDB tables in a specified AWS account and region, useful for inventory checks, administrative tasks, or automating operations across multiple tables.

import boto3 from botocore.exceptions import BotoCoreError, ClientError # Specify the region name here; set it to None to list tables from all regions # Example: 'ap-south-1' for a specific region, or None for all regions #region_name = 'us-east-1' creds = _get_creds(cred_label)['creds'] access_key = creds['username'] secret_key = creds['password'] def list_tables_in_region(dynamodb_client, region): """List tables in a given region using the provided DynamoDB client.""" paginator = dynamodb_client.get_paginator('list_tables') for page in paginator.paginate(): if page['TableNames']: for table in page['TableNames']: print(f"Region: {region}, Table: {table}") else: print(f"Region: {region}, No DynamoDB tables found.") try: if region_name: print(f"Listing DynamoDB tables from the region: {region_name}...") dynamodb_client = boto3.client( 'dynamodb', aws_access_key_id=access_key, aws_secret_access_key=secret_key, region_name=region_name ) list_tables_in_region(dynamodb_client, region_name) else: print("Listing DynamoDB tables from all regions...") ec2_client = 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_client.describe_regions()['Regions']] for region in regions: dynamodb_client = boto3.client( 'dynamodb', aws_access_key_id=access_key, aws_secret_access_key=secret_key, region_name=region_name ) list_tables_in_region(dynamodb_client, region) except ClientError as e: print(f"A client error occurred: {e.response['Error']['Message']}. Please check your AWS configuration and permissions.") except BotoCoreError as e: print(f"A BotoCore error occurred: {e}. This might be a network issue or a problem with your AWS SDK setup.") except Exception as e: print(f"An unexpected error occurred: {e}. This might be due to unexpected issues in the script or the environment.") context.proceed = False
copied