Dynamo DB CLI examples

List Tables:-

$ aws dynamodb --profile nonprod_admin list-tables
{
    "TableNames": [
        "lifecycle-exceptions", 
        "github_sync"
    ]
}

Scan table for all items:-

$ aws dynamodb --profile nonprod scan --table-name lifecycle-exceptions
{
    "Count": 2, 
    "Items": [
        {
            "deregister": {
                "S": "no"
            }, 
            "depermission": {
                "S": "yes"
            }, 
            "AMI_image_ID": {
                "S": "ami-0f97eef128d6caaaa"
            }
        }, 
        {
            "deregister": {
                "S": "no"
            }, 
            "depermission": {
                "S": "no"
            }, 
            "AMI_image_ID": {
                "S": "ami-0d99e8b1b672aaaaa"
            }
        }
    ], 
    "ScannedCount": 2, 
    "ConsumedCapacity": null
}

Scan Table with filter

Use a filter to return only certain items. Note (from AWS) “A single Scan request can retrieve a maximum of 1 MB of data”, I think this is after a filter has been applied.

$ aws dynamodb scan --table-name aws-requests-prod-access --filter-expression "createdTimestamp = :time" --expression-attribute-values '{":time":{"S":"1571147906.257259"}}'
1	1255
ACTIONTIMESTAMP	1571148016.286638
ACTIONTYPE	Approved
$ aws dynamodb scan --table-name aws-requests-prod-access --filter-expression "createdTimestamp > :time" --expression-attribute-values '{":time":{"S":"1571147906.257259"}}' | more
923     1255
ACTIONTIMESTAMP NULL
ACTIONTYPE      NULL
APPROVER        NULL
AWSACCOUNTID    464365807467
$ aws dynamodb scan --table-name aws-requests-prod-access --filter-expression "createdTimestamp > :time" --expression-attribute-values '{":time":{"S":"1571147906.257259"}}' | more
923     1255
ACTIONTIMESTAMP NULL
ACTIONTYPE      NULL
APPROVER        NULL
AWSACCOUNTID    464365807467
CREATEDTIMESTAMP        1572545806.294259

Query for one item:-

$ aws dynamodb get-item --profile nonprod_admin  --table-name lifecycle-exceptions --key '{"AMI_image_ID": {"S": "ami-0d99e8b1b672aaaaa"}}'
{
    "Item": {
        "deregister": {
            "S": "no"
        }, 
        "depermission": {
            "S": "no"
        }, 
        "AMI_image_ID": {
            "S": "ami-0d99e8b1b672aaaaa"
        }
    }
}

Get Item

First attempt:-

$ aws dynamodb get-item --table-name aws-requests-prod-access --key '{"requestid":"9915-01252ca5a755"}'

Parameter validation failed:
Invalid type for parameter Key.requestid, value: 9915-01252ca5a755, type: <class 'str'>, valid types: <class 'dict'>

So the key type needs to be specified, this can be found from the schema of the database. In my case requestId is the PartionKey and is set to string.

$ aws dynamodb get-item --table-name aws-requests-prod-access --key '{"requestid": {"S": "9915-01252ca5a755"} }'

An error occurred (ValidationException) when calling the GetItem operation: The provided key element does not match the schema

The key is case sensitive, so this worked:-

$ aws dynamodb get-item --table-name aws-requests-prod-access --key '{"requestId": {"S": "9915-01252ca5a755"} }'
ACTIONTIMESTAMP	1568714628.555759
ACTIONTYPE	Approved
... edited ...

Lambda Function - Boto3

import json
import boto3
from datetime import datetime
 
DDB_TABLE_NAME = 'test-dynamo-add'
 
 
def lambda_handler(event, context):
    print("event is", event)
 
    yearmonth, daytime = get_current_time()
    print("time", yearmonth, 'and', daytime )   
    insertresponse =  insertData(yearmonth,daytime, event['awsAccountId'], 
                          event['actionType'], event['createdTimestamp'], 
                          requestType)
def get_current_time():
    yearmonth = datetime.now().strftime('%Y-%m')
    daytime = datetime.now().strftime('%d-%H:%M:%S')
    return yearmonth, daytime
 
 
def insertData(yearmonth, daytime):
    client = boto3.client('dynamodb')
    response = client.put_item(
        TableName = DDB_TABLE_NAME,
        Item = {
            'year-month':{
                'S': yearmonth,
            },
            'day-time':{
                'S': daytime,
            },
        }
        )
    return response
 
 
aws/dynamodb.txt · Last modified: 2021/03/08 18:26 by andrew