I’m trying to list all the tables in the dynamodb and if the table name == “specific_table_name” then I have to update an attribute to true, other tables’ attribute should be updated to false.
I’m able to update the attribute if I specify tablename directly, but if use a variable name(required for tables other than specific table), it fails.
Below is the lambda function:
import boto3
def lambda_handler(event, context):
# Initialize the DynamoDB client
dynamodb = boto3.client(‘dynamodb’)
# Get a list of all table names
response = dynamodb.list_tables()
# String to search for
# search_string = 'rest1'
# List to store tables containing the search string
matching_tables = []
# Check each table name for the search string
for table_name in response['TableNames']:
if "rest1" in table_name:
matching_tables.append(table_name)
dynamodb = boto3.resource('dynamodb')
if table_name == "rest1_localrest3":
table = dynamodb.Table('rest1_localrest3')
response = table.update_item(
Key = {'PK':'restindividual#restaccountinfo', 'SK': 'restaccountinfo'},
UpdateExpression = "SET isDefault = :s",
ExpressionAttributeValues = {':s' : True},
)
else:
print("Aline31 table_name",table_name)
table = dynamodb.Table(table_name)
response = table.update_item(
Key = {'PK':'restindividual#restaccountinfo', 'SK': 'restaccountinfo'},
UpdateExpression = "SET isDefault = :s",
ExpressionAttributeValues = {':s' : False},
)
print("Aline35 Error: Exception ", e, " occurred while table name assigning !!!")
# Return the list of matching table names
return {
'body': json.dumps('Updated successfully')
}
Error at: table = dynamodb.Table(table_name)
if replace table_name by specific tablename, it works as it is if condition, but to work on other table I have to use variable table_name
Whats wrong with table = dynamodb.Table(table_name)