Search code examples
amazon-dynamodbaws-clidynamodb-queries

How to get only a list of primary key values in dynamoDB using aws cli?


I want to get a list of primary key 'values' from DynamoDB, and the the primary key looks like this:

{
  "primary_key": "value1",
  "other_elements": "element1"
},
{
  "primary_key": "value2",
  "other_elements": "element2"
},
...
{
  "primary_key": "valueN",
  "other_elements": "elementN"
}

What I want to get is just a list of "primary_key" values, which will be like:

value1
value2
...
valueN

I tried the following:

aws dynamodb scan --table-name "table_name" 
--select SPECIFIC_ATTRIBUTES 
--attributes-to-get "primary_key_name"

But it prints all table descriptions, not only those values. How can I extract only those values using cli?

Would appreciate your advice.


Solution

  • It looks like --attributes-to-get is a legacy parameter (as stated here: https://docs.aws.amazon.com/cli/latest/reference/dynamodb/scan.html#options). According to that documentation, you want to use --projection-expression instead.

    As a result, your command would look something like this:

    aws dynamodb scan --table-name "table_name" 
    --select SPECIFIC_ATTRIBUTES 
    --projection-expression "primary_key_name"
    

    The output will not be formatted the way you want, though. I would suggest using grep and/or possibly awk to only print the value of the primary key. The answer to the following question will help with that: Bash: grep pattern from command output.