0
0
AWScloud~5 mins

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

Choose your learning style9 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.