- Mais recentes
- Mais votos
- Mais comentários
I would advise not calling DescribeTable each time to check if the table exists. Why not just handle that exception in your requests, that way if you try to do a GetItem or PutItem and you get ResourceNotFoundException
you can call a function to create the table.
As an idea, how about getting a list of tables with list_tables and checking them?
def exists_table(dynamodb_client,table_name,):
return table_name in dynamodb_client.list_tables()
Also, if you are interested, having a set of means to manage infrastructure, such as SAM or CDK with code, might be a good idea. The idea is to manage DynamoDB or Lambda in a separate program.
https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html
Since don't want to use a exception for what is effectively a control flow I prefer this answer. I can quite easily build an if-else logic based on this function. My choosing this as accepted answer is very much a personal decision, based om my skill level and my particular use case. The down-votes suggest that this might be an anti-pattern, but to me using a exceptions for control flow is itself an anti-pattern.
Boto3 provides a more straightforward way to check if a DynamoDB table exists without relying on exceptions. You can use the list_tables() method of the DynamoDB client to retrieve a list of all the tables in your AWS account (or a specific region if you've configured a different region). Then, you can simply check if the table name you're interested in is present in that list.
Here's how you could modify your code to achieve this:
import boto3
# Create a DynamoDB resource
dynamodb = boto3.resource("dynamodb")
# Create a DynamoDB client
dynamodb_client = boto3.client("dynamodb")
# Specify the name of the table you want to check
table_name = "people"
# Get a list of all existing tables
existing_tables = dynamodb_client.list_tables()["TableNames"]
# Check if the table exists
if table_name in existing_tables:
print(f"Table '{table_name}' exists.")
table = dynamodb.Table(table_name)
print(table.creation_date_time)
else:
print(f"Table '{table_name}' does not exist.")
# Create the DynamoDB table.
table = dynamodb.create_table(
TableName=table_name,
KeySchema=[
{"AttributeName": "username", "KeyType": "HASH"},
{"AttributeName": "last_name", "KeyType": "RANGE"},
],
AttributeDefinitions=[
{"AttributeName": "username", "AttributeType": "S"},
{"AttributeName": "last_name", "AttributeType": "S"},
],
ProvisionedThroughput={"ReadCapacityUnits": 2, "WriteCapacityUnits": 2},
)
# Wait until the table exists.
table.wait_until_exists()
# Print out some data about the table.
print(table.item_count)
print("Continue interacting with the DynamoDB table here")
Conteúdo relevante
- AWS OFICIALAtualizada há 2 anos
- AWS OFICIALAtualizada há 7 meses
- AWS OFICIALAtualizada há 10 meses
Thanks to everyone who replied to my first post. BTW I accidentally posted this as an answer when it probably should have been a comment such as this one. . Looks like there are really two possible answers; either to handle the exception when performing GetItem or to use list_tables(). I’ll take some time to test both approaches and choose what works for me. I am still curious why there are so many so many up-votes and down-votes. Any reason one approach is preferable over the other?