简体   繁体   English

无法从 Lambda 层 AWS 导入函数

[英]Cannot import a function from Lambda Layers AWS

Trying to deploy a scraper on AWS.尝试在 AWS 上部署刮刀。 I am importing the following structure as a layer into lambda after zipping压缩后,我将以下结构作为层导入lambda

lambdalayer (folder)
:- python   ( folder)
         :- covid.py (file to run)
         :- lib
               :- python3.8
                           :-site-pakcages
                                          :- All dependencies

I have tried many variations of this and tried moving the file to different folders, but lambda is not able to import covid.py .我尝试了很多变体,并尝试将文件移动到不同的文件夹,但lambda无法导入covid.py Here is the lambda code.这是lambda代码。

import boto3
import json
from covid import scrapeGlobalCase


def lambda_handler(event,context):
    print(os.environ['PYTHONPATH'])
    s3 =boto3.resource('s3')
    print("Request Covid Data..")
    cov_case = scrapeGolbalCase()
    BUCKET_NAME='seleniumbucket43'
    DATE=f"{cov_case['date']}"
    OUTPUT_NAME = f'dataKeyTest{DATE}.json'
    OUTPUT_BODY = json.dumps(cov_case)
    print("Sending to S3")
    S3.bucket(BUCKET_NAME).put_object(Key=OUTPUT_NAME,Body=OUTPUT_BODY)
    print(f'Job done at {DATE}')

Error: -错误: -

{
  "errorMessage": "Unable to import module 'lambda_function': No module named 'covid'",
  "errorType": "Runtime.ImportModuleError",
  "stackTrace": []
}

covid.py file covid.py文件

import requests,datetime
from bs4 import BeautifulSoup
def scrapeGlobalCase():
    try:
        url = "https://www.worldometers.info/coronavirus/"
        req = requests.get(url)
        bsObj = BeautifulSoup(req.text, "html.parser")
        data = bsObj.find_all("div",class_ = "maincounter-number")
        NumConfirmed = int(data[0].text.strip().replace(',', ''))
        NumDeaths = int(data[1].text.strip().replace(',', ''))
        NumRecovered = int(data[2].text.strip().replace(',', ''))
        NumActive = NumConfirmed - NumDeaths - NumRecovered
        TimeNow = datetime.datetime.now()
        return {
            'date': str(TimeNow),
            'ConfirmedCases': NumConfirmed,
            'ActiveCases': NumActive,
            'RecoveredCases': NumRecovered,
            'Deaths': NumDeaths
         }
    except Exception as e:
        print(e)

p = scrapeGlobalCase()
print(p)

Looks like the formatting on your zip file may be incorrect.看起来您的 zip 文件的格式可能不正确。 Follow the instructions here .按照此处的说明操作。 You can also test unzipping your lamba layer and verifying you're not including the "lambdalayer" folder.您还可以测试解压缩您的 Lamba 层并验证您不包括“lambdalayer”文件夹。

python/covid.py
python/requests
....

It should not include the site packages path, only the files and folders inside site packages.它不应包含站点包路径,仅包含站点包内的​​文件和文件夹。 Here's another example .这是另一个例子

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

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