What are attribute definitions in DynamoDB CloudFormation template?
database - What's the difference between attribute and key in dynamodb query - Stack Overflow
amazon web services - AWS DynamoDB Attribute Names containing Spaces - Stack Overflow
amazon web services - Limit on Number of Attributes in Table DynamoDB? - Stack Overflow
I am trying to figure out how to use DynamoDB with CloudFormation. When I go through the dashboard to create DynamoDB, there are no options to define attributes other than indices. However, in CloudFormation, there are AttributeDefinitions field: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-dynamodb-table.html#cfn-dynamodb-table-attributedefinitions
What happens when I define these attributes? By default, when I create the table from the web interface, I can create any attribute to any document/item. Will defining these attributes disallow adding other attributes into the table?
While both a Scan with a FilterExpression and a Query with KeyConditionExpression may return the same results, there is a big difference in the way this is done, its performance, and the cost to you. In your case, it is strongly preferrable to use a Query, not a Scan. Let me explain:
A Scan always scans the entire table. It may filter out things according to FilterExpression, but DynamoDB still needs to read all the items in table, and you will pay for these reads. Doing a scan with a very selective FilterExpression (which only returns a small subset of the table) is almost always a bad idea. It will be very slow and cost you a lot, relative to the amount of data you are trying to read.
On the other hand, Query can efficiently skip directly to the partition you asked for (the KeyConditionExpression can only specify a single partition key), and inside this partition, read only the sort-key range which you specified in KeyConditionExpression. The time it takes to do that, and the cost to you, will only be proportional to the number of items you actually read - even if those are only a small subset of the entire table.
Query can do this efficiently because of the way DynamoDB partition keys and sort keys work: The partition key is also called a "hash key" because it allows DynamoDB to find the specific partition efficiently, as in a hash table, without scanning the entire table. Then, inside one partition, items are sorted by the sort key, so to find a contiguous range of sort keys (as KeyConditionExpression allows to specify), DynamoDB does not need to scan all the items in the partition - it can find the requested range efficiently in O(logN).
Scan operation is very expensive as it requires to read the entire table. In addition, scan operation is a sure way to him DynamoDB's 1MB limit of the amount of data it can retrieve, so you will likely end up having to do multiple scans and using NextToken to continue scanning your table. It is not recommended to use scan in most cases.
Here you can find more about scan operation.
A key attribute in DynamoDB can be a subject to conditions in query while non-key attribute can't. Also, DynamoDB supports only = (equal) condition on primary key attributes.
Here you will find more on how to choose the right partition key.
There is no limit to the number of attributes but the total item size is limited to 400kb.
Items
Item Size
The maximum item size in DynamoDB is 400 KB, which includes both attribute name binary length (UTF-8 length) and attribute value lengths (again binary length). The attribute name counts towards the size limit.
For example, consider an item with two attributes: one attribute named "shirt-color" with value "R" and another attribute named "shirt-size" with value "M". The total size of that item is 23 bytes.
Attributes
Attribute Name-Value Pairs Per Item
The cumulative size of attributes per item must fit within the maximum DynamoDB item size (400 KB).
Number of Values in List, Map, or Set
There is no limit on the number of values in a List, a Map, or a Set, as long as the item containing the values fits within the 400 KB item size limit.
Docs
A Dynamodb table can have any number of attributes but while writing a particular row the number of attributes is limited as the cumulative size of an attribute name and attribute value should not exceed 400KB.
Mathematically speaking if the row you are writing has an average attribute size of 7 bytes and the corresponding value for it has a size of 6 bytes then you can have approximately 400 * 1024/13 ~ 31507 attributes for that entry.
If you make another such entry with an entirely different attribute name set then overall your table will have 63014 attributes.
Keep in mind each entry has a limit of 400KB, but the table can have any number of attributes.
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Limits.html#limits-attributes