An EC2 instance never goes alone. To launch it, you always need three pieces that always accompany it: an AMI (which disk it starts from), a key pair (how you enter securely), and a Security Group (what traffic you allow). Understanding these three is essential before launching your first server.

The AMI: your instance's template

AMI stands for Amazon Machine Image. It is a template that contains the operating system and, optionally, pre-installed software, from which your instance is created.

Analogy: An AMI is like a cake mold or a factory photo of a computer. When you launch an instance, AWS "makes a copy" of that image and starts it up. If you launch 10 instances from the same AMI, all 10 start up identically.

What an AMI contains:

  • The operating system (Amazon Linux, Ubuntu, Windows Server…).
  • Initial configurations.
  • Optionally, already installed software (a web server, your application, etc.).

Types of AMIs you will find:

Type Who makes it Example
AWS AMIs Amazon Amazon Linux 2023, Ubuntu, Windows Server
Marketplace AMIs Third parties (software vendors) An AMI with WordPress already set up
Custom AMIs You Your pre-installed app, ready to clone

Pro tip: Creating your own AMI with your software already installed makes launching new instances instant and repeatable. This is key for autoscaling (Chapter 13): instead of installing everything at each startup, you start from an image that's already ready. Tools like Packer (from HashiCorp, same as Terraform) are used to build AMIs automatically.

The key pair: your secure access key

To enter your instance (via SSH on Linux or RDP on Windows) you need to authenticate. AWS uses key pairs based on public key cryptography.

A key pair has two parts:

  • Public key: AWS stores it inside the instance. It's like a lock.
  • Private key: you download it and only you have it. It's the key that opens that lock.
[Your private key]  πŸ”‘ ----opens----> πŸ”’ [Public key in the instance]
   (you keep it,                        (AWS places it when creating
    never share it)                      the instance)

How it works when you connect:

  1. You launch an instance and associate a key pair with it.
  2. AWS places the public key inside.
  3. To connect, you use your private key. The lock (public) only opens with that specific key (private).

⚠️ Critical security rule: AWS lets you download the private key only once when you create it. If you lose it, you cannot recover it and you will lose access to the instances that depend on it. Store it in a safe place and never upload it to a Git repository or share it via chat or email.

The Security Group: your instance's firewall

A Security Group is a virtual firewall that controls what network traffic can enter and leave your instance. It is your first and most important line of defense.

Analogy: It's like the bouncer at a nightclub with a list. Only those on the list are allowed in; everyone else is blocked.

Key features of Security Groups:

  • They work with inbound rules and outbound rules.
  • They only allow whitelists (allow): you define what is allowed; everything else is blocked by default.
  • They are stateful: if you allow an incoming connection, the outgoing response is automatically allowed. You don't have to duplicate rules.

Example rules for a web server:

Type Port Source Purpose
HTTP 80 0.0.0.0/0 (all internet) So anyone can view the website
HTTPS 443 0.0.0.0/0 (all internet) Secure web (SSL)
SSH 22 Only your IP So only you can administer the server

⚠️ Very common beginner mistake: opening port 22 (SSH) or 3389 (RDP) to 0.0.0.0/0 (all internet). That means anyone in the world can try to access your server. Always limit administrative access to your IP or a trusted network. It's one of the most exploited security flaws.

0.0.0.0/0 means "any IP address on the internet." It's fine for public web ports (80/443), but dangerous for admin ports.

How the three pieces fit together

When you launch an instance, you define all three at once:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚           EC2 Instance              β”‚
β”‚                                     β”‚
β”‚  Based on:  AMI (system + software) β”‚
β”‚  Enter with: Key pair (your key)    β”‚
β”‚  Protected by: Security Group       β”‚
β”‚                 (firewall)          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
  • The AMI decides what the instance is based on.
  • The key pair decides who can enter.
  • The Security Group decides what network traffic is allowed.

What you should remember

  • AMI: template (operating system + software) from which the instance is created. Creating your own AMIs makes deployment fast and repeatable.
  • Key pair: public/private key pair to connect securely. The private key is downloaded only once; store it well and never share it.
  • Security Group: virtual firewall with inbound/outbound rules, whitelist type, and stateful.
  • Mistake to avoid: opening SSH (22) or RDP (3389) to the entire internet. Always limit it to your IP.

In the next subchapter we will see the lifecycle of an instance: the different states (running, stopped, terminated) and what each one implies, including for billing.

Cloud, AWS & Terraform β€” From Zero to Expert

Chapter 1 Β· What is cloud computing

Chapter 2 Β· The cloud market and major providers

Chapter 3 Β· Regions, availability zones and edge

Chapter 4 Β· Compute: EC2

Chapter 5 Β· Storage: S3

Chapter 6 Β· Networking: VPC

Chapter 7 Β· Identity and access: IAM

Chapter 8 Β· Managed databases

Chapter 9 Β· Why Infrastructure as Code

Chapter 10 Β· HCL: the Terraform language

Chapter 11 Β· Providers and state

Chapter 12 Β· Your first real infrastructure in Terraform

Chapter 13 Β· Load balancing and auto scaling

Chapter 14 Β· Serverless with Lambda

Chapter 15 Β· Messaging and events

Chapter 16 Β· Content delivery and DNS

Chapter 17 Β· Containers on AWS

Chapter 18 Β· Modules: reuse and composition

Chapter 19 Β· Workspaces and environment management

Chapter 20 Β· Remote backends and locking

Chapter 21 Β· Infrastructure testing

Chapter 22 Β· Terraform in CI/CD

Chapter 23 Β· Defense in depth

Chapter 24 Β· Observability: logs, metrics and traces

Chapter 25 Β· Cost optimization

Chapter 26 Β· High availability and disaster recovery

Chapter 27 Β· AWS Well-Architected Framework

Chapter 28 Β· Serverless architectures at scale

Chapter 29 Β· Data platforms on AWS

Chapter 30 Β· Multi-account and landing zones

Chapter 31 Β· Platform Engineering and Internal Developer Platform

Chapter 32 Β· Relevant AWS certifications

Chapter 33 Β· Projects to consolidate what you've learned

Chapter 34 Β· Resources and community

© Copyright 2024. All rights reserved