Launch Your First EC2 Instance

Launch and connect to your first EC2 instance — AWS Cloud & DevOps with Python.

Focus: launch and connect to your first ec2 instance

Sponsored

You've built Python apps locally and pushed them to GitHub — but when you try to share a demo or run a scheduled job, your laptop has to stay on. That's the pain this lesson solves. By the end, you'll have a real, always-on Linux server in the cloud — an Amazon EC2 instance — and you'll know how to launch and connect to your first EC2 instance using nothing more than a terminal and good habits. No more fighting with hosting panels or wondering what a 'server' actually means.

The problem this lesson solves

In the last few lessons in this track, you worked with AWS accounts, the CLI, and IAM users. But your Python code still runs on your machine. That's fine for development, but it fails in production for three reasons:

  • Your computer sleeps. When you close the lid, your app or cron job dies.
  • Your IP is dynamic. Sharing a localhost URL with a friend is awkward — and often blocked by firewalls.
  • You can't scale. If 10,000 users hit your Flask API, a single laptop CPU isn't enough — and you shouldn't buy a beefier laptop for that.

AWS Elastic Compute Cloud (EC2) gives you a virtual server — an instance — that runs 24/7 inside Amazon's data centers. You pay by the second, you can resize it, and you can destroy it and start over in minutes. This is the foundation of almost every modern DevOps workflow: launch a server, configure it, deploy code, and monitor it.

This lesson is the gateway to every later topic — load balancers, autoscaling, CI/CD, and infrastructure as code. Without a working EC2 instance, none of that makes sense.

Core concept / mental model

Think of EC2 like renting a laptop from a cloud that never sleeps. You pick the hardware (CPU, RAM, disk), you pick the operating system (usually Linux), you turn it on, and you pay only for the time it runs.

Here's the mental model you need to internalize:

  • Amazon Machine Image (AMI) — a blueprint of the operating system plus software. Like an ISO you'd use to install Linux.
  • Instance type — the hardware spec. t2.micro is a small, free-tier-eligible instance (1 vCPU, 1 GiB RAM).
  • Key pair — a pair of cryptographic keys (public and private). The public key is embedded in the instance; your private key lets you SSH in securely.
  • Security Group — a virtual firewall attached to the instance. It controls which ports are open to the world (e.g., port 22 for SSH, port 80 for HTTP).
  • Public IP / Elastic IP — the instance gets a public IPv4 address so you can reach it from anywhere.
Your laptop --SSH via private key--> EC2 instance (public IP)
                                   |
                                   +-- Security Group (allows port 22)
                                   +-- AMI (Amazon Linux, Ubuntu, etc.)
                                   +-- Instance type (t2.micro)

This is the compute layer of AWS. Other services (like S3 for storage or Lambda for serverless) are separate, but EC2 is the classic go-to for any long-running service.

How it works step by step

Launching an EC2 instance involves a few distinct steps. Once you understand the sequence, you'll be able to repeat it manually or automate it with Python or Terraform (later in this track).

1. Choose an AMI

The AMI defines the operating system and software. For a general-purpose Python server, Amazon Linux 2023 or Ubuntu 22.04 LTS are excellent choices. Both are free-tier eligible.

2. Pick an instance type

For the free tier, t2.micro (or t3.micro in some regions) is the default. It's enough for small apps, experiments, and learning. Later you can upgrade to larger types like t3.medium or even GPU instances.

3. Configure the instance

Here you decide how many instances to launch, whether to enable public IP assignment, and whether to add storage (the default 8 GiB EBS volume is fine).

4. Add storage

EC2 instances use Elastic Block Store (EBS) for persistent disk. The root volume is where your OS and data live. Default is 8 GiB — enough for practice.

5. Add tags

Tags are key-value pairs that help you organize and track resources. A simple tag like Name=my-first-instance is a best practice you'll see everywhere.

6. Configure the security group

This is the firewall. For our purpose, create a new security group and allow SSH on port 22 from your IP address only. Never open port 22 to the whole world (0.0.0.0/0) unless you're absolutely sure — automated bots will try to brute-force it.

7. Create a key pair

You'll need a private key file (.pem on Linux/Mac, or .ppk for PuTTY on Windows) to SSH into the instance. AWS keeps the public key; you keep the private key. If you lose it, you can't connect — there's no way to recover it.

8. Launch and connect

Click Launch — the instance starts in minutes. Then use SSH to connect from your local machine, using the private key and the public IP.

Hands-on walkthrough

Let's do this in practice. You have two options: the AWS Console (good for a first time) or the AWS CLI (the DevOps way — and we'll use Python later).

Option A: AWS Console (beginner-friendly)

  1. Go to the EC2 console.
  2. Click Launch instance.
  3. Name it first-python-server.
  4. Choose Amazon Linux 2023 AMI (free tier eligible).
  5. Keep the default t2.micro instance type.
  6. Enable Auto-assign public IP (usually on by default).
  7. In Key pair, click Create new key pair. Name it python-key and download the .pem file. Store it in ~/.ssh/ and set permissions:
chmod 400 ~/.ssh/python-key.pem
  1. In Network settings, click Edit, then Create security group. Add a rule: type SSH, source My IP. If your IP changes (home vs. office), you'll need to update this later.
  2. Click Launch instance.

Wait 1–2 minutes until the instance status shows Running. Select the instance in the console and copy its Public IPv4 address.

Option B: AWS CLI (the DevOps way)

If you have the AWS CLI configured (from earlier lessons), you can launch via terminal:

# 1. Find the latest Amazon Linux 2023 AMI ID (adjust region)
aws ec2 describe-images \
  --owners amazon \
  --filters "Name=name,Values=al2023-ami-*-kernel-6.1-x86_64" "Name=state,Values=available" \
  --query 'Images[0].ImageId' \
  --output text

Expected output (a snippet):

ami-0abcdef1234567890
# 2. Create a security group
aws ec2 create-security-group \
  --group-name python-sg \
  --description "SSH access for Python server" \
  --output text
# 3. Allow SSH from my IP
aws ec2 authorize-security-group-ingress \
  --group-name python-sg \
  --protocol tcp \
  --port 22 \
  --cidr $(curl -s ifconfig.me)/32
# 4. Launch the instance
aws ec2 run-instances \
  --image-id ami-0abcdef1234567890 \
  --instance-type t2.micro \
  --key-name python-key \
  --security-groups python-sg \
  --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=first-python-server}]' \
  --output json

The output will include InstanceId — save it for later.

Connect via SSH

Your private key is python-key.pem, and your public IP is, say, 54.123.45.67. Now you connect:

ssh -i ~/.ssh/python-key.pem ec2-user@54.123.45.67

Expected output (first time):

The authenticity of host '54.123.45.67 (54.123.45.67)' can't be established.
...
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes

Type yes and press Enter. You'll see a shell prompt like:

[ec2-user@ip-172-31-44-123 ~]$

Congratulations — you've just launched and connected to your first EC2 instance! Now let's verify Python is available:

python3 --version

Expected output:

Python 3.9.16

That's your server. You can install anything you want:

sudo yum update -y
sudo yum install -y git

Pro tip: For Ubuntu instances, the SSH user is ubuntu instead of ec2-user. Check your AMI's documentation.

Compare options / when to choose what

You now know two ways to launch an instance — but there are more. Here's a comparison to help you choose in real projects:

Method Pros Cons Best for
AWS Console Visual, quick, good for learning Manual, error-prone First-time launch, quick tests
AWS CLI Scriptable, repeatable Need to remember flags Automation, DevOps scripts
Python boto3 + Pulumi Full programmatic control Requires coding knowledge Infrastructure as code
Terraform / CloudFormation Declarative, version-controlled Learning curve Production, team environments

For this lesson, the console is fine. But as a Python developer, you'll soon want to automate — that's where boto3 comes in. Here's a taste:

import boto3

# Create EC2 client
ec2 = boto3.client('ec2', region_name='us-east-1')

# Describe instances (just to see what's running)
response = ec2.describe_instances()
for reservation in response['Reservations']:
    for instance in reservation['Instances']:
        print(f"Instance ID: {instance['InstanceId']}, State: {instance['State']['Name']}")

Run this locally (after pip install boto3) and you'll see your running instances.

When to choose what? Use the console for one-off experiments. Use CLI or boto3 when you need to launch 10 instances or want to reproduce an environment. Use Terraform when your team manages everything as code.

Troubleshooting & edge cases

Here are the most common failures and how to fix them.

"Permission denied (publickey)" when using SSH

This usually means the key file has wrong permissions. Fix it:

chmod 400 ~/.ssh/python-key.pem

"Connection timed out"

Your security group doesn't allow SSH from your current IP. Go to EC2 console → Security Groups → your SG → Edit inbound rules → add SSH from My IP. Your home IP changes when you reconnect. You can also check with curl ifconfig.me.

Wrong username

If you launch Ubuntu and try ec2-user, you'll get a rejection. Use ubuntu instead:

ssh -i ~/.ssh/python-key.pem ubuntu@YOUR_IP

Instance is running but no public IP

You may have forgotten to enable Auto-assign public IP, or you launched a VPC without internet gateway. Easiest fix: stop the instance, detach and re-launch with public IP enabled, or allocate an Elastic IP and associate it.

Lost your private key

There is no way to recover a lost key pair. You must stop the instance (or terminate it), create a new key pair, and launch a new instance. That's why key management is critical.

Billing shock — instance left running

Even a t2.micro costs money if you run it past the free tier hours (750 hours/month). Always Stop (not terminate) if you want to keep it but not pay — stopping is free. Terminate deletes it permanently.

Pro tip: Use tags like Env=dev and set up a budget alert in AWS to avoid surprise bills.

What you learned & what's next

You've now launched and connected to your first EC2 instance — a core skill that underpins everything else in this AWS Cloud & DevOps with Python track. You understand:

  • The role of AMIs, instance types, key pairs, and security groups.
  • How to launch an instance via the console or CLI.
  • How to connect via SSH and verify Python is installed.
  • How to troubleshoot common connectivity and access errors.

This is the foundation for the next lesson: Deploying a Python app to EC2. You'll take this empty server and turn it into a live web service — using the same SSH connection and commands we just practiced, plus a bit of systemd or Docker. That's where your DevOps skills really start to shine.

Now stop your instance (to save money) or leave it running and get ready to deploy something real. The cloud is your oyster — go build!

Practice recap

Launch a second instance using the AWS CLI with a Name tag and your IAM credentials. Connect via SSH, create a simple file with echo, and disconnect. Then stop the instance to save costs. This hands-on practice cements the CLI workflow before you move on to deploying a real Python app.

Common mistakes

  • Forgetting to chmod 400 on the .pem key file — SSH will refuse with 'permissions too open'. Always set permissions immediately after download.
  • Opening SSH (port 22) to the entire internet (0.0.0.0/0) — bots will brute-force your server. Restrict to your IP address only.
  • Using the wrong SSH username for the AMI (e.g., ec2-user on Ubuntu instead of ubuntu) — you'll get 'Permission denied, please try again'.
  • Losing the private key — there is no recovery. You must terminate the instance and launch a new one. Store a backup in a secure place.
  • Leaving the instance running idle — it accumulates costs even when unused. Stop it when not in use; the free tier won't cover you forever.

Variations

  1. Use the AWS Console for a visual, point-and-click launch — great for first-timers learning the EC2 interface.
  2. Use the AWS CLI for scripted, repeatable launches without the GUI — ideal once you understand the console flow.
  3. Use Python with boto3 for programmatic control—combined with tools like Pulumi, you can manage infrastructure as code.

Real-world use cases

  • Deploy a Flask or FastAPI web application to an EC2 instance and expose it to the internet on port 80.
  • Run a nightly data-processing script (cron job) on EC2 that fetches data from an external API and writes results to S3.
  • Host a development or staging environment for your team, isolated from your local machine and easily discardable.

Key takeaways

  • EC2 launches virtual servers from an AMI, sized by instance type, secured by key pairs and security groups.
  • Launching can be done via console, CLI, or Python/boto3 — pick the method that matches your workflow.
  • SSH requires the correct private key file permissions (chmod 400) and the right username (e.g., ec2-user or ubuntu).
  • Troubleshooting connection issues almost always involves checking the security group's inbound rules and your current IP address.
  • Stop idle instances to avoid unnecessary charges; terminate them when you're done forever.
  • Mastering EC2 is the prerequisite for more advanced topics like autoscaling, load balancing, and infrastructure as code.

Sponsored

Sponsored

Discussion

Questions, corrections, and tips help everyone reading this page.

0 comments

Add a comment

Shown publicly with your comment.

Be constructive · max 4,000 characters

No comments yet — start the thread.

Related tutorials, quizzes, and articles for this topic.