Creating a Single Item

Create a single item in a table using CLI.

We'll cover the following

Creating a table

Let’s start by creating a table. In this table, we will use Blog as the table-name. And we will use the following primary keys:

  • Partition key: Author
  • Range key: Topic_Title. The range key is the concatenation of the topic of the post and its title. Both the keys are of string datatype.

Run the following command to create the Blog.

aws dynamodb create-table \
--table-name Blog \
--attribute-definitions \
AttributeName=Author,AttributeType=S \
AttributeName=Topic_Title,AttributeType=S \
--key-schema \
AttributeName=Author,KeyType=HASH \
AttributeName=Topic_Title,KeyType=RANGE \
--provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1

Note: Proceed with the next task in case you get a Table already exists error message in the terminal below.

We have created the table. Now, let’s populate it.

Put-Item

We can use the put-item API to create individual items. This API requires us to specify the primary key attributes. We can also add other attributes, but it is not compulsory.

Let’s see some examples of put-item calls using the AWS CLI.

aws dynamodb put-item \
	--table-name Blog \
	--item "{ \
		\"Author\" : { \"S\" : \"Seth Godin\"}, \
		\"Topic_Title\" : { \"S\" : \"Marketing, This is Marketing\" } }" \
	--return-consumed-capacity TOTAL

Note: If the primary key matches with a primary key of an item already present, the new item will replace the old item.

aws dynamodb put-item \
	--table-name Blog \
	--item "{ \
	    \"Author\" : { \"S\" : \"Simen Sinek\"}, \
	    \"Topic_Title\" : { \"S\" : \"Leadership, Leaders Eat Last\" }, \
        \"Year\" : {\"N\" : \"2008\"}, \
        \"Likes\" : {\"N\" : \"100000\"} \
 }" \
	--return-consumed-capacity TOTAL

Practice

To practice the skill of putting items, we just need to provide the environment variables and then call the put-item commands from above. We should put a few more items from our end into the table, play with them, and see what we can discover.

Get hands-on with 1000+ tech skills courses.