VanHau
VanHau

Reputation: 131

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

Upvotes: 11

Views: 25983

Answers (8)

Roger Montilla
Roger Montilla

Reputation: 1

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.

Upvotes: 0

Zerodf
Zerodf

Reputation: 2298

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

Upvotes: 2

Jan Alamis
Jan Alamis

Reputation: 1

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

Upvotes: 0

jarmod
jarmod

Reputation: 78663

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

Upvotes: 12

fig
fig

Reputation: 813

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)

Upvotes: 0

sabya sachi
sabya sachi

Reputation: 31

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

Upvotes: 0

Jnana Palai
Jnana Palai

Reputation: 11

in place of " put \"

use like this

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

Upvotes: 1

Alekcei Nosach
Alekcei Nosach

Reputation: 87

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.

Upvotes: 6

Related Questions