Skip to content

Instantly share code, notes, and snippets.

@reedobrien
Created June 15, 2015 15:13
Show Gist options
  • Save reedobrien/cbbe6d728e802874940a to your computer and use it in GitHub Desktop.
Save reedobrien/cbbe6d728e802874940a to your computer and use it in GitHub Desktop.
Create indexes on DynamoDB table with boto3
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