Trying to deploy a scraper on AWS. I am importing the following structure as a layer into lambda
after zipping
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
. Here is the lambda
code.
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
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. Follow the instructions here . You can also test unzipping your lamba layer and verifying you're not including the "lambdalayer" folder.
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 .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.