Search code examples
amazon-web-servicesaws-appsync

How do I get the date/requestTime/timestamp in Mapping Template


Looking at the documentation here it appears there is no way of getting the requestTime from the context variable.

Is there any other way, apart from using a lambda resolver, to get that value?

I know it is possible when using API Gateway, so surely there is a way. Am I looking at the wrong thing?

Thanks

Julien


Solution

  • (I work on the AWS AppSync team)

    You're right, we do not yet expose the request time inside the mapping template.

    May I ask what your use case is?

    This is valuable feedback, I'm going to make sure this gets seen by the team. I will update this thread as we have more information.

    UPDATE: We now support extracting the current timestamp via mapping template helper methods. Here are a few functions that can help you achieve what you need:

    $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
    

    The complete reference is available on the Resolver Mapping Template Utility Reference page.