====== Dynamo DB CLI examples ====== [[aws:dynamodb-local|Local install of AWS DynamoDB]] ===== List Tables:- ===== $ aws dynamodb --profile nonprod_admin list-tables { "TableNames": [ "lifecycle-exceptions", "github_sync" ] } $ aws dynamodb list-tables --profile nonprod_admin | jq .[][] | grep -i 'dev\|test\|nonprod' "dev-access" "dev-accounts" "dev-historic" "dev-accounts-data" "ami-builder-dev-account-contacts-data" "ami-builder-dev-pager-duty-service-data" "sharedservices-statefiles-nonprod" ===== 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 before 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: , valid types: So the key type needs to be specified, this can be found from the schema of the database. In my case requestId is the PartitionKey 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 = 'BrownBag-AccessRequests' ''' test event { "awsAccountId": "12348765", "actionType": "NewRequest", "status": "Approved" } ''' 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['status'] ) 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, account, actiontype, status): # cannot use just 'type' as it is a reserved word. client = boto3.client('dynamodb') response = client.put_item( TableName = DDB_TABLE_NAME, Item = { 'year-month':{ 'S': yearmonth, }, 'day-time':{ 'S': daytime, }, 'Account': { 'S': account, }, 'Type': { 'S': actiontype, }, 'Status': { 'S': status, }, } ) return response ===== Scan Table ===== #!/usr/bin/env python import boto3 from datetime import date, time, datetime dynamodb = boto3.resource('dynamodb', region_name='eu-west-1') table = dynamodb.Table('aws-requests-prod-access') #filter = print ("dynamodb is", dynamodb) data = table.scan( FilterExpression='createdTimestamp > :time', ExpressionAttributeValues={":time":{'S':'1571147906.257259'}} #AttributeError: 'str' object has no attribute 'update' ) print (data) #response = table.scan( # FilterExpression=Attr("Date").eq(date) and Attr("Shift").eq(shift) # ) #fe = Key('year').between(1950, 1959) #pe = "#yr, title, info.rating" # Expression Attribute Names for Projection Expression only. #ean = { "#yr": "year", } #esk = None #response = table.scan( # FilterExpression=fe, # ProjectionExpression=pe, # ExpressionAttributeNames=ean # ) #aws dynamodb scan --table-name aws-requests-prod-access --filter-expression "createdTimestamp > :time" --expression-attribute-values '{":time":{"S":"1571147906.257259"}}'