I am working on AppSync,I am struggling on how to save date in Dynamo Db using VTL. My actual requirement is to save the date when the API is called. Please let me know how to achieve this using Date.now() in Dynamo Db. Thanks in advance.
Asked
Active
Viewed 520 times
2
-
Replied in https://stackoverflow.com/questions/53625805/how-to-add-current-date-in-putitem-in-vtl – Rohan Deshpande Dec 06 '18 at 06:18
-
Possible duplicate of [How to add Current Date in putItem in VTL](https://stackoverflow.com/questions/53625805/how-to-add-current-date-in-putitem-in-vtl) – Rohan Deshpande Dec 06 '18 at 06:18
2 Answers
0
I usually do this using $util.time.nowEpochSeconds()
e.g :
#set($input={})
$util.qr($input.put("createdAt", $util.time.nowEpochSeconds()))
$util.qr($input.put("updatedAt", $util.time.nowEpochSeconds()))
#set($id=$util.autoId())
{
"version" : "2017-02-28",
"operation" : "PutItem",
"key" : {
"id": $util.dynamodb.toDynamoDBJson("$id")
},
"attributeValues" : $util.dynamodb.toMapValuesJson($input)
}

uchar
- 2,552
- 4
- 29
- 50
0
You have a few options, using DynamoDb for example you can add the attribute before mapping it:
$util.qr($ctx.args.input.put("createdAt", $util.time.nowISO8601()))
{
"version": "2017-02-28",
"operation": "PutItem",
"key": {
"id": $util.dynamodb.toDynamoDBJson($util.autoId()),
},
"attributeValues": $util.dynamodb.toMapValuesJson($ctx.args.input),
"condition": {
"expression": "attribute_not_exists(#id)",
"expressionNames": {
"#id": "id",
},
},
}
Format options:
$util.time.nowISO8601() : 2018-02-06T19:01:35.749Z
$util.time.nowEpochSeconds() : 1517943695
$util.time.nowEpochMilliSeconds() : 1517943695750
$util.time.nowFormatted("yyyy-MM-dd HH:mm:ssZ") : 2018-02-06 19:01:35+0000
$util.time.nowFormatted("yyyy-MM-dd HH:mm:ssZ", "+08:00") : 2018-02-07 03:01:35+0800
$util.time.nowFormatted("yyyy-MM-dd HH:mm:ssZ", "Australia/Perth") : 2018-02-07 03:01:35+0800
Source: https://docs.aws.amazon.com/appsync/latest/devguide/resolver-util-reference.html

Moises Marques
- 171
- 1
- 5