Connecting to Your EC2 Instance Using SSH
As a former 3D Animator with more than 12 years of experience, I have always been fascinated by the intersection of technology and creativity. That's why I recently shifted my career towards MERN stack development and software engineering, where I have been serving since 2021.
With my background in 3D animation, I bring a unique perspective to software development, combining creativity and technical expertise to build innovative and visually engaging applications. I have a passion for learning and staying up-to-date with the latest technologies and best practices, and I enjoy collaborating with cross-functional teams to solve complex problems and create seamless user experiences.
In my current role as a MERN stack developer, I have been responsible for developing and implementing web applications using MongoDB, Express, React, and Node.js. I have also gained experience in Agile development methodologies, version control with Git, and cloud-based deployment using platforms like Heroku and AWS.
I am committed to delivering high-quality work that meets the needs of both clients and end-users, and I am always seeking new challenges and opportunities to grow both personally and professionally.
In the previous session, we set up our EC2 server and created a key pair. This key pair, saved as a .pem file, will enable us to securely connect to our server using the SSH protocol.
Let’s go step by step to connect to the EC2 instance and ensure proper setup:
Step 1: Locate the Key Pair File
The .pem file you downloaded during the creation of the key pair is essential for accessing the server. Ensure you store this file securely in a dedicated folder (e.g., key_pairs).
Step 2: Navigate to Your EC2 Dashboard
Go to your AWS Management Console, open the EC2 Dashboard, and click on the Instances section. Select the instance you created (e.g., Nasser Project Instance) and verify its state is running.
Step 3: Access Connection Instructions
Once you select your instance, click on the Connect button. Amazon provides instructions for accessing your instance using an SSH client or a browser-based SSH connection. For this session, we will use an SSH client.
Step 4: Connect to Your Instance
Linux and macOS Users:
Open your terminal and navigate to the folder containing your
.pemfile:cd /path/to/key_pairsRun the command Amazon provides to connect. It should look like this:
ssh -i "key_pair_name.pem" ec2-user@your-public-dns-ispecifies the private key file.ec2-useris the default user Amazon creates for EC2 instances.Replace
your-public-dnswith the public DNS or IP address of your instance.
Verify the connection when prompted:
Are you sure you want to continue connecting (yes/no)? yesThis step adds the server to your list of known hosts, so you won’t be asked again for future connections.
Adjusting Permissions for .pem File
If you’re on Linux or macOS, you may encounter a warning about the .pem file permissions being too open. To fix this, run the following command:
chmod 400 key_pair_name.pem
This command ensures the file is readable only by your user and prevents modifications to it. After running this command, retry the SSH connection.
Step 5: Interact with Your Server
Once connected, you’ll see a Linux shell where you can execute commands. For example:
List directory contents:
lsPrint a message:
echo "Hello, EC2!"
Step 6: Browser-Based SSH Connection
AWS also allows you to connect to your EC2 instance through a browser-based terminal:
Click on Connect in the EC2 Dashboard.
Choose the Session Manager or EC2 Instance Connect option.
Click Connect to open a terminal in your browser.
While this method is convenient, it’s recommended to use an SSH client on your local machine to gain familiarity with terminal-based server management.
Important Notes
Keep Your
.pemFile Secure: This private key is crucial for accessing your instance. Never share it or upload it to a public repository.Avoid Adding
.pemto Your Source Code: Ensure this file is not included in your project directory or version control system.Secure Permissions: Always ensure that the
.pemfile has the correct permissions to avoid connection issues.
Step 7: Set Up Your Server
With SSH access established, you can now configure your EC2 instance to host your application or perform other tasks. We’ll cover this setup in the next session.
You’re now successfully connected to your EC2 instance and ready to take the next steps in your project setup!