Can't Connect to my EC2 instance with SSH

0

Hey everyone, I have been suffering a hard time connecting to my EC2 instance with SSH recently. At the beginning I could connect to it but for somehow reason I lost my connection. (tried both with my VS code studio and terminal) Here are what I have been tried to troubleshoot the problem:

  1. Reboot my instance
  2. Reboot my PC
  3. Double verify the IPv4 IP address and pair key
  4. Double verify my inbood rule is open
  5. Double verify the instance is running with 2 green check passed
  6. Double verify the ssh config set up is correct
  7. Ping to google but doesn't receive anything It seems that the only way that I could successfully re-connect to it is stopping the current one and re-starting a new one. However, Amazon will re-allocate a new IPv4 public address to me. I have to re-set up some of my working environment. I do have bash script set up for EC2 connection and transferring file with sshfs. But it is just so annoying and time consuming that I have to do it every time. BTW, my EC2 instance doesn't have the elastic IP address because I am at the free tier and don't want to pay anything yet. Does anyone have a better idea without doing those. Appreciate your response in advance.
asked a year ago6269 views
3 Answers
1

The fact that you can SSH to the instance's public IP when it is first started shows that your security groups and routing tables etc. are likely all correct. but then a short while later (are we talking hours or days?) you can no longer connect.

Does the security group allow ingress on TCP port 22 from any address (0.0.0.0/0) or did you select "My IP" when setting up the security group for the instance? If it was limited to your IP then has that changed - depending on your ISP your Home IP address can change periodically, and obviously your IP is different at home/at work/on VPN/on cellular.

The other thing I'm thinking of is that to be running within free tier you must be using a t2.micro or t3.micro instance, which are pretty puny. What else are you running on it? You may be running something that is fine at the start, but slowly consumes more and more resources until you can't login any more (it will still show up as running and 2/2 checks passed in AWS Console as it's still actually running, though unusable). Select the instance in AWS Console and look in the Monitoring tab, is there anything of concern in here?

profile picture
EXPERT
Steve_M
answered a year ago
  • Thanks for your reply. I do open port 22 for all incoming IP address. And you are 100% right. I am running a t2.micro. Not sure if this is true that I do feel it starts slowing down after few weeks since the first time I create it. When checking the monitoring tab for the instance while it still show 2/2 checks passed, I do notice that the CPU usage is up to 70%. I mean I really didn't do something seriously but just running small Docker container. I believe you are on the right track. This happened to me few time already. Every time I run the docker container, the terminal feels like freezing for few minutes and re-back up. Same thing for my visual studio code. It lost connection for few minutes and can be re-connect to it after a while. Is that just because I am using the free tier? Thanks.

  • Free tier instances are, as you would expect, not very powerful and you can quickly run out of resources. 70% CPU usage isn't a problem if it's expected that your application will need this much CPU, but it can be a warning of other issues. t2.micro has only 1GB of memory, if your application is trying to use more memory than is available then the memory manager comes into play, and will try to swap processes out of main memory and onto disk (it's more complicated, but that's the general gist of it). This in itself is CPU-intensive, and as free memory gets less and less the memory manager will spend more and more of its time (and more and more CPU) trying to free up pages of main memory, leaving fewer and fewer CPU cycles for anything else (including handling SSH requests for login).

    It might not even be memory usage, that's just a guess. Many years ago I ran a website on a free tier EC2 and found that a memory leak in Apache was making my EC2 become unavailable after about a month. The fix was to restart Apache once a week.

    Unfortunately the EC2 section of the AWS Console doesn't display metrics for memory use, you'll need to setup CloudWatch agent to collect these https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Install-CloudWatch-Agent.html

  • @RWC Thanks for your prompt reply. After checking the monitor tab, it doesn't take 70% of CPU usage but almost close to 100% while the container is running. Wow! No idea why that happens. Now everything makes sense. Thanks for your insights.

1

Hi, what does "Ping to google but doesn't receive anything" mean? Where did you ping from?

Does EC2 Instance Connect work?

I don't know what the problem is but note that to make things a little less painful you can use an Elastic IP for free so long as it remains attached; it's only the unused ones that you get charged for.

EXPERT
answered a year ago
  • What I mean by "Ping to google but doesn't receive anything" is typing command ping www.google.com, and the result is I don't receive any feedback. I have to interrupt it by using Ctrl + C. Regarding the elastic IP address, I tried it once before with attaching my running EC2 instance, and receiving an email from AWS and been notified that I am running out of 85% of my usage of the month. I looked up for amazon document, it does say it won't charge you if you attach it to your active EC2 but seeing and hearing different answer from people. Is that like totally free as long as you keep it attached to your running EC2 instance? So why am I still receiving email from AWS? Thanks.

  • Free tier gives you a certain amount per month. If you're running a "free tier included" instance constantly and get the 85% warning when you're 85% of the way through the month then there's nothing to worry about.

1

Another common issue with EC2 instances is that the IP address has changed. When you assign a public IP address during the creation of the EC2 instance, it is not permanently assigned. What happens is after a Stop and Start (not a reboot), a new IP address is assigned. So can you verify you are trying to connect to the correct address? And verify you didn't hard code the address in any ACLs or Security Groups. If you want to keep your IP address permanently, you need to create the EC2 instance with no public IP and then manually assign an allocated elastic IP. This IP address will survive reboots.

profile pictureAWS
answered a year ago
  • @Christian Thanks for your comment. I do remind changing IP address every time I stop and re-start the instance. Never hard code the address in any ACL or security groups. After reading @RWC's comments listed above, I did couple test by running different docker container on EC2 instances and it just fine. If I didn't run anything, and the connection will stay constant perfect. So, now I believe the issue is more related to CPU or memory usage.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions