1

I'm trying run this code adapted from this answer.

from datetime import datetime
from pytz import timezone
import pytz

def lambda_handler(event, context):

    date_format='%m/%d/%Y %H:%M:%S %Z'
    date = datetime.now(tz=pytz.utc)
    print('Current date & time is:', date.strftime(date_format))

    date = date.astimezone(timezone('US/Pacific'))

    print('Local date & time is  :', date.strftime(date_format))

I am running on AWS Lambda. I am getting this error:

Response:
{
  "errorMessage": "Unable to import module 'lambda_function': No module named 'pytz'",
  "errorType": "Runtime.ImportModuleError"
}

How can I fix this, using only imports available in AWS Lambda? I want to keep the code very minimalistic so that it can be copy-pasted directly into the AWS Console, without having to add instructions on how to setup packages etc.

Alex R
  • 11,364
  • 15
  • 100
  • 180
  • This answer to another question may be helpful using dateutil.tz: https://stackoverflow.com/a/51231362/11234542 – r6ch3l Sep 17 '19 at 20:37

1 Answers1

5

If you don't have access to pytz in AWS Lambda, you can use python-dateutil.
In that case you can do:

import datetime
import dateutil

date_format='%m/%d/%Y %H:%M:%S %Z'

IST = dateutil.tz.gettz('Asia/Kolkata')
print('Current date & time is : ',datetime.datetime.now(tz=IST).strftime(date_format))

or

eastern = dateutil.tz.gettz('US/Eastern')
print('Current date & time is : ',datetime.datetime.now(tz=eastern).strftime(date_format))

Here's the preview: preview

MasterAler
  • 1,614
  • 3
  • 23
  • 35