shellaws-cli

Run dynamodb describe table for all tables in the account


I am trying a small shell script to describe all the dynamodb tables in my aws account.

#!/bin/bash
# Get the list of DynamoDB table names
ddbtables=$(aws dynamodb list-tables | jq '.[]' | tr -d \")
# Iterate through the list of table names
for table in $ddbtables; do
   # Get the description of the table
   aws dynamodb describe-table --table-name $table --query "Table.{TableName:TableName,SSEDescription:SSEDescription}"
done

I have verified that this is working and the output is a list of Tablenames

aws dynamodb list-tables | jq '.[]' | tr -d \"

ddbtables = [table1,table2,table3,....,table390]

I verified that this part also works fine if a valid --table-name is passed

aws dynamodb describe-table --table-name $table --query "Table.{TableName:TableName,SSEDescription:SSEDescription}"

But my shell script is failing with below error,because in the for loop, comma is also being considered as part of tablename which I dont understand. Do we need to handle a list differently in shell script. Would be grateful to any advice

An error occurred (ValidationException) when calling the DescribeTable operation: 1 validation error detected: Value 'table1,' at 'tableName' failed to satisfy constraint: Member must satisfy regular expression pattern: [a-zA-Z0-9_.-]+

Thanks


Solution

  • Your technique on the right track, but tr -d \" is not sufficient to turn json into plaintext. Now you have invalid json because there are no quotes, and invalid plaintext because there aare still [, ,, and ] characters.

    jq itself can output raw text with the -r flag, conveniently turning json input into a more commandline-friendly stream of text.

    aws dynamodb list-tables | jq -r .TableNames[]
    

    Now you can iterate over that with a for loop, or you can use xargs to turn a list of table names on standard input to executions of the describe-tables`

    aws dynamodb list-tables | jq -r .TableNames[] \
     | xargs -n 1 aws dynamodb describe-table \
        --query "Table.{TableName:TableName,SSEDescription:SSEDescription}" \
        --table-name 
    

    xargs -n 1 tells xargs to process only one input item per execution, and by default puts the item from standard input at the end of the command as the last argument. That's why --table-name is at the end of the invocation.

    You may find that having a valid json list of results may help you process the data in further steps. In this case, use the -s option to jq to "slurp" all the documents together into a json list.

    aws dynamodb list-tables | jq -r .TableNames[] \
     | xargs -n 1 aws dynamodb describe-table \
      --query "Table.{TableName:TableName,SSEDescription:SSEDescription}"  \
      --table-name \
     | jq -s