How to Launch an AWS EC2 Instance (Ubuntu) and Connect via SSH
Step-by-step guide to launching an Ubuntu EC2 instance on AWS: choose a region and instance type, create a key pair, set security group rules, and connect from the browser or an SSH client.
What you'll use
Before you start
In this guide you will launch an Ubuntu server on Amazon EC2 and log in to it. It takes about ten minutes. You need:
- An AWS account and an IAM user to sign in with. Don't use the root account for daily work.
- A terminal: Terminal on macOS or Linux, PowerShell or Command Prompt on Windows.
- The server name (
my-backend-project) and key pair name (my-project) used below are examples. Use your own.
Open EC2 and choose your region
- Sign in to the AWS Console with your IAM user.
- Use the search bar at the top, type
EC2and open the service.
Before you create anything, check the region selector in the top-right corner and pick the region nearest to your users or your team, for example Asia Pacific (Hyderabad) ap-south-2.
On the EC2 dashboard click Launch instance.
Name your server
Under Name and tags, set Name to something that tells you what the server is for, for example:
my-backend-projectThis becomes the Name tag you will see in the instances list.
Choose the Ubuntu image
Under Application and OS Images (Amazon Machine Image), open the Quick Start tab and select Ubuntu. The console picks a current Ubuntu Server LTS Amazon Machine Image (AMI) for you. Keep that default.
An AMI is the template your server boots from, here the operating system. Because you chose Ubuntu, the default login user is ubuntu, which you will need when you connect.
Pick the instance type
The instance type sets how much CPU and memory your server gets. For a Node.js backend with a database and Nginx on the same machine, this guide uses t3.medium.
| Type | vCPU | RAM | Good for | Free tier |
|---|---|---|---|---|
| t3.micro | 2 | 1 GiB | Learning, tiny test apps | Usually eligible |
| t3.small | 2 | 2 GiB | Small apps, light traffic | Check the console label |
| t3.medium | 2 | 4 GiB | App + MySQL + Nginx together | Not eligible |
- vCPU is the number of virtual processor cores. RAM is the memory your app, database and operating system share.
- T3 instances are burstable. They run at a baseline CPU level and can burst higher for short periods, which suits most small web backends.
- Free tier rules depend on your account type and change over time. The console marks eligible types with a
Free tier eligiblelabel, so trust that label.t3.mediumis not free, so if you only want to practise, choose an eligible type.
npm install or builds. That is why t3.medium is the safer starting point for this setup.Create the key pair
The key pair is how you prove who you are when you log in over SSH. AWS keeps the public half. You download the private half once.
- Under
Key pair (login)clickCreate new key pair. - Key pair name:
my-project. - Key pair type:
RSA. - Private key file format:
.pem. - Click
Create key pair.
The browser downloads my-project.pem. Then select that key pair in the dropdown.
.pem file somewhere safe and never commit it to Git or share it. If you lose it, you lose SSH access to this instance.Configure network settings
Under Network settings you create the firewall rules (a security group) for the instance. You will see three checkboxes:
Allow SSH traffic from: lets you connect to the instance. It is already checked.Allow HTTPS traffic from the internet: needed to serve a site or API over HTTPS. Check it.Allow HTTP traffic from the internet: needed to serve a site or API over HTTP. Check it.
Leave everything else on its defaults.
0.0.0.0/0 on a real server. Restrict port 22 to your own address in the form xx.xxx.xx.xxx/32. You can find your public IP at https://checkip.amazonaws.com/.For now the default is fine. You will lock the rules down at the end of your deployment, once you know exactly which services your server runs.
Launch the instance
Review the Summary panel (name, image, instance type, key pair, storage), then click Launch instance. The default storage is 8 GiB, which is enough for this walkthrough. Increase it if you plan to host a database or many uploads.
AWS shows a success message with your instance ID, for example i-0123456789abcdef0. Click it, or open EC2 → Instances, and wait:
Instance statechanges fromPendingtoRunning.Status checkchanges fromInitializingto2/2 checks passed.
Once the instance is running and the checks pass, you can connect.
Connect to your instance
Click the instance ID to open its details page, then click Connect at the top right. There are two ways to log in.
This is the method to use on any real server. On the SSH client tab, copy the example under Connect to your instance using its Public DNS. It looks like this:
ssh -i "my-project.pem" ubuntu@ec2-xx-xxx-xx-xxx.ap-south-2.compute.amazonaws.com- Open Terminal.
- Go to the folder where the
.pemfile was downloaded. - Make the key readable only by you, or SSH will refuse to use it.
- Run the command you copied.
cd ~/Downloads
chmod 400 my-project.pem
ssh -i "my-project.pem" ubuntu@ec2-xx-xxx-xx-xxx.ap-south-2.compute.amazonaws.comThe first time, SSH asks whether to trust the host fingerprint. Type yes and press Enter.
You are in when the prompt changes to something like ubuntu@ip-xx-xx-xx-xxx:~$. That address is the server's private hostname, not the public one you connected with.
/32), you can connect from that IP only. Anyone else, and you on a different network, is blocked until the rule is updated.Verify you are logged in
Run this in the terminal:
whoamiIt should print:
ubuntuYour Ubuntu server is up and you have shell access. You can also check the OS version and the memory it has:
lsb_release -a
free -h
nprocTroubleshooting connection errors
Connection timed out: port 22 is blocked for your current IP. Open the instance's security group, edit the inbound SSH rule and set the source toMy IP. Your home IP changes from time to time, so update it when it does.Permissions 0644 for 'my-project.pem' are too open: runchmod 400 my-project.pem(macOS/Linux) or use theicaclscommands from the Windows tab.Permission denied (publickey): you are using the wrong username or key. For Ubuntu the user isubuntu, notec2-userorroot, and the key must be the one chosen at launch.No such file or directoryfor the key: your terminal is not in the folder that holdsmy-project.pem. Usecdto go there, or pass the full path to-i.- Browser connect fails: the SSH rule is probably restricted to your IP. Use the SSH client instead.
- The address stopped working after a restart: the public DNS and public IP change when an instance is stopped and started. Copy the new address from the console, or attach an Elastic IP.
Good habits and next steps
- Restrict SSH to your own
/32address once the setup is finished, and keep only the ports you actually use open. - Attach an Elastic IP if the server needs a stable address, for example when you point a domain at it.
- Stop the instance when you don't need it, and terminate it (and delete unused storage) when you are done for good.
- Keep the
.pemfile private and back it up somewhere safe.
Your server is ready. The next step is installing the runtime, your app, a web server and SSL on it.
Next article
AWS EC2 Production Deployment Guide