The number of attributes in key schema must match the number of attributes defined in attribute definitions

If you add a non-key attribute in the attribute definition during creation of a DynamoDB table you will get this error. Attribute definition is only meant to be used with primary key (partition key and hash key) and local or global secondary index. In fact if you are not using the key in primary key or index, you don’t need to define the key in the attribute definition.

For example, below table creation command will fail and throw the error “The number of attributes in key schema must match the number of attributes defined in attribute definitions”

If you remove the non-key attribute, the table will get created

Or you have to add the non-key attribute in either the primary key or index and then the table will get created