部署到 EB 的 Django 应用程序无法翻译主机名

Posted

技术标签:

【中文标题】部署到 EB 的 Django 应用程序无法翻译主机名【英文标题】:Django App deployed to EB Could not translate host name 【发布时间】:2020-12-06 22:48:14 【问题描述】:

我尝试在创建用户对象后发送电子邮件。我用芹菜来执行任务。我的应用程序被部署到 AWS Elastic Beanstalk (Linux2 Python3.7)。我为我的环境配置了一个 RDS。每当我创建一个新用户时,我的 celery worker 都会遇到如下相同的错误:

这是我在 celery-worker.log 中遇到的错误

[2020-08-17 10:51:20,667: INFO/MainProcess] Received task: account.tasks.send_user_email_when_user_created_by_admin[0c202693-43b7-44b7-a9b9-0362bc38afac]
[2020-08-17 10:51:20,675: WARNING/ForkPoolWorker-1] Sending email for new usr
[2020-08-17 10:51:20,693: ERROR/ForkPoolWorker-1] Task account.tasks.send_user_email_when_user_created_by_admin[0c202693-43b7-44b7-a9b9-0362bc38afac] raised unexpected: OperationalError('could not translate host name "aa1rm1r9klym9tk.ce9nktrqundw.us-west-2.rds.amazonaws.com" to address: Name or service not known\n')
Traceback (most recent call last):
  File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
    self.connect()
  File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/django/db/backends/base/base.py", line 194, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/django/db/backends/postgresql/base.py", line 178, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/var/app/venv/staging-LQM1lest/lib64/python3.7/site-packages/psycopg2/__init__.py", line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not translate host name "aa1rm1r9klym9tk.ce9nktrqundw.us-west-2.rds.amazonaws.com" to address: Name or service not known

这是我用来向用户发送电子邮件的代码

@receiver(post_save, sender=User)
def create_profile_handler(sender, instance, created, **kwargs):
    if created:
       profile = models.Profile(user=instance)
       profile.save()
       transaction.on_commit(tasks.send_user_email_when_user_created_by_admin.delay(instance.id))

我不知道这个主机名来自哪里,因为我在我的帐户下找不到这样的 rds。我做了nslookup aa1rm1r9klym9tk.ce9nktrqundw.us-west-2.rds.amazonaws.com 8.8.8.8,但找不到这样的域。是什么导致了这个问题?如何解决?

【问题讨论】:

【参考方案1】:

您尝试访问的数据库在该主机名下不存在。该域名没有 DNS 记录:https://www.nslookup.io/dns-records/aa1rm1r9klym9tk.ce9nktrqundw.us-west-2.rds.amazonaws.com/cloudflare/。您可能错误地配置了 Django 中的数据库设置。

【讨论】:

你是对的!我硬编码了我的配置文件,但在我重建环境后忘记更新它。

以上是关于部署到 EB 的 Django 应用程序无法翻译主机名的主要内容,如果未能解决你的问题,请参考以下文章

Elastic Beanstalk:部署 Django for mac,无法安装 EB

Django 部署到 AWS EB 显示 500 内部服务器错误

aws elasticbeanstalk:无法通过 eb cli 部署到工作环境

AWS EB CLI Django

将敏感环境变量包括到 AWS EB Django 应用程序

Elastic beanstalk - eb create 无法创建 AWSEBRDSDBSecurityGroup