设置 Elastic Beanstalk 实例时出现问题 - 无法连接到服务器

Issue Setting Up Elastic Beanstalk Instance - Could Not Connect to Server

我一直在尝试在 AWS 上建立一个网站,在 运行宁 eb initeb create 之后选择我的设置没有任何问题我 运行以下错误:

ERROR: [Instance: i-f8e3c005 Module: AWSEBAutoScalingGroup ConfigSet: null] Command failed on instance. Return code: 1 Output: (TRUNCATED)...conn = _connect(dsn, connection_factory=connection_factory, async=async) django.db.utils.OperationalError: could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?.

我不确定为什么它拒绝连接,但我怀疑这与我的安全设置有关。我知道这有点含糊,但有没有人 运行 以前遇到过这个问题,你是如何设法最终解决它的?

完整输出如下:

INFO: createEnvironment is starting.
INFO: Using elasticbeanstalk-us-east-1-671186898336 as Amazon S3 storage bucket for environment data.
INFO: Created security group named: sg-96efb5f2
INFO: Created load balancer named: awseb-e-v-AWSEBLoa-12ABLMPELT72Q
INFO: Created security group named: awseb-e-v6mwzkkgjw-stack-AWSEBSecurityGroup-JEYPGZP3YJJG
INFO: Created Auto Scaling launch configuration named: awseb-e-v6mwzkkgjw-stack-AWSEBAutoScalingLaunchConfiguration-17DLMC0THMKW
INFO: Created Auto Scaling group named: awseb-e-v6mwzkkgjw-stack-AWSEBAutoScalingGroup-FBCJ1LFH8U38
INFO: Waiting for EC2 instances to launch. This may take a few minutes.
INFO: Created Auto Scaling group policy named: arn:aws:autoscaling:us-east-1:671186898336:scalingPolicy:9e2df788-3cee-4f47-917f-a11179972c69:autoScalingGroupName/awseb-e-v6mwzkkgjw-stack-AWSEBAutoScalingGroup-FBCJ1LFH8U38:policyName/awseb-e-v6mwzkkgjw-stack-AWSEBAutoScalingScaleDownPolicy-P6J45PB8N1XA
INFO: Created Auto Scaling group policy named: arn:aws:autoscaling:us-east-1:671186898336:scalingPolicy:e5a18684-1f95-446d-be29-ad560db7e3e6:autoScalingGroupName/awseb-e-v6mwzkkgjw-stack-AWSEBAutoScalingGroup-FBCJ1LFH8U38:policyName/awseb-e-v6mwzkkgjw-stack-AWSEBAutoScalingScaleUpPolicy-1IV5PENRNB785
INFO: Created CloudWatch alarm named: awseb-e-v6mwzkkgjw-stack-AWSEBCloudwatchAlarmHigh-K0258EMNK9O3
INFO: Created CloudWatch alarm named: awseb-e-v6mwzkkgjw-stack-AWSEBCloudwatchAlarmLow-15Z0Z4KVYDGEC
ERROR: [Instance: i-f8e3c005 Module: AWSEBAutoScalingGroup ConfigSet: null] Command failed on instance. Return code: 1 Output: (TRUNCATED)...conn = _connect(dsn, connection_factory=connection_factory, async=async)
django.db.utils.OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?. 
EBExtension container_command 01_migrate failed. For more detail, check /var/log/eb-activity.log using console or EB CLI.
INFO: Command execution completed on all instances. Summary: [Successful: 0, Failed: 1].
INFO: Added EC2 instance 'i-f8e3c005' to Auto Scaling Group 'awseb-e-v6mwzkkgjw-stack-AWSEBAutoScalingGroup-FBCJ1LFH8U38'.

更新:来自 eb-activity.log

的代码
 execute_from_command_line(sys.argv)
  File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
  utility.execute()
  File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
  django.setup()
  File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
  apps.populate(settings.INSTALLED_APPS)
  File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
  app_config.import_models(all_models)
  File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
  self.models_module = import_module(models_module_name)
  File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
  __import__(name)
  File "/opt/python/bundle/2/app/paypaladaptive/models.py", line 126, in <module>
  class Payment(PaypalAdaptive):
  File "/opt/python/bundle/2/app/paypaladaptive/models.py", line 178, in Payment
  def process(self, receivers, preapproval=None, **kwargs):

更新:

在尝试了 Nick 的建议后,我遇到了同样的错误:

ERROR: [Instance: i-d05e9b2f Module: AWSEBAutoScalingGroup ConfigSet: null] Command failed on instance. Return code: 1 Output: (TRUNCATED)...conn = _connect(dsn, connection_factory=connection_factory, async=async)
django.db.utils.OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?. 
EBExtension container_command 01_migrate failed. For more detail, check /var/log/eb-activity.log using console or EB CLI.

您的实例似乎正在尝试在端口 5342 上与自身连接。如果您需要此功能,可以打开端口 5342 以使其正常工作。

一种方法是使用 .ebextensions。

以您的项目名称创建一个文件 .ebextensions/openport.config 并将以下内容放入其中。

Resources:
  databaseSecurityGroupIngress:
    Type: AWS::EC2::SecurityGroupIngress
    Properties:
      GroupName: {Ref : AWSEBSecurityGroup}
      IpProtocol: tcp
      ToPort: 5432
      FromPort: 5432
      CidrIp: 0.0.0.0/0

(如果需要提交文件)然后尝试 eb create

简短版本:您的 Django 应用正在尝试连接到 本地数据库实例。使用 RDS 实例.

您不能在 Beanstalk 节点本地拥有数据库实例(如 PostgreSQL)。这是因为它在自动缩放的负载平衡应用程序上没有意义,其中根据您的服务负载创建和销毁节点(因此销毁本地数据库实例)。

为了解决这个问题,您需要使用自动缩放组外部的 AWS RDS 实例,该实例是持久的。在您的设置中,使用用于创建 RDS 实例的 hostusernamepassword。请记住在分配给 RDS 实例的安全组上打开正确的端口(例如 5432 用于 PostgreSQL)。