AWS Lambda函数错误:无法导入模块“ lambda_function”:无法导入名称时区(导入日期时间模块)

时间:2019-07-11 20:30:15

标签: amazon-web-services aws-lambda

我写了一个非常简单的python脚本来删除由我编写的另一个lambda函数自动创建的EBS快照(全部在Lambda控制台中完成)。我正在测试它是否可以删除过去一天添加的快照。

我在测试代码时收到以下错误:

{
    "errorMessage": "Unable to import module 'lambda_function'"
}

Unable to import module 'lambda_function': cannot import name timezone

我检查了一下是否可能是与文件名有关的问题,但这不是可能的原因。似乎与“ datetime”有关。有什么办法可以解决这个问题?

下面的代码:

    import boto3
    from datetime import datetime, timezone, timedelta

    ec2=boto3.resource('ec2') #resource, higher level
    snapshots = ec2.snapshots.filter(OwnerIds=['self']) #all snapshots owned by me, returns list

    def lambda_handler(event, context):
        for i in snapshots:#for each snapshot
            starttime=i.start_time #timestamp when snapshot was initiated
            deletetime=datetime.now(tz=timezone.utc)-timedelta(days=1) #Correct time in UTC timezone - 1 days
            if deletetime>starttime:#if delete time is more than start time (more than a day)
                i.delete() #call method to delete that snapshot
                print 'Snapshot with Id = {} is deleted'.format(i.snapshot_id) #pull ID that was deleted

0 个答案:

没有答案