EC2: Virtual Servers

EC2 (Elastic Compute Cloud) rents you virtual servers, called instances, that you fully control — install anything, run anything, exactly like a physical machine, except billed by the hour or second and destroyed with one command when you're done.

AMIs and instance types

An AMI (Amazon Machine Image) is a template — an operating system plus whatever software is pre-installed on it — that a new instance boots from. An instance type (like t3.micro or m5.large) determines the CPU, memory, and network performance you're renting.

bash terminal
aws ec2 run-instances --image-id ami-0abc123 --instance-type t3.micro --key-name my-key --count 1
Output (abridged)
{
    "Instances": [
        {
            "InstanceId": "i-0a1b2c3d4e5f67890",
            "InstanceType": "t3.micro",
            "State": {"Name": "pending"}
        }
    ]
}

Security groups: the instance-level firewall

A security group controls what traffic is allowed in and out of an instance — by default, everything inbound is blocked until you explicitly open a port:

bash terminal
aws ec2 authorize-security-group-ingress --group-id sg-0123abcd --protocol tcp --port 22 --cidr 203.0.113.4/32
What this does
Allows SSH (port 22) into the instance, but only from the single IP address 203.0.113.4 — not the whole internet.
Security groups are stateful: if you allow inbound traffic on a port, the matching outbound response traffic is automatically allowed back out — you don't need a separate outbound rule for replies. This is different from the older, lower-level Network ACLs, which are stateless and need explicit rules in both directions. Opening port 22 (SSH) or 3389 (RDP) to 0.0.0.0/0 — the entire internet — is one of the most common misconfigurations that leads to a compromised instance.

Stopping vs. terminating

Stopping an instance shuts it down but keeps its storage — you can start it again later, and you stop paying for compute (though storage still costs a little). Terminating destroys it permanently, storage included. Get in the habit of stopping instances you're not actively using — the next lesson's cost course will make clear why that matters.