1

I am working on a Cloud Computing class project using AWS Command Line Interface to insert a table in DynamoDB. The step I'm stuck on is adding the other non-key attributes to the table. I read through the Help section about adding the attributes and came up with the following:

aws dynamodb put-item \
--table-name Sensors \
--item '{"SensorDescription": {"S": "A"}, \
"ImageFile": {"S": "file1"}, \
"SampleRate": {"N": "100"}, \
"Locations": {"L": "Aberdeen MD, Warren MI, Orlando FL"} \
--return-consumed-capacity TOTAL

I've tried batch-write-item and re-formatting examples I found through AWS Help but I am still having issues. I've been using Cloud9 for a while but DynamoDB is very new to me. Any help is greatly appreciated. The table Sensors is already created and has the following info:

vocstartsoft:~/environment $ aws dynamodb describe-table --table-name Sensors
{
    "Table": {
        "AttributeDefinitions": [
            {
                "AttributeName": "Sensor",
                "AttributeType": "S"
            }
        ],
        "TableName": "Sensors",
        "KeySchema": [
            {
                "AttributeName": "Sensor",
                "KeyType": "HASH"
            }
        ],
        "TableStatus": "ACTIVE",
        "CreationDateTime": 1607909069.531,
        "ProvisionedThroughput": {
            "NumberOfDecreasesToday": 0,
            "ReadCapacityUnits": 5,
            "WriteCapacityUnits": 5
        },
        "TableSizeBytes": 0,
        "ItemCount": 0,
        "TableArn": "arn:aws:dynamodb:us-east-1:466773264882:table/Sensors",
        "TableId": "5ddc11bc-8167-4bfa-bbcc-96199954ba39"
    }
}
2
  • If you have an error to post as well, that could be helpful. However I think you are just missing an ending bracket and single quote on your --item parameter. I think the error will likely be some kind of unexpected end of json. Commented Dec 14, 2020 at 21:51
  • I fixed the missing bracket and single quote but I'm getting this error: Error parsing parameter '--item': Invalid JSON: Expecting property name enclosed in double quotes: line 1 column 31 (char 30). I initially thought it was saying each attribute needs more parentheses but it did not work when I added them. Commented Dec 14, 2020 at 21:54

1 Answer 1

1

Please, first, be aware that you always need to include the primary key attributes in the put item operation.

On the other hand, the list Locations is not correctly defined.

Also, be aware that you missed }' at the end of the item values.

Please, try something like the following:

    aws dynamodb put-item \
    --table-name Sensors \
    --item '{"Sensor": {"S", "Sensor 1"},
    "SensorDescription": {"S": "A"},
    "ImageFile": {"S": "file1"},
    "SampleRate": {"N": "100"},
    "Locations": {"L": [{"S": "Aberdeen MD"}, {"S": "Warren MI"}, {"S": "Orlando FL"}]}}' \
    --return-consumed-capacity TOTAL

For simplicity, and in order to avoid problems with shell continuation characters, you can include the item information in a file, let's name it item.json:

{
  "Sensor": {"S", "Sensor 1"},
  "SensorDescription": {"S": "A"},
  "ImageFile": {"S": "file1"},
  "SampleRate": {"N": "100"},
  "Locations": {
    "L": [
      {"S": "Aberdeen MD"},
      {"S": "Warren MI"},
      {"S": "Orlando FL"}
    ]
  }
}

And apply the command as follows:

aws dynamodb put-item \
    --table-name Sensors \
    --item file://item.json \
    --return-consumed-capacity TOTAL
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks! For the assignment, I am supposed to use the CLI to create the table. I am running into another problem. I'm getting this error: Error parsing parameter '--item': Invalid JSON: Expecting property name enclosed in double quotes: line 1 column 31 (char 30). I initially thought it was saying each attribute needs parentheses but it did not work when I added them.
Hi @ZackLutz. You are welcome! No, I am afraid that the new problem have to do with the definition of your list Locations. Please, see my modified answer. I hope it helps.
Hmm...I changed my Locations definition and I'm still getting the same error. Error parsing parameter '--item': Invalid JSON: Expecting property name enclosed in double quotes: line 1 column 31 (char 30) JSON received: {"Sensor": {"S": "Sensor 1"}, \ "SensorDescription": {"S": "A"}, \ "ImageFile": {"S": "file1"}, \ "SampleRate": {"N": "100"}, \ "Locations": {"L": [{"S": "Aberdeen MD"}, {"S": "Warren MI"}, {"S": "Orlando FL"}]}}
Are you using the item.json file, or the inline information? Please, remove the \ characters within your JSON in the command shell, see my answer. As I told you, at least for me, the shell continuation characters within JSON are a nightmare... Please, see also: stackoverflow.com/questions/34847981/…
I was using the inline b/c the assignment called for it, but I am switching to item.json. That seems to be working. Thanks!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.