Learn to create Amazon DynamoDB Table using AWS Cloud9
Objectives:
1. Learn to create a DynamoDB Table through AWS Cloud9.
2. Learn to use CLI commands to populate the table using JSON files.
3. Learn to run Query on the created Table.
Step 1: Cloud9 operates in some specific regions for now. So we have to switch to one of those operative Regions.
In AWS Console, top blue bar, from Region drop down, select US East (Ohio) us-east-2.
Step 2: Go to Cloud9 Service. Click on Create Environment.
Provide the following configuration:
Environment Name: MyCloud9
Description: Cloud9 IDE for CloudPlusPlus Tutorial
Go to Next Step. Confirm the following list of default selected choices:
Environment type: Create a new EC2 instance for environment (direct access)
Instance type: t2.micro (1 GiB RAM + 1 vCPU)
Platform: Amazon Linux 2 (recommended)
Proceed to Next Step. Review and click on Create environment.
Step 3: You will have the Cloud9 IDE ready in some time. A window as below is visible.
In the bottom part of the screen, a Terminal Window is visible. If not go to the Window option in Menu Bar of Cloud9 IDE and click on New Terminal.
Type the following command to check if AWS CLI version.
aws –version
Now check if the dynamodb tables:
aws dynamodb list-tables
An empty array is visible.
Now we create a table.
aws dynamodb create-table \
--table-name Weather \
--attribute-definitions \
AttributeName=CityID,AttributeType=N AttributeName=Date,AttributeType=S \
--key-schema \
AttributeName=CityID,KeyType=HASH AttributeName=Date,KeyType=RANGE \
--provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5
We confirm this in DynamoDB console.
Step 4: Create a new Json file with name weather-item.json.
Give the following code in that file and save the file.
{
"CityID": { "N": "1" },
"Date": { "S": "2017-04-12" },
"City": { "S": "Seattle" },
"State": { "S": "WA" },
"Conditions": { "S": "Rain" },
"Temperatures": { "M": {
"HighF": { "N": "59" },
"LowF": { "N": "46" }
}
}
}
Now we run this code to add contents to the table. For this run the following command in terminal window.
aws dynamodb put-item \
--table-name Weather \
--item file://weather-item.json
Confirm the same in DyanamoDB console in Items tab of the table.
Step 5: Simiarly create a new JSON file with name
more-weather-items.json.
Give the following code in that file and save the file.
{
"Weather": [
{
"PutRequest": {
"Item": {
"CityID": { "N": "1" },
"Date": { "S": "2017-04-13" },
"City": { "S": "Seattle" },
"State": { "S": "WA" },
"Conditions": { "S": "Rain" },
"Temperatures": { "M": {
"HighF": { "N": "52" },
"LowF": { "N": "43" }
}
}
}
}
},
{
"PutRequest": {
"Item": {
"CityID": { "N": "1" },
"Date": { "S": "2017-04-14" },
"City": { "S": "Seattle" },
"State": { "S": "WA" },
"Conditions": { "S": "Rain" },
"Temperatures": { "M": {
"HighF": { "N": "49" },
"LowF": { "N": "43" }
}
}
}
}
},
{
"PutRequest": {
"Item": {
"CityID": { "N": "2" },
"Date": { "S": "2017-04-12" },
"City": { "S": "Portland" },
"State": { "S": "OR" },
"Conditions": { "S": "Thunderstorms" },
"Temperatures": { "M": {
"HighF": { "N": "59" },
"LowF": { "N": "43" }
}
}
}
}
},
{
"PutRequest": {
"Item": {
"CityID": { "N": "2" },
"Date": { "S": "2017-04-13" },
"City": { "S": "Portland" },
"State": { "S": "OR" },
"Conditions": { "S": "Rain" },
"Temperatures": { "M": {
"HighF": { "N": "51" },
"LowF": { "N": "41" }
}
}
}
}
},
{
"PutRequest": {
"Item": {
"CityID": { "N": "2" },
"Date": { "S": "2017-04-14" },
"City": { "S": "Portland" },
"State": { "S": "OR" },
"Conditions": { "S": "Rain Showers" },
"Temperatures": { "M": {
"HighF": { "N": "49" },
"LowF": { "N": "39" }
}
}
}
}
},
{
"PutRequest": {
"Item": {
"CityID": { "N": "3" },
"Date": { "S": "2017-04-12" },
"City": { "S": "Portland" },
"State": { "S": "ME" },
"Conditions": { "S": "Rain" },
"Temperatures": { "M": {
"HighF": { "N": "59" },
"LowF": { "N": "40" }
}
}
}
}
},
{
"PutRequest": {
"Item": {
"CityID": { "N": "3" },
"Date": { "S": "2017-04-13" },
"City": { "S": "Portland" },
"State": { "S": "ME" },
"Conditions": { "S": "Partly Sunny" },
"Temperatures": { "M": {
"HighF": { "N": "54" },
"LowF": { "N": "37" }
}
}
}
}
},
{
"PutRequest": {
"Item": {
"CityID": { "N": "3" },
"Date": { "S": "2017-04-14" },
"City": { "S": "Portland" },
"State": { "S": "ME" },
"Conditions": { "S": "Mostly Sunny" },
"Temperatures": { "M": {
"HighF": { "N": "53" },
"LowF": { "N": "37" }
}
}
}
}
}
]
}
Now we run this code to add contents to the table. For this run the following command in terminal window.
aws dynamodb batch-write-item \
--request-items file://more-weather-items.json
Step 6: We can now run a query in the terminal to test the table. Run the following command in terminal:
aws dynamodb query \
--table-name Weather \
--key-condition-expression "(CityID = :cityID) and (#D = :date)" \
--expression-attribute-values \
'{ ":cityID": { "N": "1" }, ":date": { "S": "2017-04-12" } }' \
--select SPECIFIC_ATTRIBUTES \
--projection-expression \
"City, #D, Conditions, Temperatures.HighF, Temperatures.LowF" \
--expression-attribute-names '{ "#D": "Date" }'
We receive output as follows:
Step 7: The table can be deleted using the following command:
aws dynamodb delete-table --table-name Weather
Initially Table Status will be set to DELETING in the terminal output. The table will be eventually deleted.
We confirm the same in DynamoDB
Step 8: If you no longer need the IDE, you may delete it.
In the top side of screen, click on the Cloud9 logo.
Further Click on Go To Your Dashboard.
In the Cloud9 Dashboard, select your IDE and click on Delete. Type “Delete” in the next screen to confirm deletion.
Note: Switch back to your usual operating Region.
Was this document helpful? How can we make this document better. Please provide your insights. You can download PDF version for reference.
For AWS certification / AWS trainings needs contact us.
Easy to understand the concept of DynamoDB using AWS Cloud9.
Easy to understand
well defined
Clearly explained and Easy to understand
helpful blog