Introduction
Launching an EC2 instance on AWS is the absolute first step for any cloud engineer. However, simply clicking ‘Next’ through the wizard often leads to security vulnerabilities or unexpected charges. In this step-by-step guide, I will walk you through launching an Amazon Linux 2023 EC2 instance correctly, configuring secure network access, and connecting via SSH without exposing your virtual machine to the public internet.
What is Elastic Compute EC2?
A scalable computing service, Amazon EC2 enables customers to run virtual machines (VMs), also known as instances, in the cloud. The CPU, memory, and storage of EC2 instances can be altered to suit specific needs. Depending on your needs, you can start instances of various operating systems, including Windows, Linux, and others.
Step 1: Create EC2 Inastances
- Go to AWS Console Management account
- To setup EC2 instances, you will need an AWS account. If you don’t have one, go to AWS official website and sign up.
- Once you have an account
- Navigate to the AWS Management Console.
- Log in using your AWS credentials.

- Launch an EC2 Instance
- Go to EC2 Dashboard
- From the AWS Management Console, find and click on EC2 under the “Compute” section to open the EC2 Dashboard.
- Click the Launch Instance button to start the process of create EC2 instance.
- Name your server: My-Demo-Server

- Choose an Amazon Machine Image (AMI):
- Amazon provides several pre-configured AMIs that you can choose from based on your use case.
- For example, you can select a Ubuntu Server if you need a Linux-based system or Windows Server for a Windows-based system. In this demo session I selected Amazon Linux.
- Click Select on the AMI you prefer

- Choose an Instance Type
- Here, you select the hardware configuration for your EC2 instance (e.g., CPU, RAM).
- AWS offers various instance types such as t2.micro, t3.medium, etc. For general usage, you can select the t2.micro instance, which is eligible for the free tier if you are a new user.

- Key Pair
- You will be prompted to select a Key Pair for SSH access (we will cover this in the next step).
- Choose “Create a new key pair,” name it, and download the private key file (.pem). Keep this file safe, as you’ll need it to access your instance.

Note: Download your .pem file immediately and save it in a secure directory. AWS will never show or let you download this private key again. If you lose this file, you lose access to your instance entirely, and you will have to go through a painful process of detaching the root volume to recover it
- Network Setting
- In this section, you can configure additional settings like the network settings, and security groups. For now, the default settings should work well.

- Configure Security Group
- This step is important as it involves configuring the firewall for your EC2 instance.
- Create a new security group or select an existing one. A typical security group for web applications would allow SSH (port 22) for Linux instances or RDP (port 3389) for Windows instances and Http (80) and TPS (443) for internet access and select source type anywhere.
- Make sure to allow inbound SSH access if you want to connect to your instance via terminal.


- Add Storage
- By default, a root volume (EBS) is created. You can increase the size if needed, but the default size is typically sufficient for most users.

- Add Tags (Optional)
- Tags help you identify and organize your resources in AWS.
- You can add a tag like Name: MyEC2Instance for easier management.
- Review and Launch
- Review all your selections and click Launch.

Step 2: Accessing Your EC2 Instance
- For Linux Instances (via SSH)
- Open your terminal (Linux/macOS) or use an SSH client (Windows).
- Change Permissions of the Key Pair (for Linux/macOS users)Run the following command to set the correct permissions for the downloaded .pem file
chmod 400 /C/user/jarvuy/downloads/my-demo-keyp-pair.pem
- Find Your Instance’s Public IP Address
- Go back to the EC2 dashboard and click on Instances.
- Select the instance you just launched and find the Public IPv4 address listed.
- Connect to the Instance
- In the terminal, use the following command to connect to your EC2 instance:
ssh -i my-demo-key-pair.pem ec2-user@172.162.122.89
- Replace /path/to/your-key.pem with the path to your key pair file, and your-ec2-public-ip with the public IP address of your EC2 instance.
- For Ubuntu instances, the default username is usually ubuntu. For Amazon Linux, it’s ec2-user.
ssh -i ~/Downloads/my-key.pem ec2-user@54.123.45.67
- Accept the SSH Key Fingerprint
- The first time you connect to the instance, you will be prompted to confirm the authenticity of the host. Type yes to proceed.
- You are now connected to your EC2 instance!
- For Windows Instances (via RDP)
- Download an RDP Client
- If you are using Windows, the default Remote Desktop Protocol (RDP) client is already installed. For macOS and Linux, you can use a tool like Microsoft Remote Desktop.
- Obtain the Administrator Password
- From the EC2 console, select your instance, and click on Connect.
- Click the Get Password button and upload your .pem key file to decrypt the password.
- Connect Using RDP
- Open the RDP client and enter the Public IP address of the EC2 instance.
- When prompted, use the Administrator username and the decrypted password to log in.
- Download an RDP Client
Step 3: Fixing the Common SSH ‘Unprotected Private Key File’ Error
- If you try to connect via SSH from a Linux or Mac terminal using a freshly downloaded key, you will likely encounter this error
Permissions 0644 for 'key.pem' are too open
- To fix this, you must restrict the file permissions before running the SSH command. Run the following command in your terminal
chmod 400 your-key-pair.pem
- This ensures that only your current user has read permissions for the key, which is a mandatory security requirement for SSH
Step 4: Terminate the Instance (Optional)
- If you’re done with the instance and want to avoid unnecessary charges, it’s important to terminate it:
- Go to the EC2 dashboard.
- Select your instance.
- Click on the Actions dropdown and select Instance State > Terminate.
Best Practice Tip
- Never open Port 22 to
0.0.0.0/0(Anywhere) in a production environment, as it invites brute-force attacks from bots worldwide. Always select ‘My IP’ to restrict access to your specific network location
Conclusion
Whether you are using Windows or Linux, setup EC2 instance and accessing an instance is simple. You may grow your resources according to your demands with the flexibility that AWS EC2 offers. You may launch an instance, gain access to it, and begin utilizing it for your applications by following these easy steps.

