Created
June 15, 2015 15:13
-
-
Save reedobrien/cbbe6d728e802874940a to your computer and use it in GitHub Desktop.
Create indexes on DynamoDB table with boto3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
from boto3.dynamodb.types import STRING | |
table = boto3.resource("dynamodb", region_name="us-east-1").Table("availdev-JobStateDB-1UIJ881212TPX") | |
## Attrs for GSI | |
attrdef = [ | |
{"AttributeName": "state", "AttributeType": STRING}, | |
{"AttributeName": "userId", "AttributeType": STRING}, | |
{"AttributeName": "assetId", "AttributeType": STRING} | |
] | |
## Asset ID index definition | |
assetIdIdx = [ | |
{"Create": { | |
"IndexName": "assetId-index", | |
"KeySchema": [{ | |
"AttributeName": "assetId", | |
"KeyType": "HASH" | |
}], | |
'Projection': { | |
'ProjectionType': "ALL", | |
}, | |
'ProvisionedThroughput': { | |
'ReadCapacityUnits': 5, | |
'WriteCapacityUnits': 5 | |
} | |
} | |
}, | |
] | |
## State index definition | |
stateIdx = [ | |
{"Create": { | |
"IndexName": "state-index", | |
"KeySchema": [{ | |
"AttributeName": "state", | |
"KeyType": "HASH" | |
}], | |
'Projection': { | |
'ProjectionType': "ALL", | |
}, | |
'ProvisionedThroughput': { | |
'ReadCapacityUnits': 5, | |
'WriteCapacityUnits': 5 | |
} | |
}}, | |
] | |
## User ID index definition | |
userIdIdx =[ | |
{"Create": { | |
"IndexName": "userId-index", | |
"KeySchema": [{ | |
"AttributeName": "userId", | |
"KeyType": "HASH" | |
}], | |
'Projection': { | |
'ProjectionType': "ALL", | |
}, | |
'ProvisionedThroughput': { | |
'ReadCapacityUnits': 5, | |
'WriteCapacityUnits': 5 | |
} | |
}}, | |
] | |
# for each index | |
table.update(AttributeDefinitions=attrdef, GlobalSecondaryIndexUpdates=index) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment