As a fun side project, I'm building a serverless Todo application on AWS. I do a lot in the terminal, but my knowledge is basic.
The command to add something into my DynamoDB table via the AWS CLI (v2.3.4) is this:
aws dynamodb put-item \ --table-name tasks \ --item \ '{"task_id": {"S": "3495353e-726f-4e0e-b290-8014c03be971"}, "user_id": {"S": "aae30f8e-aabe-4e38-918f-0f5a2223f589"}, "created_at": {"S": "2022-09-09T12:51:05Z"}, "content": {"S": "Clean car"}, "is_done": {"BOOL": false}}' \ --profile personal
Notice that for created_at
I'm manually typing in the ISO-8601 date as a string.
Now I know that on linux, in order to get the UTC datetime in the ISO-8601 format I need to run:
date -u +"%Y-%m-%dT%H:%M:%SZ"
My question is, how do I fit that into my DynamoDB put-item
command so that I automatically/dynamically get the created_at
from my linux system.
What I have tried:
I tried to simply plunk the date
command into my DynamoDB command where the created_at
value would go like this:
aws dynamodb put-item \ --table-name tasks \ --item \ '{"task_id": {"S": "3495353e-726f-4e0e-b290-8014c03be971"}, "user_id": {"S": "aae30f8e-aabe-4e38-918f-0f5a2223f589"}, "created_at": {"S": date -u +"%Y-%m-%dT%H:%M:%SZ"}, "content": {"S": "Clean car"}, "is_done": {"BOOL": false}}' \ --profile personal
But that doesn't work. The command errors out and it returns with:
Error parsing parameter '--item': Invalid JSON: Expecting value: line 1 column 138 (char 137) JSON received: {"task_id": {"S": "3495353e-726f-4e0e-b290-8014c03be971"}, "user_id": {"S": "aae30f8e-aabe-4e38-918f-0f5a2223f589"}, "created_at": {"S": date -u +"%Y-%m-%dT%H:%M:%SZ"}, "content": {"S": "Clean car"}, "is_done": {"BOOL": false}}
Update:
I just tried the $(command)
method as suggested by Marcus, and I still get the invalid JSON error.
aws dynamodb put-item \ --table-name tasks \ --item \ '{"task_id": {"S": "3495353e-726f-4e0e-b290-8014c03be971"}, "user_id": {"S": "aae30f8e-aabe-4e38-918f-0f5a2223f589"}, "created_at": {"S": "$(date -u +"%Y-%m-%dT%H:%M:%SZ")"}, "content": {"S": "Clean car"}, "is_done": {"BOOL": false}}' \ --profile personal