behzodfaiziev / command-collection

This repository serves as a centralized collection of commands from various services, making it a handy reference for developers, system administrators, and anyone working with different technologies.
1 stars 0 forks source link

add Documentation on how to configure SSH for EC2 with Elastic Beanstalk #23

Open behzodfaiziev opened 1 week ago

behzodfaiziev commented 1 week ago

Steps to Create a New .pem Key Pair and Use it for SSH Step 1: Create a New Key Pair in AWS Console Go to the EC2 Dashboard:

Open the AWS Management Console and navigate to the EC2 Dashboard. Create a New Key Pair:

In the left-hand menu, under Network & Security, select Key Pairs. Click the Create Key Pair button. Fill out the following:

Name: Give your key pair a descriptive name (e.g., my-eb-key). File Format: Choose .pem (for Linux/Mac users). Click Create Key Pair. The .pem file will automatically download to your machine.

Move the .pem File to Your Linux Machine:

If you're on the same machine you’ll be connecting from, skip this step. Otherwise, transfer the .pem file to your Linux machine securely, using a method such as SCP. Example using SCP:

bash Copy code scp my-eb-key.pem user@your-linux-machine:/path/to/your/local/directory Set Permissions for the Key Pair:

On your Linux machine, ensure the key pair has the correct permissions: bash Copy code chmod 400 my-eb-key.pem Step 2: Assign the New Key Pair to Your Elastic Beanstalk EC2 Instance Stop the EC2 Instance:

Navigate to the Instances section in the EC2 Dashboard. Select the EC2 instance associated with your Elastic Beanstalk environment. Click Instance State → Stop Instance. (Do not Terminate the instance, just Stop it.)

Create an Image (AMI) from the Instance:

After the instance is stopped, select it, click Actions → Image and templates → Create Image. Give the image a name (e.g., my-eb-instance-ami) and click Create Image. Launch a New Instance from the AMI:

Once the image is created, go to AMIs in the left-hand menu under Images. Select the AMI you just created, and click Launch. During the launch process: Choose the same instance type (e.g., t2.micro). In the Key Pair section, choose the new key pair (my-eb-key). Launch the instance. Replace the Old Instance (Optional):

If you want to replace the instance managed by Elastic Beanstalk with this new instance, you can manually adjust the Elastic Beanstalk configuration, but in most cases, simply SSHing into this new instance is enough. Step 3: Update the Security Group to Allow SSH from Your IP Go to the Security Groups Section:

In the EC2 Dashboard, click Security Groups in the left-hand menu. Find the Security Group for Your EC2 Instance:

Locate the security group used by your Elastic Beanstalk environment or the new instance you just launched. Edit Inbound Rules:

Select the security group and click Edit Inbound Rules. Add the following rule: Type: SSH Protocol: TCP Port Range: 22 Source: Choose My IP (it will automatically detect your current public IP). Save the rule. Step 4: Connect to Your EC2 Instance via SSH Get the Public IP/DNS of the EC2 Instance:

In the EC2 Dashboard, go to Instances. Select your new instance, and copy the Public IPv4 address or Public DNS name (e.g., ec2-203-0-113-25.compute-1.amazonaws.com). SSH into the Instance:

Use the .pem key to SSH into the instance from your Linux terminal: bash Copy code ssh -i "/path/to/my-eb-key.pem" ec2-user@ec2-203-0-113-25.compute-1.amazonaws.com Replace /path/to/my-eb-key.pem with the actual path to your .pem file. Replace the ec2-user with the appropriate user for your instance's AMI: For Amazon Linux: ec2-user For Ubuntu: ubuntu Troubleshooting SSH: Permission Denied: Ensure your .pem file has the correct permissions (chmod 400). Connection Timeout: Double-check that your security group is allowing SSH (port 22) access from your IP address.

behzodfaiziev commented 1 week ago

When creating a key pair in AWS, you will be asked to choose the key type between RSA and ED25519. Here’s a brief explanation to help you choose:

Key Types: RSA (Rivest-Shamir-Adleman):

Widely supported: RSA is the most widely supported key type across all systems, tools, and services. Security: RSA is considered secure, though the key size matters. AWS typically uses 2048-bit RSA keys, which are secure and widely compatible. Performance: RSA key generation and verification are slower compared to ED25519, but this isn’t usually a noticeable difference for typical SSH usage. Use RSA if you want maximum compatibility across various platforms and tools.

ED25519 (Edwards-curve Digital Signature Algorithm):

Newer and faster: ED25519 keys are faster for signing and verification compared to RSA. They also use smaller keys and are optimized for performance. Security: ED25519 is considered very secure, even more than RSA at equivalent key sizes. It uses elliptic curve cryptography, which offers strong security with smaller key sizes (256-bit). Support: While supported by most modern systems, it may not be compatible with older services or systems that expect RSA keys. Use ED25519 if you prioritize speed and modern cryptography and don't need to worry about compatibility with older systems.

Which to Choose? RSA: Choose this if you want broad compatibility, especially if you are working with older systems, services, or tools that may not support ED25519. ED25519: Choose this if you prefer faster performance and more efficient cryptography, and you’re confident that your system (and tools) support ED25519. Since AWS EC2 supports both, you should be fine with either choice. If you're unsure and want broader compatibility, I recommend going with RSA.

behzodfaiziev commented 1 week ago

To create a script in Linux that opens a terminal and connects to an SSH server using a .pem file, follow these steps:

Steps to Create a Script for SSH Connection: Step 1: Create a New Script File Open a Terminal on your Linux system. Create a new script file with a .sh extension (e.g., connect-ssh.sh). You can use a text editor like nano, vim, or any editor you prefer. Here, we’ll use nano: bash Copy code nano ~/connect-ssh.sh Step 2: Write the SSH Command in the Script Inside the script file, write the command that will be used to connect to the SSH server.

For example:

bash Copy code

!/bin/bash

Script to connect to an EC2 instance via SSH

ssh -i "/path/to/my-eb-key.pem" ec2-user@ Replace:

/path/to/my-eb-key.pem with the actual path to your .pem file (e.g., ~/.ssh/my-eb-key.pem). ec2-user@ with the user (ec2-user for Amazon Linux, ubuntu for Ubuntu) and the public IP address or DNS name of your EC2 instance (e.g., ec2-user@ec2-54-123-45-67.compute-1.amazonaws.com). Step 3: Save the Script If you're using nano, save and exit by pressing CTRL + X, then Y, and press Enter. Step 4: Make the Script Executable After saving the file, you need to make the script executable so that you can run it directly.

Run the following command to give the script execute permissions:

bash Copy code chmod +x ~/connect-ssh.sh