Zappa / AWS - 电子邮件不会发送,只是暂停

时间:2017-11-17 23:52:52

标签: django amazon-web-services

目前,我已经为我的交易电子邮件提供商(邮戳)尝试了普通的Django SMTP和一些不同的基于api的Django库。

当我运行我的开发服务器时,一切都运行良好。电子邮件通过邮戳API发送没有问题。

当我使用Zappa部署到AWS时,访问我的网站,并执行应该发送电子邮件的任务(例如重置用户的密码)页面会不断加载,直到它显示Endpoint request timed out

我已尝试将我的AWS Lambda函数的超时设置为更长的持续时间,以防Django决定抛出错误。

这是抛出的错误。请记住,此错误仅发生在生产中。我创建了一个自定义管理命令,能够检索此错误。

HTTPSConnectionPool(host='api.postmarkapp.com', port=443): Max retries exceeded with url: /email/batch (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f6cfbd5dd30>: Failed to establish a new connection: [Errno 110] Connection timed out',)): ConnectionError
Traceback (most recent call last):
  File "/var/task/handler.py", line 509, in lambda_handler
    return LambdaHandler.lambda_handler(event, context)
  File "/var/task/handler.py", line 240, in lambda_handler
    return handler.handler(event, context)
  File "/var/task/handler.py", line 376, in handler
    management.call_command(*event['manage'].split(' '))
  File "/var/task/django/core/management/__init__.py", line 131, in call_command
    return command.execute(*args, **defaults)
  File "/var/task/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/var/task/users/management/commands/sendemail.py", line 13, in handle
    fail_silently=False,
  File "/var/task/django/core/mail/__init__.py", line 62, in send_mail
    return mail.send()
  File "/var/task/django/core/mail/message.py", line 348, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/var/task/postmarker/django/backend.py", line 66, in send_messages
    responses = self.client.emails.send_batch(*prepared_messages, TrackOpens=self.get_option('TRACK_OPENS'))
  File "/var/task/postmarker/models/emails.py", line 332, in send_batch
    return self.EmailBatch(*emails).send(**extra)
  File "/var/task/postmarker/models/emails.py", line 247, in send
    responses = [self._manager._send_batch(*batch) for batch in chunks(emails, self.MAX_SIZE)]
  File "/var/task/postmarker/models/emails.py", line 247, in <listcomp>
    responses = [self._manager._send_batch(*batch) for batch in chunks(emails, self.MAX_SIZE)]
  File "/var/task/postmarker/models/emails.py", line 276, in _send_batch
    return self.call('POST', '/email/batch', data=emails)
  File "/var/task/postmarker/models/base.py", line 72, in call
    return self.client.call(*args, **kwargs)
  File "/var/task/postmarker/core.py", line 106, in call
    **kwargs
  File "/var/task/postmarker/core.py", line 129, in _call
    method, url, json=data, params=kwargs, headers=default_headers, timeout=self.timeout
  File "/var/task/requests/sessions.py", line 508, in request
    resp = self.send(prep, **send_kwargs)
  File "/var/task/requests/sessions.py", line 618, in send
    r = adapter.send(request, **kwargs)
  File "/var/task/requests/adapters.py", line 508, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.postmarkapp.com', port=443): Max retries exceeded with url: /email/batch (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f6cfbd5dd30>: Failed to establish a new connection: [Errno 110] Connection timed out',))

我已允许所有传入和传出流量到我的AWS安全组,以尝试解决此问题。仍无济于事。

非常感谢任何帮助。欢呼声。

1 个答案:

答案 0 :(得分:3)

解释很简单:在VPC cannot access the internet中运行的Lambda实例:

  

将VPC配置添加到Lambda函数时,它只能访问该VPC中的资源。如果Lambda函数需要访问VPC资源和公共Internet,则VPC需要在VPC内部具有网络地址转换(NAT)实例。

解决方案也很简单,如果烦人的话:在VPC中运行NAT InstanceNAT Gateway。 (另一种解决方案是将您的Lambda从VPC中取出,但这是一个更大的变化。)

我在Lambda中运行Django / Zappa,使用NAT实例连接到Amazon Simple Email Service,它运行正常。