4
votes

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 ^.

5
Error you see is apparently a formatting error. Did you try it without using escaping characters in a single string? - A.Khan
I tried it of course, but it still showed the same error. - VanHau

5 Answers

3
votes

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

2
votes

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.

0
votes

in place of " put \"

use like this

aws dynamodb put-item \
    --table-name UsersTable \
    --item '{
      \"Username\": {\"S\": \"alexdebrie\"}
    }' \
0
votes

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"} }" \

0
votes

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)