Internet Gateway et NAT Gateway
Internet Gateway (IGW)
Qu'est-ce qu'un Internet Gateway ?
Un Internet Gateway (IGW) est la porte d'entrée/sortie entre votre VPC et Internet. Sans IGW, votre VPC est complètement isolé d'Internet.
┌─────────────────────────────────────────────────────────────┐
│ INTERNET GATEWAY │
├─────────────────────────────────────────────────────────────┤
│ │
│ INTERNET │
│ │ │
│ │ │
│ ┌──────▼──────┐ │
│ │ Internet │ │
│ │ Gateway │ │
│ │ (IGW) │ │
│ └──────┬──────┘ │
│ │ │
│ ┌──────────────────────┼──────────────────────┐ │
│ │ VPC │ │ │
│ │ │ │ │
│ │ ┌──────────────────▼───────────────────┐ │ │
│ │ │ │ │ │
│ │ │ Ressources avec IP publique │ │ │
│ │ │ ┌─────────┐ │ │ │
│ │ │ │ EC2 │ │ │ │
│ │ │ │ (public)│ │ │ │
│ │ │ └─────────┘ │ │ │
│ │ │ │ │ │
│ │ └───────────────────────────────────────┘ │ │
│ │ │ │
│ └──────────────────────────────────────────────┘ │
│ │
│ Caractéristiques : │
│ • Hautement disponible (géré par AWS) │
│ • Pas de limite de bande passante │
│ • 1 IGW par VPC maximum │
│ • Gratuit (pas de frais pour l'IGW lui-même) │
│ │
└─────────────────────────────────────────────────────────────┘
Fonctionnement de l'IGW
┌─────────────────────────────────────────────────────────────┐
│ FONCTIONNEMENT IGW │
├─────────────────────────────────────────────────────────────┤
│ │
│ TRAFIC SORTANT (Instance → Internet) │
│ ──────────────────────────────────── │
│ │
│ ┌─────────┐ ┌─────┐ ┌─────────┐ │
│ │Instance │ ──────► │ IGW │ ──────► │Internet │ │
│ │10.0.1.5 │ │ │ │ │ │
│ └─────────┘ └─────┘ └─────────┘ │
│ │ │
│ Translation NAT │
│ 10.0.1.5 → 52.1.2.3 │
│ (IP privée → IP publique) │
│ │
│ TRAFIC ENTRANT (Internet → Instance) │
│ ──────────────────────────────────── │
│ │
│ ┌─────────┐ ┌─────┐ ┌─────────┐ │
│ │Instance │ ◄────── │ IGW │ ◄────── │Internet │ │
│ │10.0.1.5 │ │ │ │52.1.2.3 │ │
│ └─────────┘ └─────┘ └─────────┘ │
│ │ │
│ Translation NAT │
│ 52.1.2.3 → 10.0.1.5 │
│ (IP publique → IP privée) │
│ │
└─────────────────────────────────────────────────────────────┘
Créer et Attacher un IGW
# 1. Créer l'Internet Gateway
aws ec2 create-internet-gateway \
--tag-specifications 'ResourceType=internet-gateway,Tags=[{Key=Name,Value=mon-igw}]'
# 2. Attacher au VPC
aws ec2 attach-internet-gateway \
--internet-gateway-id igw-xxx \
--vpc-id vpc-xxx
# 3. Ajouter la route dans la Route Table publique
aws ec2 create-route \
--route-table-id rtb-xxx \
--destination-cidr-block 0.0.0.0/0 \
--gateway-id igw-xxx
Conditions pour Accès Internet (Instance)
┌─────────────────────────────────────────────────────────────┐
│ CONDITIONS POUR ACCÈS INTERNET │
├─────────────────────────────────────────────────────────────┤
│ │
│ Pour qu'une instance accède à Internet, il faut : │
│ │
│ ☐ 1. VPC a un Internet Gateway attaché │
│ │
│ ☐ 2. Route Table du subnet a une route vers IGW │
│ Destination: 0.0.0.0/0 → Target: igw-xxx │
│ │
│ ☐ 3. Instance a une IP publique ou Elastic IP │
│ (sinon pas de retour possible) │
│ │
│ ☐ 4. Security Group autorise le trafic sortant │
│ (par défaut : tout autorisé) │
│ │
│ ☐ 5. NACL autorise le trafic (entrée ET sortie) │
│ (par défaut : tout autorisé) │
│ │
│ Si UN SEUL manque = Pas d'accès Internet ! │
│ │
└─────────────────────────────────────────────────────────────┘
NAT Gateway
Qu'est-ce qu'un NAT Gateway ?
Un NAT Gateway permet aux instances dans des subnets privés d'accéder à Internet (sortant uniquement) sans être accessibles depuis Internet.
┌─────────────────────────────────────────────────────────────┐
│ NAT GATEWAY │
├─────────────────────────────────────────────────────────────┤
│ │
│ INTERNET │
│ │ │
│ ┌──────▼──────┐ │
│ │ IGW │ │
│ └──────┬──────┘ │
│ │ │
│ ┌──────────────────────┼──────────────────────┐ │
│ │ VPC │ │ │
│ │ │ │ │
│ │ ┌──────────────────▼───────────────────┐ │ │
│ │ │ SUBNET PUBLIC │ │ │
│ │ │ │ │ │
│ │ │ ┌─────────────┐ │ │ │
│ │ │ │ NAT Gateway │ │ │ │
│ │ │ │ (52.1.2.99) │ │ │ │
│ │ │ └──────┬──────┘ │ │ │
│ │ │ │ │ │ │
│ │ └──────────────────┼────────────────────┘ │ │
│ │ │ │ │
│ │ ┌──────────────────▼───────────────────┐ │ │
│ │ │ SUBNET PRIVÉ │ │ │
│ │ │ │ │ │
│ │ │ ┌─────────┐ │ │ │
│ │ │ │ EC2 │ │ │ │
│ │ │ │(10.0.11.5) │ │ │
│ │ │ └─────────┘ │ │ │
│ │ │ │ │ │
│ │ │ Route : 0.0.0.0/0 → nat-xxx │ │ │
│ │ │ │ │ │
│ │ └───────────────────────────────────────┘ │ │
│ │ │ │
│ └──────────────────────────────────────────────┘ │
│ │
│ Flux : EC2 privé → NAT GW → IGW → Internet │
│ Pas de retour initié depuis Internet ! │
│ │
└─────────────────────────────────────────────────────────────┘
NAT Gateway vs NAT Instance
| Caractéristique | NAT Gateway | NAT Instance |
|---|---|---|
| Disponibilité | Haute (géré AWS) | Vous gérez |
| Bande passante | Jusqu'à 45 Gbps | Dépend du type |
| Maintenance | AWS | Vous |
| Coût | Plus cher | Moins cher |
| Recommandation | ✅ Production | ❌ Legacy |
Créer un NAT Gateway
# 1. Allouer une Elastic IP
aws ec2 allocate-address --domain vpc
# 2. Créer le NAT Gateway (dans subnet PUBLIC)
aws ec2 create-nat-gateway \
--subnet-id subnet-public-xxx \
--allocation-id eipalloc-xxx \
--tag-specifications 'ResourceType=natgateway,Tags=[{Key=Name,Value=nat-gw-1a}]'
# 3. Ajouter route dans Route Table PRIVÉE
aws ec2 create-route \
--route-table-id rtb-private-xxx \
--destination-cidr-block 0.0.0.0/0 \
--nat-gateway-id nat-xxx
Coût du NAT Gateway
┌─────────────────────────────────────────────────────────────┐
│ COÛT NAT GATEWAY │
├─────────────────────────────────────────────────────────────┤
│ │
│ FRAIS HORAIRES │
│ • ~$0.045/heure par NAT Gateway │
│ • ~$32/mois si actif 24/7 │
│ │
│ FRAIS DE DONNÉES │
│ • ~$0.045/GB traité │
│ │
│ Exemple mensuel : │
│ • 1 NAT Gateway : $32 │
│ • 100 GB transfert : $4.50 │
│ • Total : $36.50/mois │
│ │
│ ⚠️ ASTUCE ÉCONOMIE : │
│ • Pour dev/test : utiliser NAT Instance (moins cher) │
│ • Ou : VPC Endpoints pour S3/DynamoDB (gratuit) │
│ • Ou : Subnet public pour instances qui ont besoin │
│ d'Internet │
│ │
└─────────────────────────────────────────────────────────────┘
NAT Gateway Haute Disponibilité
┌─────────────────────────────────────────────────────────────┐
│ NAT GATEWAY HAUTE DISPONIBILITÉ │
├─────────────────────────────────────────────────────────────┤
│ │
│ Un NAT Gateway = Une AZ │
│ Pour la HA, créer un NAT GW par AZ │
│ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ VPC │ │
│ │ │ │
│ │ ┌───────────────┐ ┌───────────────┐ │ │
│ │ │ AZ 1a │ │ AZ 1b │ │ │
│ │ │ │ │ │ │ │
│ │ │ ┌───────────┐ │ │ ┌───────────┐ │ │ │
│ │ │ │ NAT GW 1a │ │ │ │ NAT GW 1b │ │ │ │
│ │ │ └─────┬─────┘ │ │ └─────┬─────┘ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ ┌─────▼─────┐ │ │ ┌─────▼─────┐ │ │ │
│ │ │ │ Private │ │ │ │ Private │ │ │ │
│ │ │ │ Subnet 1a │ │ │ │ Subnet 1b │ │ │ │
│ │ │ │ │ │ │ │ │ │ │ │
│ │ │ │ rtb: → │ │ │ │ rtb: → │ │ │ │
│ │ │ │ nat-1a │ │ │ │ nat-1b │ │ │ │
│ │ │ └───────────┘ │ │ └───────────┘ │ │ │
│ │ │ │ │ │ │ │
│ │ └───────────────┘ └───────────────┘ │ │
│ │ │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
│ Si NAT GW 1a tombe → Subnets privés 1a perdent Internet │
│ Mais subnets privés 1b continuent de fonctionner │
│ │
└─────────────────────────────────────────────────────────────┘
Elastic IP
Qu'est-ce qu'une Elastic IP ?
Une Elastic IP est une adresse IPv4 publique statique que vous pouvez associer à vos ressources.
┌─────────────────────────────────────────────────────────────┐
│ ELASTIC IP │
├─────────────────────────────────────────────────────────────┤
│ │
│ IP Publique Normale Elastic IP │
│ ──────────────────── ────────── │
│ │
│ • Assignée par AWS • Vous l'allouez │
│ • Change si Stop/Start • Reste fixe │
│ • Gratuite si utilisée • Gratuite si associée │
│ • Perdue si terminée • Conservée après terminate │
│ │
│ Cas d'usage Elastic IP : │
│ • Serveur qui doit garder la même IP │
│ • NAT Gateway (obligatoire) │
│ • Failover rapide entre instances │
│ │
│ ⚠️ COÛT : EIP non associée = ~$3.60/mois │
│ (AWS facture les EIP non utilisées !) │
│ │
└─────────────────────────────────────────────────────────────┘
Schéma Complet
┌─────────────────────────────────────────────────────────────────────────┐
│ ARCHITECTURE RÉSEAU COMPLÈTE │
├─────────────────────────────────────────────────────────────────────────┤
│ │
│ INTERNET │
│ │ │
│ ┌──────▼──────┐ │
│ │ IGW │ │
│ └──────┬──────┘ │
│ │ │
│ ┌──────────────────────────────┼──────────────────────────────┐ │
│ │ VPC │ (10.0.0.0/16) │ │
│ │ │ │ │
│ │ ┌──────────────────────────┴────────────────────────────┐ │ │
│ │ │ SUBNETS PUBLICS │ │ │
│ │ │ Route: 0.0.0.0/0 → IGW │ │ │
│ │ │ │ │ │
│ │ │ ┌─────────────────┐ ┌─────────────────┐ │ │ │
│ │ │ │ Public 1a │ │ Public 1b │ │ │ │
│ │ │ │ 10.0.1.0/24 │ │ 10.0.2.0/24 │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ ┌───────────┐ │ │ ┌───────────┐ │ │ │ │
│ │ │ │ │ NAT GW │ │ │ │ NAT GW │ │ │ │ │
│ │ │ │ │ 1a │ │ │ │ 1b │ │ │ │ │
│ │ │ │ └─────┬─────┘ │ │ └─────┬─────┘ │ │ │ │
│ │ │ └────────┼────────┘ └────────┼────────┘ │ │ │
│ │ │ │ │ │ │ │
│ │ └───────────┼──────────────────────────┼─────────────────┘ │ │
│ │ │ │ │ │
│ │ ┌───────────┼──────────────────────────┼─────────────────┐ │ │
│ │ │ │ SUBNETS PRIVÉS │ │ │ │
│ │ │ │ Route: 0.0.0.0/0 → NAT │ │ │ │
│ │ │ │ │ │ │ │
│ │ │ ┌────────▼────────┐ ┌────────▼────────┐ │ │ │
│ │ │ │ Private 1a │ │ Private 1b │ │ │ │
│ │ │ │ 10.0.11.0/24 │ │ 10.0.12.0/24 │ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ │ ┌───────────┐ │ │ ┌───────────┐ │ │ │ │
│ │ │ │ │ EC2 │ │ │ │ EC2 │ │ │ │ │
│ │ │ │ │ App │ │ │ │ App │ │ │ │ │
│ │ │ │ └───────────┘ │ │ └───────────┘ │ │ │ │
│ │ │ └─────────────────┘ └─────────────────┘ │ │ │
│ │ │ │ │ │
│ │ └────────────────────────────────────────────────────────┘ │ │
│ │ │ │
│ └───────────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────┘
Résumé
| Composant | Fonction | Coût |
|---|---|---|
| IGW | Accès Internet bidirectionnel | Gratuit |
| NAT Gateway | Sortie Internet pour privé | ~$32/mois + data |
| Elastic IP | IP publique fixe | Gratuit si utilisée |
| NAT Instance | Alternative NAT (legacy) | Coût EC2 |
Quiz de Validation
- À quoi sert un Internet Gateway ?
- Pourquoi un NAT Gateway est-il nécessaire pour les subnets privés ?
- Dans quel type de subnet doit être le NAT Gateway ?
- Combien coûte une Elastic IP non utilisée ?
- Comment assurer la HA pour les NAT Gateways ?
Prochaine section : Lab : Schéma VPC