I am testing the aws-lambda function locally with my own input and the function runs perfectly, but when I attach the docker image to the function it quits with this import error. I have seen other questions about not finding the handler function, but I am successfully finding that.
Docker File:
FROM public.ecr.aws/lambda/python:3.8.2021.09.13.11
ADD lambda_function.py .
ADD getEmail.py .
ADD site-packages .
CMD ["lambda_function.handler"]
Error Json:
{
"errorMessage": "Unable to import module
'lambda_function': cannot import name 'etree' from
'lxml' (/var/task/lxml/__init__.py)"
"errorType": "Runtime.ImportModuleError",
"stackTrace": []
}
lambda_function.py:
from logging import currentframe
import smtplib
import email.utils
import json
from datetime import datetime
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import time
import getEmail
from lxml.html.builder import S
name = ''
time = datetime.now()
def handler(event, context):
global name
name = event['id']
return event ['id']
def emailInfo(id, currentTime):
SENDER =
SENDERNAME =
# Replace recipient@example.com with a "To" address. If your account
# is still in the sandbox, this address must be verified.
info = getEmail.get_email(id)
print(info)
RECIPIENT = info['Email']
# Replace smtp_username with your Amazon SES SMTP user name.
USERNAME_SMTP =
# Replace smtp_password with your Amazon SES SMTP password.
PASSWORD_SMTP =
# (Optional) the name of a configuration set to use for this message.
# If you comment out this line, you also need to remove or comment out
# the "X-SES-CONFIGURATION-SET:" header below.
#CONFIGURATION_SET = "ConfigSet"
# If you're using Amazon SES in an AWS Region other than US West (Oregon),
# replace email-smtp.us-west-2.amazonaws.com with the Amazon SES SMTP
# endpoint in the appropriate region.
HOST = "email-smtp.us-east-1.amazonaws.com"
PORT = 587
# The subject line of the email.
SUBJECT = ''
# The email body for recipients with non-HTML email clients.
BODY_TEXT = """""
""""".format(id, currentTime)
# The HTML body of the email.
BODY_HTML = """<html>
<head></head>
<body>
</p>
</body>
</html>
""".format(id, currentTime)
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = SUBJECT
msg['From'] = email.utils.formataddr((SENDERNAME, SENDER))
msg['To'] = RECIPIENT
# Comment or delete the next line if you are not using a configuration set
#msg.add_header('X-SES-CONFIGURATION-SET',CONFIGURATION_SET)
# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(BODY_TEXT, 'plain')
part2 = MIMEText(BODY_HTML, 'html')
# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)
# Try to send the message.
try:
server = smtplib.SMTP(HOST, PORT)
server.ehlo()
server.starttls()
#stmplib docs recommend calling ehlo() before & after starttls()
server.ehlo()
server.login(USERNAME_SMTP, PASSWORD_SMTP)
server.sendmail(SENDER, RECIPIENT, msg.as_string())
server.close()
# Display an error message if something goes wrong.
except Exception as e:
print ("Error: ", e)
else:
print ("Email sent!")
emailInfo(name ,time)