I’m trying to test my lambda function and it’s telling me there’s a key error for ‘daily’ and I have no idea why. Everything else next to daily is fine somehow. Here’s my entire error message and the daily key error is down towards the bottom. Also, this function works when I run it locally on my computer, but after creating an image and pushing it to Lambda, I’m now getting this error.
"stackTrace": [
" File "/var/lang/lib/python3.8/imp.py", line 234, in load_module\n return load_source(name, filename, file)\n",
" File "/var/lang/lib/python3.8/imp.py", line 171, in load_source\n module = _load(spec)\n",
" File "<frozen importlib._bootstrap>", line 702, in _load\n",
" File "<frozen importlib._bootstrap>", line 671, in _load_unlocked\n",
" File "<frozen importlib._bootstrap_external>", line 843, in exec_module\n",
" File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed\n",
" File "/var/task/app.py", line 59, in <module>\n handler(None, None)\n",
" File "/var/task/app.py", line 48, in handler\n today_weather = data['daily'][0]['weather'][0]['main'].lower()\n"
]
}
I forgot to add my source code, apologies! I’m also on Python 3.9
import requests
import os
import smtplib
from datetime import datetime
lat = '33.4483771'
lon = '-112.0740373'
exclude = 'minutely,hourly,alerts'
url = (
'https://api.openweathermap.org/data/2.5/onecall?' +
'lat={lat}&lon={lon}&exclude={exclude}&appid={API_key}&units=imperial'
)
if os.path.isfile('.env'):
from dotenv import load_dotenv
load_dotenv()
def __send_email(msg: str) -> None:
gmail_user = os.getenv('EMAIL_USER')
gmail_password = os.getenv('EMAIL_PASSWORD')
mail_from = gmail_user
mail_to = gmail_user
mail_subject = f'Weather Today {datetime.today().strftime("%m/%d/%Y")}'
mail_message = f'Subject: {mail_subject}\n\n{msg}'
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(gmail_user, gmail_password)
server.sendmail(mail_from, mail_to, mail_message)
server.close()
def handler(event, context):
response = requests.get(url.format(
lat=lat,
lon=lon,
exclude=exclude,
API_key=os.getenv('WEATHER_API_KEY')
))
data = response.json()
rain_conditions = ['rain', 'thunderstorm', 'drizzle']
snow_conditions = ['snow']
today_weather = data['daily'][0]['weather'][0]['main'].lower()
if today_weather in rain_conditions:
msg = 'Pack an umbrella!'
elif today_weather in snow_conditions:
msg = 'Pack your snow boots!'
else:
msg = 'Clear skies today!'
__send_email(msg)
handler(None, None)