Zappa/AWS-电子邮件不会发送,只会超时 [英] Zappa/AWS - Emails won't send and just timeout

查看:39
本文介绍了Zappa/AWS-电子邮件不会发送,只会超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我已经为我的事务电子邮件提供商(邮戳)尝试了普通的Django SMTP和一些基于API的不同Django库.

Currently, I have tried both plain-old Django SMTP and a few different api-based Django libraries for my transactional email provider (Postmark).

运行开发服务器时,一切运行正常.通过Postmark API发送的电子邮件没有问题.

When I run my development server, everything works perfectly. Emails send via the Postmark API with no problem.

当我使用Zappa部署到AWS时,请访问我的网站,并执行应该发送电子邮件的任务(例如,重置用户密码),该页面会持续加载,直到它说端点请求超时.

When I deploy to AWS with Zappa, visit my website, and do a task that is supposed to send an email (Ex. Resetting a user's password) the page continually loads until it says Endpoint request timed out.

我尝试将AWS Lambda函数的超时设置为更长的持续时间,以防Django决定引发错误.

I have tried setting the timeout of my AWS Lambda function to a longer duration in case Django decides to throw an error.

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

Here is the error that was thrown. Just keep in mind this error only happens in production. I created a custom management command in able to retrieve this error.

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安全组,以试图解决此问题.仍然无济于事.

I have allowed all incoming and outgoing traffic to my AWS security group in an attempt to fix this. Still to no avail.

任何帮助将不胜感激.干杯.

Any help would be greatly, greatly appreciated. Cheers.

推荐答案

解释很简单:在VPC中运行的Lambda实例

The explanation is simple: A Lambda instance running in a VPC cannot access the internet:

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

When you add VPC configuration to a Lambda function, it can only access resources in that VPC. If a Lambda function needs to access both VPC resources and the public Internet, the VPC needs to have a Network Address Translation (NAT) instance inside the VPC.

如果烦人的话,解决方案也很简单:运行 NAT实例 NAT网关VPC.(另一种解决方案是将Lambda移出VPC,但这是一个更大的改变.)

The solution is also simple, if annoying: run a NAT Instance or NAT Gateway in the VPC. (An alternate solution is to take your Lambda out of the VPC, but that is a much bigger change.)

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

I am running Django / Zappa in Lambda with a NAT instance for connecting to Amazon Simple Email Service and it works fine.

这篇关于Zappa/AWS-电子邮件不会发送,只会超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆