Creating Your First AWS EC2 Instance

Launch a Linux server on Amazon EC2 from the console or CLI, connect over SSH, Instance Connect or Session Manager, and clean up without stray charges.

Cloud

Launching an EC2 instance takes about two minutes. Understanding what you just launched takes a little longer, and that is the part that decides whether the server is reachable, whether it is safe, and whether it is still costing you money a month later.

This article walks through creating a small Linux server on Amazon EC2, first in the console and then with the AWS CLI. Along the way it explains each choice the launch wizard asks for, the three ways to connect, and what happens to the instance and its bill when you stop or terminate it.

What an EC2 instance is made of

An EC2 instance is a virtual machine running in one Availability Zone of one AWS Region. When you launch one, you are really assembling several separate resources:

  • An AMI (Amazon Machine Image): the template for the root disk, including the operating system.
  • An instance type: how many vCPUs and how much memory the machine gets, such as t3.micro.
  • A subnet in a VPC: where the instance sits on the network, which decides whether it can get a public IP address.
  • A security group: a stateful firewall attached to the instance’s network interface.
  • One or more EBS volumes: the disks, created from the AMI’s snapshot.
  • A key pair: the public key AWS places on the instance so you can log in with the matching private key.
flowchart TB
    subgraph Region["Region, for example us-east-1"]
        subgraph VPC["Default VPC"]
            subgraph Subnet["Public subnet in one AZ"]
                I["EC2 instance (t3.micro)"]
                V["EBS root volume"]
                SG["Security group"]
            end
        end
    end
    AMI["AMI: Amazon Linux 2023"] -->|"copied to"| V
    V --- I
    SG -->|"filters traffic to"| I
    K["Key pair: public half"] -->|"installed on"| I
    U["You, with the private key"] -->|"SSH on port 22"| SG

Each of these has its own lifecycle. That matters later: terminating an instance does not automatically remove its security group or key pair, and depending on a setting, not always its volumes either.

Before you launch

A few things are worth sorting out first.

Use an IAM identity, not the root user. Sign in as an IAM user or through IAM Identity Center with permissions to manage EC2. AWS Identity and Access Management (IAM) explains why the root user should stay locked away.

Pick a Region and stay in it. EC2 resources are Regional, so an instance launched in Ohio won’t appear when the console is set to Virginia. Choose one near you and check the Region selector at the top right of the console before every launch. AWS Global Infrastructure covers how Regions and Availability Zones relate.

Know what is free on your account. Accounts created on or after July 15, 2025 use the credit-based Free Tier, and the instance types that count as free tier eligible on them are t3.micro, t3.small, t4g.micro, t4g.small, c7i-flex.large and m7i-flex.large. Older accounts are on the legacy program, whose 12-month EC2 allowance has now expired for everyone. AWS Free Tier and Billing Overview explains both, and it is worth setting up a budget alert before you start.

Launching an instance in the console

Open the EC2 console, make sure the right Region is selected, and choose Launch instance. The wizard is a single page with collapsible sections. Here is what each one does and what to pick for a first server.

Name and tags

The name you type becomes a tag with the key Name. It is what the instance list shows, so make it descriptive, such as first-server. You can add more tags here, like project or env, which later let you filter instances and break down costs.

Application and OS image

This is the AMI. For a first Linux server, choose Amazon Linux 2023. It is AWS’s own distribution, it comes with the SSM Agent and EC2 Instance Connect already installed, and it is supported until 2029.

Avoid Amazon Linux 2, which many older tutorials use. It reached end of support on June 30, 2026, and no longer receives regular security updates. If you prefer Ubuntu, pick a current LTS release published by Canonical, not an older one like 20.04 that has left standard support.

Pay attention to the architecture. Graviton instance types such as t4g.micro need an Arm (arm64) AMI; Intel and AMD types such as t3.micro need x86_64. The wizard filters instance types to match the AMI you pick.

Instance type

For learning, t3.micro (2 vCPUs, 1 GiB of memory) is enough to run a small web server, and it is free tier eligible on new accounts. The console labels eligible types in the drop-down.

The “t” family is burstable. It earns CPU credits while idle and spends them when busy, so it suits workloads that are mostly quiet with occasional spikes. Deep Dive into AWS EC2 Instances covers instance families and when to pick something else.

Key pair

The key pair is how you log in over SSH. Choose Create new key pair, give it a name, and pick:

  • Type: ED25519 for Linux instances. It produces shorter keys and is the modern default; RSA also works and is the only type Windows instances accept.
  • Format: .pem for OpenSSH (macOS, Linux and current Windows), or .ppk if you use PuTTY.

Your browser downloads the private key once. AWS does not keep a copy, so if you lose it you cannot download it again. Store it somewhere safe and outside any Git repository.

You can also choose Proceed without a key pair if you plan to connect only through EC2 Instance Connect or Session Manager, both described below.

Network settings

Leave the default VPC and subnet as they are. In the default VPC, every subnet is public and Auto-assign public IP is enabled, so the instance gets a public IPv4 address it can be reached on.

That address is not free. AWS charges hourly for every public IPv4 address, including ones it assigns automatically, so it is part of what a running instance costs.

The wizard proposes creating a new security group with an SSH rule. Change the source from Anywhere (0.0.0.0/0) to My IP. A server with port 22 open to the whole internet is scanned within minutes, and limiting the rule to your own address removes almost all of that noise. If you plan to serve a web page, also tick Allow HTTP traffic from the internet.

Configure storage

The root volume comes from the AMI. For Amazon Linux 2023 it is a gp3 volume, the current general purpose SSD type. The default size is enough for experiments; you can make it larger here, and can grow it later without relaunching.

Under the volume’s advanced details, Delete on termination is enabled by default for the root volume. Leave it on for a lab server so that terminating the instance also removes its disk.

Advanced details and user data

This section has dozens of options, and almost all can stay at their defaults. Two are worth knowing about now:

  • IAM instance profile: a role the instance itself can use to call AWS APIs. You need one with the AmazonSSMManagedInstanceCore policy to use Session Manager.
  • User data: a script that runs as root on the first boot. It is the simplest way to configure a server without logging in.

This user data installs and starts nginx on Amazon Linux 2023, which uses dnf as its package manager:

#!/bin/bash
dnf install -y nginx
systemctl enable --now nginx

If you allowed HTTP in the security group, opening the instance’s public DNS name in a browser a minute after launch shows the nginx welcome page. Use http://, not https://, because nothing is listening on port 443.

Review the summary panel on the right and choose Launch instance. The instance moves from pending to running within a minute or so, and the Status check column turns to “checks passed” shortly after.

Wizard sectionSensible first choiceWhy it matters
Name and tagsName=first-serverFinding and costing the instance later
AMIAmazon Linux 2023Supported, patched, connection tools preinstalled
Instance typet3.microFree tier eligible on new accounts, enough for a small server
Key pairNew ED25519 key, .pemNeeded for plain SSH; not recoverable if lost
NetworkDefault VPC, SSH from My IPPublic address, but port 22 closed to everyone else
StorageDefault gp3 root volume, delete on terminationNo orphaned disks after cleanup
User dataShort setup scriptRepeatable configuration without logging in

The launch wizard offers an SSH rule with source "Anywhere". What is the better choice for a personal test server?

  • Keep Anywhere, since the key pair already protects the login
  • Change the source to My IP
  • Remove the security group entirely

Correct! Restricting SSH to your own address keeps the port invisible to the scanners that probe every public IP, while the key pair still protects the login itself.

Connecting to the instance

There are three common ways to get a shell on a Linux instance. They differ in what they need from the network and from IAM.

MethodNeeds a key pairNeeds inbound port 22Needs a public IPWhere it runs
SSH clientYesYes, from your IPYes, or a VPN or bastionYour terminal
EC2 Instance ConnectNo, a temporary key is pushed for youYes, from the Instance Connect service rangeYes, for the browser optionBrowser or terminal
Session ManagerNoNoNo, outbound HTTPS is enoughBrowser or AWS CLI

SSH with your key

Find the instance’s Public IPv4 DNS on its details page. On macOS or Linux, restrict the key file’s permissions first, because OpenSSH refuses keys that other users can read:

chmod 400 ~/Downloads/first-key.pem
ssh -i ~/Downloads/first-key.pem ec2-user@ec2-203-0-113-25.compute-1.amazonaws.com

The user name depends on the AMI: ec2-user for Amazon Linux and ubuntu for Ubuntu. Logging in as the wrong user produces a “Permission denied (publickey)” error that looks exactly like a wrong key, which is the most common first-day problem. On the first connection, SSH asks you to confirm the host’s fingerprint; type yes.

sequenceDiagram
    participant You as Your laptop
    participant SG as Security group
    participant EC2 as Instance (sshd)
    You->>SG: TCP 22 from your IP
    SG->>EC2: Allowed by inbound rule
    EC2-->>You: Host key
    You->>EC2: Proves it holds the private key
    EC2->>EC2: Checks ~/.ssh/authorized_keys
    EC2-->>You: Shell as ec2-user

EC2 Instance Connect

Select the instance, choose Connect, and use the EC2 Instance Connect tab. AWS pushes a one-time public key to the instance and opens a terminal in your browser, so you don’t need the .pem file.

The catch is the security group. The browser session comes from AWS’s Instance Connect service, not from your IP, so a rule that allows only “My IP” blocks it. Add an inbound SSH rule whose source is the managed prefix list for your Region, named like com.amazonaws.us-east-1.ec2-instance-connect.

Session Manager

Session Manager, part of AWS Systems Manager, needs no open inbound ports, no key pair and no public IP. The SSM Agent on the instance makes an outbound HTTPS connection to the service, and you get a shell through the console or the CLI. It needs an instance profile with AmazonSSMManagedInstanceCore attached, and every session can be logged.

For anything beyond a throwaway lab, it is the better default: you can remove the SSH rule altogether and control access through IAM instead of key files.

An instance's security group has no inbound rules at all. Which method can still give you a shell?

  • SSH with the instance's key pair
  • EC2 Instance Connect in the browser
  • Session Manager, with the right instance profile

Correct! Session Manager relies on the agent's outbound connection, so it works with no inbound rules, as long as the instance role allows it.

The same launch with the AWS CLI

The console is good for learning what the options are. The CLI is how you make a launch repeatable. These commands recreate the setup above in the default VPC, assuming the CLI is configured with credentials and a default Region.

# Key pair: AWS returns the private key once, so save it straight away
aws ec2 create-key-pair \
  --key-name first-key \
  --key-type ed25519 \
  --query KeyMaterial --output text > first-key.pem
chmod 400 first-key.pem

# Security group allowing SSH from your current public IP only
SG_ID=$(aws ec2 create-security-group \
  --group-name first-server-sg \
  --description "SSH from my IP" \
  --query GroupId --output text)
MY_IP=$(curl -s https://checkip.amazonaws.com)
aws ec2 authorize-security-group-ingress \
  --group-id "$SG_ID" --protocol tcp --port 22 --cidr "${MY_IP}/32"

# Launch, resolving the latest Amazon Linux 2023 AMI from a public SSM parameter
INSTANCE_ID=$(aws ec2 run-instances \
  --image-id resolve:ssm:/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64 \
  --instance-type t3.micro \
  --key-name first-key \
  --security-group-ids "$SG_ID" \
  --user-data file://user-data.sh \
  --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=first-server}]' \
  --query 'Instances[0].InstanceId' --output text)

aws ec2 wait instance-running --instance-ids "$INSTANCE_ID"
aws ec2 describe-instances --instance-ids "$INSTANCE_ID" \
  --query 'Reservations[0].Instances[0].PublicDnsName' --output text

Two details are worth pointing out. AMI IDs differ in every Region and change with each release, so instead of hard-coding one, the resolve:ssm: prefix looks up the current Amazon Linux 2023 image from a parameter AWS maintains. And user-data.sh is the same three-line nginx script from the console section; the CLI base64-encodes it for you.

For anything you launch more than a few times, the next step is infrastructure as code with CloudFormation or Terraform, so that the security group, key and instance are created and deleted together.

Stop, start, reboot and terminate

An instance moves through a small set of states, and what you pay for depends on which one it is in.

flowchart LR
    P["pending"] --> R["running"]
    R -->|"reboot"| R
    R -->|"stop"| S["stopped"]
    S -->|"start"| P
    R -->|"terminate"| T["terminated"]
    S -->|"terminate"| T
  • Running: you pay for compute by the second (with a one-minute minimum for Linux), plus storage and the public IPv4 address.
  • Reboot: the operating system restarts on the same host. The instance keeps its public IP and nothing about billing changes.
  • Stopped: compute charges end, but the EBS volumes are kept and billed. The automatically assigned public IP is released, so the instance usually comes back with a different address after a start. If you need a fixed address, allocate an Elastic IP, which is charged like any public IPv4 address.
  • Terminated: the instance is gone for good. Volumes with delete on termination enabled are deleted too; anything else stays and keeps costing money.

Stopping is right when you will need the server again soon. Terminating is right when the experiment is over. If you want protection against terminating the wrong thing, enable termination protection in the instance settings.

You stop your instance overnight and start it in the morning. What changes?

  • It probably has a new public IP address, and its EBS volume was billed overnight
  • Nothing changes, and nothing was billed overnight
  • Its root volume was wiped and recreated from the AMI

Correct! A stop releases the auto-assigned public IPv4 address and keeps the EBS volume, which is billed while the instance is stopped. The data on the volume survives.

Cleaning up

When you’re done, remove everything the launch created, not just the instance. In the console:

  1. Select the instance and choose Instance state, then Terminate (delete) instance.
  2. Under Elastic Block Store, open Volumes and check that nothing you created is left in the available state.
  3. Release any Elastic IP addresses you allocated.
  4. Delete the security group and, if you won’t reuse it, the key pair.

Or with the CLI, using the variables from earlier:

aws ec2 terminate-instances --instance-ids "$INSTANCE_ID"
aws ec2 wait instance-terminated --instance-ids "$INSTANCE_ID"
aws ec2 delete-security-group --group-id "$SG_ID"
aws ec2 delete-key-pair --key-name first-key

The security group can only be deleted once no instance uses it, which is why the wait comes first. A day later, check Cost Explorer or the Bills page to confirm nothing from the experiment is still accruing.

If you are learning more than one cloud, the same exercise on the other two providers is in Launching Your First GCP Compute Engine Instance and Creating Your First Azure Virtual Machine. The concepts map closely, though the names differ.

Further reading

Key takeaways

  • An EC2 instance is assembled from an AMI, an instance type, a subnet, a security group, EBS volumes and usually a key pair, and each has its own lifecycle.
  • Use Amazon Linux 2023 or a current Ubuntu LTS; Amazon Linux 2 reached end of support on June 30, 2026.
  • On accounts created since July 15, 2025, t3.micro and t4g.micro are among the free tier eligible types; t2.micro advice comes from the legacy program.
  • Limit SSH to your own IP, or skip inbound rules entirely and connect with Session Manager.
  • The login user depends on the AMI: ec2-user for Amazon Linux, ubuntu for Ubuntu.
  • Stopping ends compute charges but not storage, and usually changes the public IP. Terminate and delete the leftovers when an experiment is over.

Part 6 of 7 · Getting to Know AWS

  1. 01Introduction to Amazon Web Services (AWS)
  2. 02Navigating the AWS Management Console
  3. 03AWS Regions and Availability Zones
  4. 04AWS Identity and Access Management (IAM)
  5. 05AWS Free Tier and Billing Overview
  6. 06Creating Your First AWS EC2 Instance
  7. 07AWS Support and Documentation Resources
Ready to check your understanding?Take the Fluffy Computing quiz
esc

Type to search all articles · ↑↓ to move · ↵ to open