DEV Community

Hritik Raj
Hritik Raj

Posted on

Battling "Connection Refused" and "Externally-Managed-Environment" on AWS EC2

Deploying applications on AWS can be a rewarding experience, but it also comes with its share of challenges. Today, I wrestled with a couple of common (and some not-so-common) errors while setting up a Node.js and Python-based application on an EC2 instance.

The "Connection Refused" Saga:

My Node.js server was running on port 3000, but I kept getting "connection refused" when trying to access it from my browser. The issue? AWS security groups. If you're new to AWS, remember that security groups act as virtual firewalls for your EC2 instances. Make sure to open the necessary ports in your security group's inbound rules.

Python Dependency Hell:

My Node.js app relied on a Python script for data processing. I encountered "spawn python ENOENT," which meant Python wasn't installed. After installing Python 3, I faced another hurdle: ModuleNotFoundError: No module named 'pandas'.

Trying to install pandas with pip3 resulted in the "externally-managed-environment" error. This is a new security feature in recent Ubuntu versions that encourages using virtual environments.

The Solution:

  1. Virtual Environments: I created a virtual environment with python3 -m venv venv and activated it using source venv/bin/activate.
  2. Package Installation: Inside the virtual environment, I installed pandas using pip3 install pandas.
  3. Node.js Update: I updated my Node.js code to use python3 when spawning the python process.

Lessons Learned:

  • Always verify security group rules.
  • Embrace virtual environments for Python dependency management.
  • Read error messages carefully; they often provide valuable clues.
  • When executing a child process, always fully define the path to the executable.

Debugging is an essential part of development. By systematically addressing each error, I was able to get my application up and running.

Top comments (0)