AWS EC2 Hosted Discord Bot Experiencing Connection Timeout Issues

I’m having trouble with my Discord bot on AWS EC2. The bot won’t start because the client.login() call times out after 10 seconds. Here’s what’s happening:

  • EC2 instances are starting up from my auto scaling group
  • The cluster is running the task to start my Docker image
  • New tasks keep spawning when others fail (which is good for keeping the bot online)

I’ve tried a few things to fix it:

  • Tested connecting to Discord with axios.get()
  • Opened up all ports for my EC2, Load Balancer, and ECS Service
  • Checked that the bot runs fine locally and in Docker

Has anyone run into this before? Any ideas on what might be causing the timeout?

Here’s the error I’m getting:

ConnectTimeoutError: Connect Timeout Error
code: 'UND_ERR_CONNECT_TIMEOUT'

I’m using Node.js with discord.js v14. My setup includes Terraform for AWS config, ECR for the Docker image, and ECS for running the container. Any help would be great!

hey mate, had similar issues before. try checking ur EC2 instance limits - sometimes AWS throttles connections if u hit the limit. also, make sure ur using the latest discord.js version. older ones can be finicky with AWS. if all else fails, maybe try a different region? some r better for discord bots than others. good luck!

Have you considered implementing a retry mechanism with exponential backoff? This approach can be quite effective for handling intermittent connection issues. You could wrap your client.login() call in a function that attempts to reconnect multiple times, increasing the delay between attempts.

Another avenue to explore is your network’s DNS configuration. Sometimes, EC2 instances struggle with name resolution. You might want to try using Google’s public DNS servers (8.8.8.8 and 8.8.4.4) to see if that resolves the issue.

It’s also worth checking your EC2 instance’s network interface. Ensure it’s correctly configured and that there are no packet loss issues. You can use tools like tcpdump or Wireshark to analyze network traffic and pinpoint where the connection is failing.

Lastly, have you considered using AWS Lambda instead of EC2? Lambda can be more cost-effective for running Discord bots and often has fewer networking issues.