Home > Software design >  how to get a list of dynamo db tables in aws that have a creationDate less than a certain value
how to get a list of dynamo db tables in aws that have a creationDate less than a certain value

Time:11-17

I wish to use an aws cli commad that will return a list of dynamodb tables in my aws account where the CreationDateTime is less than a certain value. CreationDateTime is a property that is shown with the describe-table command, but the list-tables command only returns the names of the table. Is there any way I could possibly use a query to filter the names returned by list-tables in accordance with their corresponding CreationDateTime?

CodePudding user response:

As noted, the answer is simply no, the AWS CLI can not do what you want in one query.

You need to resort to shell scripting to chain the necessary aws commands, or, if you are willing to give up the hard requirement that it must be the AWS CLI, an alternative solution (and probably faster than forking off aws over and over) is to use a 5 line python script to get the result you want. You can still run this from the command-line, but it won't be the AWS CLI specifically.
Just perform the time arithmetic that suits your specific selection criteria.

#!/usr/bin/env python3

import boto3
from datetime import datetime, timedelta, timezone

ddb = boto3.resource('dynamodb')
for t in sorted(filter(lambda table: datetime.now(timezone.utc) - table.creation_date_time < timedelta(days=90), ddb.tables.all()), key=lambda table: table.creation_date_time):
    print(f"{t.name}, {str(t.creation_date_time)}")  # tables created in the last 90 days

CodePudding user response:

No - As you noted, ListTables only lists the names of tables, and there is no request that provides additional information for each table, let alone filter on such information. You'll need to use ListTables and then DescribeTable on each of those tables. You can run all of those DescribeTable requests in parallel to make the whole thing much faster (although there is a practical snag - to do it in parallel, you'll need to have opened a bunch of connections to the server).

  • Related