'DynamoDB putting item using AWS CLI
I am trying the AWS CLI with local DynamoDB.
While I was working, I found some issues.
Inserting & Retrieving Items | DynamoDB, explained. shows how to create a table using JSON format. But it didn't work for me. So I had to use Basic Operations for Tables - Amazon DynamoDB. Anyways, it worked.
But what was the trouble was when putting an item.
I tried to add item to the local db like this:
aws dynamodb put-item \
--table-name UsersTable \
--item '{
"Username": {"S": "alexdebrie"}
}' \
--endpoint-url http://localhost:8000
But there was an error like this:
Unknown options: {S:, alexdebrie}, }', Username:
How can I handle this?
PS: I am using Windows so instead of \
, I used ^
.
Solution 1:[1]
Here is an one-line example that works on Windows:
aws dynamodb put-item --table-name test --item "{\"id\":{\"S\":\"mars\"},\"miles\":{\"N\":\"999\"}}"
So, in your case:
aws dynamodb put-item --table-name UsersTable --item "{\"Username\":{\"S\":\"alexdebrie\"}}" --endpoint-url http://localhost:8000
A few things to note:
- try to avoid multiple lines in your command
- include the entire item in double-quotes
- precede every double-quote within the item with a backslash e.g.
\"id\"
- if your attribute value is numeric, you must still quote its value for example
\"N\":\"999\"
I feel your pain. It's horrible.
You might want to also consider supplying parameters in a JSON file using --cli-input-json
Solution 2:[2]
Try with git bash, that solved my problem. I was using cmd from windows and i was facing the same problem with the formatting. The same command works in git bash.
Solution 3:[3]
in place of "
put \"
use like this
aws dynamodb put-item \
--table-name UsersTable \
--item '{
\"Username\": {\"S\": \"alexdebrie\"}
}' \
Solution 4:[4]
I believe the issue is specific in only on windows not on Mac or linux. But I agree with @Jnana Palai. But make sure the JSON is also enclosed in " not '
Likewise:
aws dynamodb put-item
--table-name UsersTable
--item "{
"Username": {"S": "alexdebrie"}
}" \
Solution 5:[5]
If you're on Windows 10 the best way to permanently deal with this (recurrent) problem is to install Windows Subsystem for Linux (WSL) with an Ubuntu system, and then install AWS CLI for Linux as here (AWS CLI Ubuntu). Once you've done this you can use the ~95% of AWS CLI snippets online that are from Linux systems, and all the horrible quoting/escaping nonsense just vanishes! (Also you get the huge benefit of having Linux inside your Windows system)
Solution 6:[6]
json='{"pets": {"cat": "lucy"},"fur":{"color": "brown"}}'
pets=$(printf "$json" "$pets" "$fur")
aws dynamodb put-item --table-name table-name --item "$pets"
the error you got was from the shell splitting your variable, you can wrap it in double quotes to prevent that.
user='{"Username": {"S":"alexdebrie"}}'
formattedUser=$(printf "$user" "$Username")
aws dynamodb put-item --table-name UsersTable --item "$formattedUser" --endpoint-url http://localhost:8000
Solution 7:[7]
One thing that might help Mac and Linux users, try a heredoc.
Consider the following example table:
$ aws dynamodb create-table \
--table-name MNLakes \
--key-schema AttributeName=LakeID,KeyType=HASH \
--attribute-definitions AttributeName=LakeID,AttributeType=S \
--provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1
Now we will add some data for Lake Nokomis.
$ aws dynamodb put-item --table-name MNLakes --item "{\"LakeID\": {\"S\": \"27001900\"}}"
Escaping the double quotes in the JSON is OK for simple items. However, for anything more complicated, it gets rather unpleasant. A good solution might be to try a heredoc:
$ aws dynamodb put-item --table-name MNLakes --item "$(cat << EOF
{
"LakeID":
{
"S": "36000800"
},
"Sasquatch":
{
"S": "YES"
},
"LastLakeSurvey":
{
"S": "2016"
}
}
EOF
)"
Results here:
$ alex@DESKTOP-VG47CLN:~$ aws dynamodb scan --table-name MNLakes
{
"Items": [
{
"Sasquatch": {
"S": "YES"
},
"LakeID": {
"S": "36000800"
},
"LastLakeSurvey": {
"S": "2016"
}
},
{
"LakeID": {
"S": "27001900"
}
}
],
"Count": 2,
"ScannedCount": 2,
"ConsumedCapacity": null
}
While the old cmd.exe
/Batch syntax doesn't support anything that elegant, PowerShell does
Solution 8:[8]
you can also use a json file:
awslocal dynamodb put-item --table-name your-table-name --item "$(cat dynamo-data/my-mock-data.json)"
I hope that helps.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Alekcei Nosach |
Solution 3 | flppv |
Solution 4 | sabya sachi |
Solution 5 | fig |
Solution 6 | Jan Alamis |
Solution 7 | Zerodf |
Solution 8 | Roger Montilla |