简体   繁体   English

将pytz导入AWS lambda function

[英]Import pytz into AWS lambda function

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.我正在写一个 lambda function 与日期时间一起使用并尝试导入 pytz 以便在比较时可以考虑时区。

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)但是当我删除 import pytz 时,function 会触发(如果没有时区信息,它就无法正常工作)

If you don't have access to pytz in your environment, maybe you have access to python-dateutil .如果您在您的环境中无权访问pytz ,也许您可​​以访问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? 导入 pytz 失败时如何获取太平洋时区的当前时间?

You need to install the pytz package so it's available for your lambda.您需要安装 pytz 包,以便它可用于您的 lambda。 The way you do this is having pip install it into the directory you are going to zip and upload to AWS (ie peered with the file containing your lambda function).您执行此操作的方法是将 pip 安装到您要压缩并上传到 AWS 的目录中(即与包含您的 lambda 函数的文件对等)。

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编辑添加我创建了一个工具来为你做很多事情 - 你可以在这里找到它: https ://github.com/jimjkelly/lambda-deploy

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.要跟进@cheframzi 对“以 python/pytz/... 格式打包 pytz zip 文件”作为 Lambda 层的回答,这是一种方法。

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.然后您可以使用aws lambda publish-layer-version --layer-name <layer_name> --zip-file fileb://./pytz.zip来部署层的新版本。

As long as the library is installed at the python/pytz level of the zip file, AWS Lambda should be able to find it.只要库安装在 zip 文件的python/pytz级别,AWS Lambda 应该能够找到它。 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您也可以将其放在python/lib/python3.8/site-packages\pytz ,但对于您的特定 python 运行时版本,请参见此处: https ://docs.aws.amazon.com/lambda/latest/dg/configuration-layers .html

I ran into this issue today.我今天遇到了这个问题。 The way I solved is我解决的方法是

  • Package a pytz zip file in the format python/pytz/... the library file打包一个 pytz zip 文件,格式为 python/pytz/... 库文件
  • Created a Lambda Layer创建了一个 Lambda 层在此处输入图像描述
  • In my lambda used the above layer在我的 lambda 中使用了上面的层

I've spent few hours for this pytz issue.我花了几个小时来解决这个 pytz 问题。 With AWS you can try using the gettz method from 'dateutil.tz'.使用 AWS,您可以尝试使用“dateutil.tz”中的 gettz 方法。 This way you can get the desired result that you were getting with pytz.通过这种方式,您可以获得使用 pytz 获得的预期结果。 In my case I required isoformat time (in utc with (+00:00) timezone).在我的情况下,我需要 isoformat 时间(在带有 (+00:00) 时区的 utc 中)。

from datetime import datetime
from dateutil.tz import gettz
datetime.now(gettz('UTC')).isoformat() # same result as datetime.now(pytz.utc).isoformat()

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您还可以将公共 ARN 添加为 Lambda 层,我使用了这个https://dev.to/vumdao/create-cron-jobs-on-aws-lambda-with-cloudwatch-event-3e07

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM