AWS: Can't Connect to Amazon Linux EC2 Instance? Here's How to Fix

AWS: Can’t Connect to Amazon Linux EC2 Instance? Here’s How to Fix
If you’re a data scientist or software engineer working with the AWS ecosystem, there’s a good chance you’re using Amazon Elastic Compute Cloud (EC2) instances. However, connection issues can sometimes occur. In this post, we’ll walk through the steps to troubleshoot and fix common problems that prevent you from connecting to an Amazon Linux EC2 instance.
1. Check Your Security Group Rules
Firstly, check your EC2 instance’s security group rules. Ensure that inbound rules allow SSH (port 22) traffic from your IP address.
1. Open the Amazon EC2 console.
2. In the navigation pane, choose 'Instances', select your instance.
3. On the 'Description' tab, under 'Security groups', choose your security group.
4. In the inbound rules, check for a rule that allows SSH (port 22) from your IP address.
If the rule doesn’t exist, add a new rule:
1. Choose 'Edit inbound rules'.
2. Choose 'Add rule'.
3. For 'Type', choose 'SSH'.
4. For 'Source', choose 'My IP'.
5. Choose 'Save rules'.
2. Confirm Your Key Pair and SSH Configuration
Ensure you are using the correct private key file (.pem) that corresponds to the key pair associated with the instance.
ssh -i /path/my-key-pair.pem ec2-user@my-instance-public-dns-name
If you receive a ‘Permission denied’ error, your key might not be set with the right file permissions. The .pem file must have strict permissions:
chmod 400 /path/my-key-pair.pem
3. Check Network Accessibility
Ensure your network allows traffic on port 22. Some corporate firewalls block this port, causing connection failures. You can check network accessibility with the following command:
nc -vz my-instance-public-dns-name 22
4. Verify the Instance’s Status
Check the instance’s status checks in the AWS console to ensure it’s in a ‘running’ state and passing both system and instance status checks.
5. Troubleshoot Using EC2Rescue
EC2Rescue is a robust tool offered by AWS for troubleshooting and fixing common Linux issues. Use it if the previous steps do not resolve your connection problem.
1. Open the AWS Systems Manager console.
2. In the navigation pane, choose 'Automation', and then 'Execute automation'.
3. In the 'Automation document' list, choose 'AWSSupport-ExecuteEC2Rescue'.
4. In the 'Execution mode' section, choose 'Simple execution'.
5. In the 'Input parameters' section, for 'EC2RescueInstanceID', enter the ID of the impaired instance.
6. Choose 'Execute'.
Conclusion
Connecting to an Amazon Linux EC2 instance should be straightforward, but occasional issues can arise. By following these steps, you can identify and rectify common problems. Always remember to secure your instances and maintain the correct configurations to keep problems at a minimum. Happy troubleshooting!
Remember to share this post with your team if it helped you, and comment below with any other issues or solutions you’ve found useful.
keywords: AWS, Amazon Linux EC2, Troubleshoot, Connection Issues, Guide, Security Group, Key Pair, SSH, Network Accessibility, EC2Rescue
About Saturn Cloud
Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Join today and get 150 hours of free compute per month.