patJR
patJR

Reputation: 79

Current date/time issue with AWS-Lambda function

I am using python for lambda function and it works fine. There is a small issue with date and time returned by lambda function. It shows different date/time and I think it shows UTC timezone. Time shown is 4 hours ahead of original time

Currently, I am connect to us-east-1 region and would like to get results according to my region.

So far I tried in lambda function:

import time
time.ctime() 

from datetime import datetime
datetime.now().strf("%m-%d-%Y %H:%M:%S") 

file_name = 'VDI_Health_Status_Report-%s.csv' % time.ctime()
print(file_name)

Locally, they work fine but with lambda function, I dont get the desired result. I also came across "pytz" module in python which could be the solution but not sure if lambda function would have access to it as I had to install the module locally. Any way around this or any suggestions? Thanks

enter image description here

Upvotes: 7

Views: 34733

Answers (1)

ViennaMike
ViennaMike

Reputation: 2337

Yes, AWS Lambda uses GMT as local time for all their servers. I just found this out myself. To accurately get local time, including daylight savings time issues, install the pytz library and use it to convert. The pytz library is not installed on AWS Lambda by default, you need to upload it as part of your zipped package.

Then see How to convert GMT time to EST time using Python for how to proceed. For example:

eastern_time = datetime.now(pytz.timezone('US/Eastern'))

See Import pytz into AWS lambda function for how to install pytz with your Lambda app.

Upvotes: 8

Related Questions