Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've been trying to set up a website on AWS and after running eb init and eb create and choosing my settings without any issues I ran into the following error:

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?.

I'm not sure why it's refusing to connect, but I suspect it has something to do with my security settings. I know this is a bit vague, but has anyone run into this problem before and how did you manage to finally solve it?

The full output is as follows:

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'.

UPDATE: Code from 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):

UPDATE:

After trying Nick's suggestion I got the same error:

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.
like image 513
114 Avatar asked May 08 '15 00:05

114


People also ask

How do I connect to the Elastic Beanstalk server?

Right-click the instance ID for the Amazon EC2 instance running in your environment's load balancer, and then select Connect from the context menu. Make a note of the instance's public DNS address on the Description tab. Connect to an instance running Linux by using the SSH client of your choice, and then type ssh -i .

Why is my EC2 instance not connecting?

The following are common reasons why EC2 Instance Connect might not work as expected: EC2 Instance Connect doesn't support the OS distribution. The EC2 Instance Connect package isn't installed on the instance. There are missing or incorrect AWS Identity and Access Management (IAM) policies or permissions.

What server does Elastic Beanstalk use?

AWS Elastic Beanstalk is an AWS managed service for web applications. Elastic beanstalk is a pre-configured EC2 server that can directly take up your application code and environment configurations and use it to automatically provision and deploy the required resources within AWS to run the web application.


2 Answers

Short version: Your Django app is trying to connect to a local db instance. Use an RDS instance.

You cannot have a db instance (like PostgreSQL) local to a Beanstalk node. This is because it does not make sense on a auto-scaled load-balanced application, where nodes are created and destroyed depending on your service load (therefore destroying the local db instance).

In order to fix this you need to use an AWS RDS instance which is external to the autoscaling group and which is persistent. In your settings, use the host, username and password used to create the RDS instance. Remember to open the proper ports on the security group assigned to the RDS instance (eg. 5432 for PostgreSQL).

like image 80
Sdra Avatar answered Oct 08 '22 15:10

Sdra


It looks like your instance is trying to connect with itself on port 5342. If you need this functionality, you can open port 5342 to get it to work.

One way to do this is with .ebextensions.

Create a file in your project name .ebextensions/openport.config and put the following in it.

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

(Commit file if needed) Then try eb create.

like image 42
Nick Humrich Avatar answered Oct 08 '22 14:10

Nick Humrich