Creating DynamoDB Table in CLI - amazon-web-services

I created a DynamoDB table and was running the following command in the CLI
aws dynamodb put-item \
--table-name NBATeams \
--item '{"Team": {"S": "Team Name"},"Title": {"S": "Suns"}}' \
--region us-east-1
but I keep getting "An error occurred (ValidationException) when calling the PutItem operation: One or more parameter values were invalid: Missing the key Team Name in the item" Not sure What am I missing since my partition key is City.
aws dynamodb put-item \
--table-name NBATeams \
--item '{"Team": {"S": "Team Name"},"Title": {"S": "Suns"}}' \
--region us-east-1

i believe problem is with your partition key, please double checked the name of your "PARTTION_KEY " make sure all attribute names, values, and keys are case sensitive.

When doing a put-item you must provide the exact keys for the table, which includes the partition key and sort key if one is defined.
If you still face issue, share the output of the describe-table command so we can understand your schema.
I believe your key is Team Name
aws dynamodb put-item \
--table-name NBATeams \
--item '{"Team Name": {"S": "Team Name"},"Title": {"S": "Suns"}}' \
--region us-east-1

Related

AWS CLI DynamoDB with shell variable

I am trying to put a variable in the AWS CLI DynamoDB command for add a row in a table:
aws dynamodb put-item --table-name table_name --item '{"id_proceso": {"N": "1"}, "fecha_proceso": {"S": "20210707170486"}, "status": {"S": "OK"}, "fecha_termino": {"S": "fecha_termino_var" } }' --region us-east-1
Where fecha_termino_var it should be a variable. example: fecha_termino_var=(date +'%Y/%m/%d %H:%M:%S')
but I have tried various ways but I have not managed to do it, is there any way to enter a variable in a aws cli command line?
The goal it is to add a row, which will contain a variable of the date in real time.
I've tried various ways but it just adds the variable type, in this case "date", or just the variable name in text plain "fecha_termino_var".
I'm working in a shell (.sh) to do this.
Failed image adds:
Your screenshot shows that you are missng " infront of fecha_termino. So it should be:
, "fecha_termino":
not:
, fecha_termino":
Update:
The full correct command is:
aws dynamodb put-item --table-name mcp_control_proceso --item '{"id_proceso": {"N": "1"}, "fecha_proceso": {"S": "20210707170487"}, "status": {"S": "OK"}, "fecha_termino": {"S": "'"${fecha_termino_var}"'"} }' --region us-east-1

unable to create DynamoDB table from aws CLI

I am very new to aws. I was trying to create a database table by running following command:
``
aws dynamodb create-table --table-name pizza-order --attribute-definitions AttributeName=orderId, AttributeType=S --key-schema AttributeName=orderId,keyType=HASH --provisioned-throughput ReadCapacityUnit=1,WriteCapacityUnit=1 --region us-east-2 --query TableDescription.TableArn --output text
``
But I am getting a error like this:
Error parsing parameter '--attribute-definitions': Expected: '', received: '' for input:
AttributeName=orderId,
The command works with following modifications:
Remove space after orderId,
KeyType correctly capitalized
ReadCapacityUnits and WriteCapacityUnits correctly pluralized.
Here is the working command for your reference:
aws dynamodb create-table --table-name pizza-order --attribute-definitions AttributeName=orderId,AttributeType=S --key-schema AttributeName=orderId,KeyType=HASH --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 --region us-east-2 --query TableDescription.TableArn --output text
aws dynamodb create-table --table-name pizza-order --attribute-definitions AttributeName=orderId,AttributeType=S --key-schema AttributeName=orderId,KeyType=HASH --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 --region us-east-2 --query TableDescription.TableArn --output text
Docs for reference
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/getting-started-step-1.html
To verify that DynamoDB has finished creating the Music table, use the describe-table command.
aws dynamodb describe-table --table-name pizza-order | grep TableStatus
This command returns the following result. When DynamoDB finishes creating the table, the value of the TableStatus field is set to ACTIVE.
"TableStatus": "ACTIVE",
note:- whenever you are stuck or want to know the details of the command its a good practice to check aws cli doc for particular API for create table

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 ^.
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
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.
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
in place of " put \"
use like this
aws dynamodb put-item \
--table-name UsersTable \
--item '{
\"Username\": {\"S\": \"alexdebrie\"}
}' \
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"}
}" \
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)
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
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.

AWS DynamoDB CLI command not working

I am unable to execute the put-item command via the command line - sample below.
C:\Users\Ishtiaque>aws dynamodb put-item --table-name weatherstationdata --item '{"stationid":{"S":"000001"},"dateandtime" : {"S": "2015/12/25 00:00"},"temperature": {"N" : "0"}}' --profile LOAdmin
Unknown options: {S:, 2015/12/25 00:00},temperature:, {N, :, 0}}', :
I have also tried the "\" switch e.g.:
C:\Users\Ishtiaque>aws dynamodb put-item --table-name weatherstationdata --item '{\"\stationid":{\"\S":\"\000001"},"dateandtime" : {\"\S": \"\2015/12/25 00:00"},\"\temperature": {\"\N" : \"\0"}}' --profile LOAdmin
But still unsuccessful.
I think it is something related to the "" sign but not sure how else resolve.
I can crate item from the console and when use the 'get-item' command, I face a similar error.
Your expression looks ok but if you have complex JSon you can create a new JSon file file.json
{
"dateandtime": {
"S": "2015/12/25 00:00"
},
"stationid": {
"S": "000001"
},
"temperature": {
"N": "0"
}
}
and then pass it as expression to your command
aws dynamodb put-item --table-name weatherstationdata \
--item file://item.json --profile LOAdmin
You should make sure not to include space after the key in Json so something like this would work in CLI
aws dynamodb put-item --table-name weatherstationdata --item '{"stationid": {"S": "000001"},"dateandtime": {"S": "2015/12/25 00:00"},"temperature": {"N": "0"}}' --profile LOAdmin

AWS CLI DynamoDB command

I am writing a shell script file for doing some operation on dynamoDB using AWS CLI. I am trying to update an attribute in an item in a dynamodb table if the attribute already exists.
However, I am not comfortable with the syntax of the update-item command. I want to update an attribute named 'conf' with some value. However, I am not able to figure out the syntax for SET in thie command. This is what I have got till now :
aws dynamodb update-item --table-name MY_TABLE_NAME --key '{"AccountId": {"S": accountId}}'
I know the above has to followed by the SET option.
Any help would be appreciated.
I think it would look something like this:
aws dynamodb update-item --table-name MY_TABLE_NAME --key file://update-key.json --update-expression "SET conf = :newconf" --expression-attribute-values file://update-attr-values.json --condition-expression "attribute_exists(conf)" --return-values ALL_NEW
update-key.json
{
"AccountId": {
"S": "account123"
}
}
update-attr-values.json
{
":newconf": {
"S": "new conf value"
}
}
It is possible to do this without files but the answer is hidden in the 700+ pages developer guide of dynamodb:
aws dynamodb update-item \
--region MY_REGION \
--table-name MY_TABLE_NAME \
--key='{"AccountId": {"S": accountId}}' \
--update-expression 'SET conf=:newconf' \
--expression-attribute-values '{":newconf":{"S":"new conf value"}}'
The developer guide of Dynamo DB can be found here: Dynamo DB Developer guide
On page 206 by Atomic Counters, there is an example of how to use --expression-attribute-values without a file