Issue
I have the following code in a lambda:
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
def lambda_handler(event, context):
msg_email = MIMEMultipart('alternative')
msg_email.attach(MIMEText("mymessage", 'plain'))
msg_email['Subject'] = subject
msg_email['From'] = "[email protected]"
msg_email['To'] = "[email protected]"
try:
print("AAAA")
with smtplib.SMTP('email-smtp.eu-west-1.amazonaws.com', 587) as smtp_server:
print("BBBB")
smtp_server.ehlo()
smtp_server.starttls()
smtp_server.ehlo()
smtp_server.login('myid', 'mypass')
smtp_server.sendmail("[email protected]", "[email protected]", msg_email.as_string())
except Exception:
print("Couldn't send message.")
raise
else:
print("Email sent!")
This code prints "AAAA", but before printing "BBBB" I get an error executing the lambda: Task timed out after 3.01 seconds. However, if I run it in my EC2 instance works fine. Why lambda cannot execute it?
EDIT: The role of the lambda has these permissions: AWSLambdaBasicExecutionRole(with some uiid after it), pinpoint-email-ers-, AmazonSESFullAccess, AmazonWorkMailFullAccess, AmazonWorkMailMessageFlowFullAccess
Solution
Lambda in a VPC does not have internet connection if you place it in a public subnet. It must be placed in a private subnet, which has route table with routes pointing to a NAT gateway. The NAT will be placed in the public subnet.
The details are described in AWS Docs:
Alternatively, just don't place your lambda in a VPC if you don't have to.
Answered By - Marcin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.