Import pytz into AWS lambda function
Asked Answered
P

7

11

I'm writing a lambda function that works with datetimes and trying to import pytz so I can have timezone be accounted for when comparing.

import boto3
import pytz
from datetime import timedelta, date, datetime
from boto3.dynamodb.conditions import Key, Attr

causes this to display

{errorMessage=Unable to import module 'lambda_function'}

but when I remove import pytz the function fires (it just doesn't work properly without timezone info)

Pullman answered 20/1, 2016 at 17:26 Comment(0)
P
14

You need to install the pytz package so it's available for your lambda. The way you do this is having pip install it into the directory you are going to zip and upload to AWS (i.e. peered with the file containing your lambda function).

pip install -t path/to/your/lambda pytz

Then when you zip it up and upload it, it will be available.

Editing to add that I created a tool to do a lot of this for you - you can find it here: https://github.com/jimjkelly/lambda-deploy

Presentable answered 20/1, 2016 at 17:36 Comment(2)
I was hoping to avoid this, but I have a feeling you're probably correct. I'm going to try and find another way to accomplish my end goal, but marking as correct answer for now. much thanks.Pullman
I've actually been working on a tool to kind of do this automatically (as well as handle pushing it up to AWS for you), but I haven't pushed it up to github just yet. The big pain comes when you are working with libraries with compiled bits. :/Presentable
O
29

If you don't have access to pytz in your environment, maybe you have access to python-dateutil. In that case you can do:

import datetime
import dateutil.tz

eastern = dateutil.tz.gettz('US/Eastern')
datetime.datetime.now(tz=eastern)

REF. How to get current time in Pacific Timezone when import pytz fails?

Oller answered 24/10, 2018 at 17:23 Comment(1)
Perfect answer. Happy to use this functionality without having external library.Eisenach
P
14

You need to install the pytz package so it's available for your lambda. The way you do this is having pip install it into the directory you are going to zip and upload to AWS (i.e. peered with the file containing your lambda function).

pip install -t path/to/your/lambda pytz

Then when you zip it up and upload it, it will be available.

Editing to add that I created a tool to do a lot of this for you - you can find it here: https://github.com/jimjkelly/lambda-deploy

Presentable answered 20/1, 2016 at 17:36 Comment(2)
I was hoping to avoid this, but I have a feeling you're probably correct. I'm going to try and find another way to accomplish my end goal, but marking as correct answer for now. much thanks.Pullman
I've actually been working on a tool to kind of do this automatically (as well as handle pushing it up to AWS for you), but I haven't pushed it up to github just yet. The big pain comes when you are working with libraries with compiled bits. :/Presentable
M
3

To follow up on @cheframzi's answer to "Package a pytz zip file in the format python/pytz/..." as a Lambda Layer, here is one way to do that.

mkdir python
pip3 install -t python pytz=='2019.2'
zip -r pytz.zip python
rm -rf python

And then you can use aws lambda publish-layer-version --layer-name <layer_name> --zip-file fileb://./pytz.zip to deploy a new version of the layer.

As long as the library is installed at the python/pytz level of the zip file, AWS Lambda should be able to find it. You can also put it inside python/lib/python3.8/site-packages\pytz though for your specific python runtime version per here: https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html

Marseilles answered 5/1, 2021 at 19:31 Comment(0)
M
2

I ran into this issue today. The way I solved is

  • Package a pytz zip file in the format python/pytz/... the library file
  • Created a Lambda Layer enter image description here
  • In my lambda used the above layer
Millennium answered 17/12, 2020 at 23:57 Comment(0)
M
1

I've spent few hours for this pytz issue. With AWS you can try using the gettz method from 'dateutil.tz'. This way you can get the desired result that you were getting with pytz. In my case I required isoformat time (in utc with (+00:00) timezone).

from datetime import datetime
from dateutil.tz import gettz
datetime.now(gettz('UTC')).isoformat() # same result as datetime.now(pytz.utc).isoformat()
Meditation answered 30/6, 2022 at 10:25 Comment(0)
I
0

You can also add public ARNs as Lambda layers, I used this https://dev.to/vumdao/create-cron-jobs-on-aws-lambda-with-cloudwatch-event-3e07

Isolecithal answered 26/7, 2022 at 22:21 Comment(0)
O
-1
import datetime as dt
import dateutil.tz

aesttime = dateutil.tz.gettz('Australia/Brisbane')
print(dt.datetime.now(tz=aesttime).strftime(format="%Y-%m-%d %H:%M:%S"))
Olimpiaolin answered 7/11, 2022 at 9:20 Comment(2)
Please read How to Answer and edit your answer to contain an explanation as to why this code would actually solve the problem at hand. Always remember that you're not only solving the problem, but are also educating the OP and any future readers of this post. Also, please include code formatting in your posts to make them more readable.Conto
Apparently most of your answers have severe formatting problems (lack of code formatting mainly) and lack an explanation. Please go through your answers and make sure they adhere to the Stack Overflow quality standards. Whilst it is great that you're willing to share your knowledge, it's hard to use when it's unreadable due to the lack of formatting, or difficult to understand due to the lack of explanation. Please edit your answers and make sure to include formatting and explanation in your future answers.Conto

© 2022 - 2025 — McMap. All rights reserved.