agent: |
NLKB91SwBwZkDsMXyWP0Working with AWS DynamoDB
Working with AWS DynamoDB
There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.
This runbook allows users to list all available tables in the database, enabling easy management and overview. Additionally, DynamoDB provides the flexibility to access and modify individual table items, supporting a wide range of use cases from web applications to IoT systems, by efficiently handling data retrieval and updates.
inputs
outputs
- 1YlDLNOTAFYgvxf6MUm1wList All AWS DynamoDB tables
1
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.
inputsoutputsimport 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 = Falsecopied1 - 2yoHHDsF3cmAry80zyBV3Access/Modify AWS DynamoDB Table Item
2
Access/Modify AWS DynamoDB Table Item
There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.This task includes fetching (accessing) data using item keys and updating or altering (modifying) table data(optional).
inputsoutputsimport boto3 from botocore.exceptions import BotoCoreError, ClientError creds = _get_creds(cred_label)['creds'] access_key = creds['username'] secret_key = creds['password'] # Specify the AWS region, table name, and primary key for the DynamoDB item #region_name = 'us-east-1' # e.g., 'ap-south-1' #table_name = 'Music' # e.g., 'your_table_name' #primary_key = {"Artist": "Acme Band", "SongTitle": "Happy Day"} #primary_key = {'PrimaryKeyName': 'PrimaryKeyValue'} # Replace with your primary key and its value try: # Initialize DynamoDB client for the specified region dynamodb = boto3.resource('dynamodb', aws_access_key_id=access_key,aws_secret_access_key=secret_key,region_name=region_name) # Initialize the table table = dynamodb.Table(table_name) # Fetch the item response = table.get_item(Key=primary_key) item = response.get('Item') if item: print("Fetched item:", item) # Example logic for updating the item (if needed) # update_key = 'AttributeNameToUpdate' # Attribute you want to update # new_value = 'NewValue' # New value for the attribute # update_response = table.update_item( # Key=primary_key, # UpdateExpression=f"SET {update_key} = :val", # ExpressionAttributeValues={':val': new_value}, # ReturnValues="UPDATED_NEW" # ) # print("Updated item:", update_response) else: print("Item not found.") 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.")copied2