Bird
Raised Fist0
AWScloud~5 mins

Put, get, and query operations in AWS - Commands & Configuration

Choose your learning style10 modes available
Introduction
When you store data in the cloud, you need simple ways to save, find, and read that data. Put, get, and query operations let you add new data, retrieve specific data, and search through data easily.
When you want to save user information like names and emails in a database.
When you need to find a specific item by its ID quickly.
When you want to search for all items that match certain criteria, like all orders from a customer.
When you want to update or add new data without replacing everything.
When you want to read data without downloading the entire database.
Config File - main.tf
main.tf
provider "aws" {
  region = "us-east-1"
}

resource "aws_dynamodb_table" "example" {
  name           = "example-table"
  billing_mode   = "PAY_PER_REQUEST"
  hash_key       = "UserId"

  attribute {
    name = "UserId"
    type = "S"
  }

  attribute {
    name = "OrderId"
    type = "S"
  }

  global_secondary_index {
    name               = "OrderIndex"
    hash_key           = "OrderId"
    projection_type    = "ALL"
  }
}

This Terraform file creates an AWS DynamoDB table named example-table. It uses UserId as the main key to store and retrieve data quickly. It also defines a secondary index on OrderId to allow searching by order ID. The billing mode is set to pay per request, so you only pay for what you use.

Commands
This command adds a new item to the DynamoDB table with user ID 'user123', name 'Alice', and order ID 'order789'. It saves the data so you can get or query it later.
Terminal
aws dynamodb put-item --table-name example-table --item '{"UserId": {"S": "user123"}, "Name": {"S": "Alice"}, "OrderId": {"S": "order789"}}'
Expected OutputExpected
{}
→
--table-name - Specifies the DynamoDB table to put the item into
→
--item - Defines the data to store in JSON format
This command retrieves the item with user ID 'user123' from the table. It shows the saved data for that user.
Terminal
aws dynamodb get-item --table-name example-table --key '{"UserId": {"S": "user123"}}'
Expected OutputExpected
{ "Item": { "UserId": {"S": "user123"}, "Name": {"S": "Alice"}, "OrderId": {"S": "order789"} } }
→
--table-name - Specifies the table to get the item from
→
--key - Defines the key of the item to retrieve
This command searches the table for all items with order ID 'order789' using the secondary index. It helps find data by order ID quickly.
Terminal
aws dynamodb query --table-name example-table --index-name OrderIndex --key-condition-expression "OrderId = :orderId" --expression-attribute-values '{":orderId": {"S": "order789"}}'
Expected OutputExpected
{ "Items": [ { "UserId": {"S": "user123"}, "Name": {"S": "Alice"}, "OrderId": {"S": "order789"} } ], "Count": 1, "ScannedCount": 1 }
→
--table-name - Specifies the table to query
→
--index-name - Uses the secondary index to query by order ID
→
--key-condition-expression - Defines the condition to find matching items
→
--expression-attribute-values - Provides the value for the condition
Key Concept

If you remember nothing else from this pattern, remember: put saves data, get retrieves data by key, and query searches data by conditions.

Common Mistakes
Using get-item with a key that does not exist
The command returns no data because the key is missing in the table
Make sure the key you use in get-item matches an existing item in the table
Using query without specifying the index name when querying a secondary index
The query fails because DynamoDB does not know which index to use
Always include --index-name when querying a secondary index
Incorrect JSON format in --item or --key parameters
The command fails with a syntax error and does not run
Use proper JSON syntax with quotes and braces exactly as required
Summary
Use aws dynamodb put-item to add new data to your DynamoDB table.
Use aws dynamodb get-item to retrieve data by its primary key.
Use aws dynamodb query with an index to search data by specific attributes.

Practice

(1/5)
1. What does the put operation do in AWS DynamoDB?
easy
A. Saves or updates an item in the table
B. Retrieves a single item by its key
C. Searches items based on key conditions
D. Deletes an item from the table

Solution

  1. Step 1: Understand the purpose of put

    The put operation is used to save a new item or update an existing item in a DynamoDB table.
  2. Step 2: Compare with other operations

    get retrieves one item by key, query searches by key conditions, and delete removes an item.
  3. Final Answer:

    Saves or updates an item in the table -> Option A
  4. Quick Check:

    Put = Save or update [OK]
Hint: Put means save or update data [OK]
Common Mistakes:
  • Confusing put with get or query
  • Thinking put deletes data
  • Assuming put only adds new items
2. Which of the following is the correct syntax to get an item by its key in AWS SDK for JavaScript DynamoDB client?
easy
A. dynamoDBClient.put({ TableName: 'MyTable', Item: { id: '123' } })
B. dynamoDBClient.get({ TableName: 'MyTable', Key: { id: '123' } })
C. dynamoDBClient.query({ TableName: 'MyTable', KeyConditionExpression: 'id = :id' })
D. dynamoDBClient.getItem({ TableName: 'MyTable', Key: { id: '123' } })

Solution

  1. Step 1: Identify the correct method for getting an item

    The AWS SDK for JavaScript v3 uses get method on the DynamoDB DocumentClient to retrieve a single item by key.
  2. Step 2: Check the parameters

    The parameters include TableName and Key with the key attribute and value.
  3. Final Answer:

    dynamoDBClient.get({ TableName: 'MyTable', Key: { id: '123' } }) -> Option B
  4. Quick Check:

    Get item uses get() with TableName and Key [OK]
Hint: Get single item uses get() with Key [OK]
Common Mistakes:
  • Using getItem() instead of get()
  • Confusing query with get
  • Using put() to get data
3. Given this DynamoDB query code snippet, what will be the output if the table contains items with userId 'user1' and 'user2' only?
const params = {
  TableName: 'Users',
  KeyConditionExpression: 'userId = :uid',
  ExpressionAttributeValues: { ':uid': 'user1' }
};
const result = await dynamoDBClient.query(params);
console.log(result.Items);
medium
A. [{ userId: 'user2', name: 'Bob' }]
B. []
C. [{ userId: 'user1', name: 'Alice' }]
D. Error: Missing Key

Solution

  1. Step 1: Understand the query parameters

    The query searches for items where userId equals 'user1'.
  2. Step 2: Match items in the table

    Since the table has 'user1' and 'user2', only the item with 'user1' matches and is returned.
  3. Final Answer:

    [{ userId: 'user1', name: 'Alice' }] -> Option C
  4. Quick Check:

    Query filters by key condition, returns matching items [OK]
Hint: Query returns items matching key condition [OK]
Common Mistakes:
  • Expecting all items returned
  • Confusing query with scan
  • Thinking query returns error without full key
4. You wrote this code to get an item but it throws an error:
const params = {
  TableName: 'Products',
  Key: { productId: 101 }
};
const data = await dynamoDBClient.getItem(params);
console.log(data.Item);

What is the likely cause of the error?
medium
A. The key attribute value must be a string, not a number
B. The method should be get instead of getItem
C. Missing KeyConditionExpression in parameters
D. TableName is incorrect

Solution

  1. Step 1: Check the key attribute type

    DynamoDB keys must be typed as attribute values, usually strings or objects with type wrappers.
  2. Step 2: Identify the error cause

    Passing a plain number (101) without type wrapper causes a validation error.
  3. Final Answer:

    The key attribute value must be a string, not a number -> Option A
  4. Quick Check:

    Key values need correct type format [OK]
Hint: Key values must be strings or typed attributes [OK]
Common Mistakes:
  • Using wrong method name
  • Missing KeyConditionExpression in getItem
  • Incorrect table name
5. You want to update a user's email in DynamoDB only if the user exists. Which operation and condition should you use?
hard
A. Use query to find the user, then put to update
B. Use get to check existence, then put to update
C. Use put without any condition
D. Use put with a condition expression checking attribute_exists(userId)

Solution

  1. Step 1: Understand conditional update

    To update only if the user exists, use put with a condition expression that checks for the user's existence.
  2. Step 2: Use attribute_exists condition

    The condition attribute_exists(userId) ensures the item exists before updating, preventing overwriting non-existing items.
  3. Final Answer:

    Use put with a condition expression checking attribute_exists(userId) -> Option D
  4. Quick Check:

    Conditional put prevents overwriting missing items [OK]
Hint: Use put with condition attribute_exists() to update safely [OK]
Common Mistakes:
  • Using get then put causes race conditions
  • Using query unnecessarily for single item
  • Putting without condition overwrites blindly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard