AWS API Gateway - How do I get the date/timestamp/epoch in a body mapping template?

John Himmelman picture John Himmelman · Dec 17, 2016 · Viewed 7.3k times · Source

I need to include the request time in the body mapping template for an API Gateway method. Is there a date/time variable or function? I couldn't find anything in the template reference.

Example body mapping template:

Action=SendMessage&MessageBody=$util.urlEncode("{""timestamp"":""TIMESTAMP_HERE"",""body-json"":$input.json('$'),""params"":""$input.params()""}")

Answer

Abhigna Nagaraja picture Abhigna Nagaraja · Dec 19, 2016

UPDATE: API Gateway just added two new context variables http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html

$context.requestTime    The CLF-formatted request time (dd/MMM/yyyy:HH:mm:ss +-hhmm).
$context.requestTimeEpoch   The Epoch-formatted request time.

API gateway doesn't support this currently. Its been requested before on the forums - https://forums.aws.amazon.com/thread.jspa?messageID=697658&. We have this feature in our backlog, but unfortunately I can't commit to any timelines.