AWS CLI DynamoDB with shell variable - amazon-web-services

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

Related

Creating DynamoDB Table in CLI

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

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

Substituting variable value in AWS CLI DynamoDB Query Operation

I am writing a shell script to query an attribute from dynamoDB table. I am using AWS CLI to write the script.
I want to find AccountId from MY_TABLE_NAME for ReferenceId gfsdgrhfsh. When I supply the exact value of the attribute in the AttributeValueList, the query operation succeeds and I get the correct attribute value.
aws dynamodb query --table-name MY_TABLE_NAME \
--select SPECIFIC_ATTRIBUTES --attributes-to-get "AccountId" \
--key-conditions '{"ReferenceId": {"AttributeValueList": [ {"S": "gfsdgrhfsh" } ], "ComparisonOperator": "EQ"} }' \
--limit 1 | jq '.Items[0].AccountId.S'
The above command gives me the correct account id.
However, when I am assigning the ReferenceId gfsdgrhfsh to a variable and then putting this variable in the command, I am not getting a response.
referenceId=gfsdgrhfsh
aws dynamodb query --table-name MY_TABLE_NAME \
--select SPECIFIC_ATTRIBUTES --attributes-to-get "AccountId" \
--key-conditions '{"ReferenceId": {"AttributeValueList": [ {"S": "$referenceId" } ], "ComparisonOperator": "EQ"} }' \
--limit 1 | jq '.Items[0].AccountId.S'
Can someone please advise on how to inject the value of the variable in the command. I have to perform query operation for lots of referenceIds by reading them from a file, so I need to inject the variable value in the command.
Any help would be appreciated.
Figured out, we need to pass the value enclosed within double quotes. The below command works :
aws dynamodb query --table-name MY_TABLE_NAME \
--select SPECIFIC_ATTRIBUTES --attributes-to-get "AccountId" \
--key-conditions '{"ReferenceId": {"AttributeValueList": [ {"S": '\"$referenceId\"' } ], "ComparisonOperator": "EQ"} }' \
--limit 1 | jq '.Items[0].AccountId.S'

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