amazon-web-servicesamazon-dynamodbpynamodb

Error while using Global Secondary index in dynamodb in python


I have created a global secondary index in AWS dynamodb where In user table in which I am using the phone as GSI. I am getting an error with the message:

'Table UsersTable has no index: PHONE_INDEX'.

Below is the code snippet for the user table and phone as GSI. I am unable to figure out the reason why I am getting this error.

from pynamodb.models import Model
from pynamodb.indexes import GlobalSecondaryIndex, KeysOnlyProjection, AllProjection
from pynamodb.attributes import( UnicodeAttribute, NumberAttribute)
from passlib.hash import pbkdf2_sha256 as sha256
import auth.exceptions as exception

class PhoneIndex(GlobalSecondaryIndex):
    
    class Meta:
        index_name = 'PHONE_INDEX'
        read_capacity_units = 2
        write_capacity_units = 2
        # All attributes are projected
        projection = KeysOnlyProjection()

    # This attribute is the hash key for the index
    # Note that this attribute must also exist
    # in the model
    phone = UnicodeAttribute(hash_key=True)


class UserModel(Model):
    # Table Users
    if config.IS_OFFLINE:
        class Meta:
            table_name = config.USERS_TABLE
            index_name = 'PHONE_INDEX'
            host = "http://localhost:8000"
            region = config.REGION
            aws_access_key_id = 'my_access_key_id'
            aws_secret_access_key = 'my_secret_access_key'
            aws_session_token = 'my_session_token'
    else:
        class Meta:
            table_name = config.USERS_TABLE
            region = config.REGION
    customerid= UnicodeAttribute()
    phone = UnicodeAttribute()
    name = UnicodeAttribute()
    username = UnicodeAttribute(hash_key=True)
    password= UnicodeAttribute()
    phone_index=PhoneIndex()

Solution

  • I was getting this error as I have not added global secondary index in my cloudformation template file.