I am writing a query to get Amazon Athena records for the past one week only. Here is what I wrote so far:
WITH events AS (
SELECT
event.eventVersion,
event.eventID,
event.eventTime,
event.eventName,
event.eventType,
event.eventSource,
event.awsRegion,
event.sourceIPAddress,
event.userAgent,
event.userIdentity.type AS userType,
event.userIdentity.arn AS userArn,
event.userIdentity.principalId as userPrincipalId,
event.userIdentity.accountId as userAccountId,
event.userIdentity.userName as userName
FROM cloudtrail.events
CROSS JOIN UNNEST (Records) AS r (event)
)
SELECT userName,sourceIPAddress,eventName,eventTime FROM events WHERE eventName='ConsoleLogin';
But I am not sure how to write it to extract records for the past 1 week only.
Best Answer
@Philᵀᴹ's answer is almost there. I just used it on my query and found the fix. I would have commented, but don't have enough points, so here's the answer.
You have to use
current_timestamp
and then convert it to iso8601 format. Like so:You can test the format you actually need by doing a test query like this:
Returns: '2018-06-05'
Returns: '2018-06-05T19:25:21.331Z', which is the same format as event.eventTime, and that works.