Pull out that DynamoDB record

May 6, 2023 programming CLI tools AWS DynamoDB

Checking a newly created DynamoDB table I wanted to use the AWS CLI tool to quickly check that a record looked right. From the “probably should have been obvious to start with” department…

The key format wasn’t actually that obvious from skimming the --help output until I’d bumped my nose on the error messages a couple of times, so here’s the idiot’s (i.e. me) guide to using the AWS cli to get a single record by partition key from a DynamoDB table:

aws \
   dynamodb get-item \
   --table-name 'fingerbob' \
   --key '{ "username" : { "S" : "dcminter"}}' \
   --output json

Actual output from that:

{
    "Item": {
        "plan": {
            "S": "Take over the world."
        },
        "username": {
            "S": "dcminter"
        },
        "since": {
            "N": "1683400121"
        },
        "name": {
            "S": "Dave Minter"
        }
    }
}

Relevant error if you’re an idiot like me and try to just use --key username=dcminter or something along those lines:

Parameter validation failed:
Invalid type for parameter Key.username, value: dcminter, type: <class 'str'>, valid types: <class 'dict'>

Hopefully when I have forgotten this syntax again I’ll remember to look here again. Or at least find it via Google!

© 2017 - 2024 Dave Minter