0
0
AWScloud~5 mins

Tables, items, and attributes in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you want to store data in a way that is easy to find and update, you use tables. Tables hold items, which are like rows in a spreadsheet. Each item has attributes, which are like the columns or details about that item.
When you need to save user profiles with different details like name, age, and email.
When you want to keep track of orders in an online store with order ID, date, and status.
When you want to store settings for an app where each setting has a name and value.
When you want to quickly find data by a unique key like a user ID or product code.
When you want a flexible way to add or remove details without changing the whole structure.
Config File - table-definition.json
table-definition.json
{
  "TableName": "Users",
  "AttributeDefinitions": [
    {
      "AttributeName": "UserID",
      "AttributeType": "S"
    }
  ],
  "KeySchema": [
    {
      "AttributeName": "UserID",
      "KeyType": "HASH"
    }
  ],
  "ProvisionedThroughput": {
    "ReadCapacityUnits": 5,
    "WriteCapacityUnits": 5
  }
}

This file defines a DynamoDB table named Users. It has one attribute called UserID which is a string (S).

The KeySchema says UserID is the primary key to find items quickly.

ProvisionedThroughput sets how many reads and writes the table can handle per second.

Commands
This command creates the DynamoDB table using the definition in the JSON file.
Terminal
aws dynamodb create-table --cli-input-json file://table-definition.json
Expected OutputExpected
{ "TableDescription": { "TableName": "Users", "TableStatus": "CREATING", "ProvisionedThroughput": { "ReadCapacityUnits": 5, "WriteCapacityUnits": 5 }, "TableSizeBytes": 0, "ItemCount": 0, "KeySchema": [ { "AttributeName": "UserID", "KeyType": "HASH" } ], "AttributeDefinitions": [ { "AttributeName": "UserID", "AttributeType": "S" } ], "TableArn": "arn:aws:dynamodb:us-east-1:123456789012:table/Users" } }
--cli-input-json - Specifies the JSON file with the table definition
This command waits until the table is fully created and ready to use.
Terminal
aws dynamodb wait table-exists --table-name Users
Expected OutputExpected
No output (command runs silently)
--table-name - Specifies the name of the table to wait for
This command adds an item to the Users table with UserID, Name, and Age attributes.
Terminal
aws dynamodb put-item --table-name Users --item '{"UserID": {"S": "user123"}, "Name": {"S": "Alice"}, "Age": {"N": "30"}}'
Expected OutputExpected
No output (command runs silently)
--table-name - Specifies the table to add the item to
--item - Defines the item attributes and their types
This command retrieves the item with UserID 'user123' from the Users table.
Terminal
aws dynamodb get-item --table-name Users --key '{"UserID": {"S": "user123"}}'
Expected OutputExpected
{ "Item": { "UserID": {"S": "user123"}, "Name": {"S": "Alice"}, "Age": {"N": "30"} } }
--table-name - Specifies the table to get the item from
--key - Defines the primary key of the item to retrieve
Key Concept

If you remember nothing else from this pattern, remember: tables hold items, and items have attributes that store your data.

Common Mistakes
Trying to add an item without specifying the primary key attribute.
DynamoDB requires the primary key to find and store items uniquely; missing it causes an error.
Always include the primary key attribute with the correct type when adding items.
Using incorrect attribute types like putting a number as a string without the right format.
DynamoDB expects attribute types to match exactly; wrong types cause failures or wrong data storage.
Use "S" for strings and "N" for numbers with values as strings, e.g., "Age": {"N": "30"}.
Not waiting for the table to be active before adding items.
If the table is still creating, adding items will fail because the table is not ready.
Use the wait command to ensure the table is active before inserting data.
Summary
Create a DynamoDB table by defining its name, primary key, and capacity in a JSON file.
Use the AWS CLI to create the table and wait until it is ready.
Add items with attributes to the table using the put-item command.
Retrieve items by their primary key using the get-item command.