Loading...

How to connect to EC2 instance in AWS?

How to connect to EC2 instance in AWS?

In this blog post, we are going to explore various ways to connect to an EC2 instance in AWS. Amazon Web Services (AWS) provides a wide range of cloud computing services, and among them, Elastic Compute Cloud (EC2) is one of the most popular. EC2 instances are virtual servers that can be used for many purposes, including hosting web applications, running databases, and more. Connecting to an EC2 instance is an essential skill for any developer working with AWS. Let's dive in and learn how to connect to an EC2 instance in various ways, step by step.

Prerequisites

Before we begin, make sure you have the following:

  1. An AWS account (sign up for a free tier account if you don't have one)
  2. AWS CLI installed on your local machine
  3. An active EC2 instance in your AWS account

Connecting to an EC2 Instance Using SSH

One of the most common methods to connect to an EC2 instance is by using Secure Shell (SSH). SSH is a cryptographic network protocol that allows secure communication between two devices over an unsecured network.

Step 1: Locate your EC2 instance

First, log in to the AWS Management Console and navigate to the EC2 Dashboard. Then, click on "Instances" in the left sidebar to see a list of your active instances.

Step 2: Obtain the EC2 instance's public IP address or hostname

Select the instance you want to connect to, and note its public IP address or hostname, which is displayed in the "Description" tab.

Step 3: Locate your private key file

When you launched your EC2 instance, you should have created a key pair and downloaded the private key (.pem) file. Locate this file on your local machine.

Step 4: Set the correct permissions for the private key file

On your local machine, open a terminal and navigate to the directory where your private key file is stored. Set the correct permissions for the file by running the following command:

chmod 400 your-key-pair-name.pem

Step 5: Connect to the EC2 instance using SSH

Now, use the following command to connect to the EC2 instance:

ssh -i your-key-pair-name.pem ec2-user@your-public-ip-or-hostname

Replace your-key-pair-name.pem with the name of your private key file and your-public-ip-or-hostname with the public IP address or hostname of your EC2 instance. The default username for Amazon Linux instances is ec2-user.

If the connection is successful, you will be logged in to your EC2 instance.

Connecting to an EC2 Instance Using AWS CLI

Another way to connect to an EC2 instance is using the AWS CLI. This method is especially useful when you need to automate tasks or work with multiple instances.

Step 1: Configure the AWS CLI

If you haven't already, configure the AWS CLI by running the following command:

aws configure

You will be prompted to enter your AWS access key ID, secret access key, default region, and default output format. You can find your access keys in the "Security Credentials" section of your AWS account.

Step 2: Connect to the EC2 instance using the AWS CLI

Use the following command to connect to your EC2 instance:

aws ec2-instance-connect send-ssh-public-key --instance-id your-instance-id --availability-zone your-instance-availability-zone --instance-os-user ec2-user --ssh-public-key file://your-public-key-file

Replace your-instance-id with the ID of your EC2 instance, your-instance-availability-zone with the availability zone of your instance, and your-public-key-file with the path to your public key file. You can find this information in the "Instances" section of the EC2 Dashboard.

If the command is successful, you will receive a JSON response containing the instance ID and a message indicating that the SSH key was sent successfully.

Now, use the SSH command from the previous section to connect to your EC2 instance.

Connecting to an EC2 Instance Using AWS Systems Manager Session Manager

AWS Systems Manager Session Manager is a fully managed service that allows you to manage and troubleshoot your EC2 instances through an interactive one-click browser-based shell or through the AWS CLI.

Step 1: Enable Session Manager for your EC2 instance

To enable Session Manager for your EC2 instance, attach the AmazonSSMManagedInstanceCore IAM policy to your instance's IAM role. You can do this by navigating to the IAM Dashboard, clicking on "Roles," and selecting your instance's role. Then, click on "Attach policies" and search for AmazonSSMManagedInstanceCore. Check the box next to the policy and click "Attach policy."

Step 2: Install and configure the Session Manager plugin

On your local machine, download and install the Session Manager plugin for the AWS CLI.

Step 3: Start a Session Manager session

To start a session, open a terminal and run the following command:

aws ssm start-session --target your-instance-id

Replace your-instance-id with the ID of your EC2 instance.

If the connection is successful, you will be logged in to your EC2 instance.

FAQ

Q: Can I connect to an EC2 instance using RDP?

A: Yes, you can connect to an EC2 instance running Windows using Remote Desktop Protocol (RDP). You will need the public IP address or hostname of the instance, the administrator username, and the password to log in.

Q: Can I connect to an EC2 instance using FTP or SFTP?

A: Yes, you can connect to an EC2 instance using File Transfer Protocol (FTP) or Secure File Transfer Protocol (SFTP) to transfer files. You will need to install and configure an FTP or SFTP server on your instance and open the necessary ports in your instance's security group.

Q: How do I troubleshoot connection issues with my EC2 instance?

A: If you're unable to connect to your EC2 instance, make sure that the instance is running, the security group allows incoming traffic on the necessary ports (e.g., 22 for SSH), and your private key file has the correct permissions. You can also check the instance's system log and status checks in the EC2 Dashboard for any errors or issues.

That's it! We've covered various ways to connect to an EC2 instance in AWS, including using SSH, the AWS CLI, and AWS Systems Manager Session Manager. With these methods, you can now securely access your instances for management and troubleshooting. Happy coding on codedamn!

Sharing is caring

Did you like what Rishabh Rao wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far