how to use variable table name in aws dynamoDB, using lambda- python?

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’)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code># 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')
}
</code>
<code># 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') } </code>
# 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)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật