Amazon EC2 Basics

EC2 gives you virtual servers in the cloud that you can launch, stop and terminate on demand.

Amazon EC2 (Elastic Compute Cloud) provides resizable virtual machines called instances. It is the classic way to run almost any workload on AWS.

The lifecycle of an instance

  • Launch — pick an AMI, instance type, network and key pair.
  • Running — you are billed per second while it runs.
  • Stop — shuts down but keeps the disk (and stops most charges).
  • Terminate — permanently deletes the instance.

What you choose at launch

  1. An AMI (the operating-system image).
  2. An instance type (CPU and memory size).
  3. A key pair (for SSH access).
  4. A security group (the firewall).
  5. A VPC and subnet (the network).

Example

Example · bash
# Launch a small Linux instance
aws ec2 run-instances \
  --image-id ami-0abcd1234efgh5678 \
  --instance-type t3.micro \
  --key-name my-key \
  --security-group-ids sg-0123456789abcdef0 \
  --subnet-id subnet-0a1b2c3d4e5f67890 \
  --count 1

# Later, terminate it
aws ec2 terminate-instances --instance-ids i-0123456789abcdef0

When to use it

  • A web developer launches a t3.medium EC2 instance to host a Node.js API server that serves a mobile app backend.
  • A research team runs computationally intensive simulations on c5.18xlarge instances for hours and terminates them when done, paying per second.
  • A company migrates an on-premises Windows application to EC2 by lifting and shifting it to a Windows Server instance without rewriting code.

More examples

Launch a Basic EC2 Instance

Launches a single t3.micro EC2 instance with a Name tag, the minimal command to get a virtual server running.

Example · bash
aws ec2 run-instances \
  --image-id ami-0abcdef1234567890 \
  --instance-type t3.micro \
  --count 1 \
  --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=web-server}]'

List Running Instances

Lists all currently running EC2 instances with their type and public IP, equivalent to the Console instance list.

Example · bash
aws ec2 describe-instances \
  --filters Name=instance-state-name,Values=running \
  --query 'Reservations[].Instances[].{ID:InstanceId,Type:InstanceType,IP:PublicIpAddress}' \
  --output table

Stop and Terminate Instances

Demonstrates the difference between stopping an instance (preserving it) and terminating it (permanent deletion).

Example · bash
# Stop (instance persists, billing for compute stops)
aws ec2 stop-instances --instance-ids i-0123456789abcdef0

# Terminate (instance and root EBS deleted permanently)
aws ec2 terminate-instances --instance-ids i-0123456789abcdef0

Discussion

  • Be the first to comment on this lesson.