Skip to content

Sprint 2 — Infrastructure Terraform AWS

VPC, EKS, RDS. Terraform state, locks, imports. L'infra se rebelle. đŸ”„


INC-008 — Terraform state lock S3 non libĂ©rĂ©

Contexte : terraform plan/apply
SymptĂŽme :

Error: Error acquiring the state lock
PreconditionFailed: At least one of the pre-conditions did not hold

Cause :

Terraform interrompu (Ctrl+C, redémarrage container)
→ fichier .tflock reste dans S3
→ prochaine opĂ©ration bloquĂ©e

Fix :

# Supprimer le lock manuellement
aws s3 rm s3://BUCKET/fastapi-eks/ephemeral/.tflock
# ou
terraform force-unlock LOCK_ID

Leçon :

Ne jamais interrompre un terraform apply/destroy
Utiliser tmux pour les longues opérations :
tmux new-session -d -s terraform 'terraform apply -auto-approve'
tmux attach -t terraform

INC-009 — Ressources AWS hors state Terraform (ECR, IAM)

Contexte : Restructuration modules → persistent/ephemeral
SymptĂŽme :

RepositoryAlreadyExistsException: ECR repository already exists
EntityAlreadyExists: IAM Role already exists

Cause :

Ressources créées avec l'ancien state (terraform/main/)
Nouveau state (terraform/persistent/) ne les connaĂźt pas
Terraform essaie de les recrĂ©er → conflit AWS

Fix :

# En dev : supprimer et recréer
aws ecr delete-repository --repository-name fastapi-eks/fastapi --force
aws iam delete-role --role-name fastapi-eks-eks-cluster-role

# En prod : toujours terraform import
terraform import module.ecr.aws_ecr_repository.fastapi fastapi-eks/fastapi

Leçon :

Terraform ne connaĂźt que ce qui est dans SON state
RĂšgle d'or : jamais restructurer sans terraform import en production
En dev : terraform destroy → restructurer → terraform apply

INC-010 — EKS Node Group bloquĂ© sans NAT Gateway (27+ min)

Contexte : terraform apply — EKS module
SymptĂŽme :

module.eks.aws_eks_node_group.main: Still creating... [27m36s elapsed]

Cause :

EKS nodes lancent dans des subnets privés
Besoin d'accÚs internet pour télécharger images ECR
Sans NAT Gateway → pas d'internet → nodes timeout

Fix :

resource "aws_nat_gateway" "main" {
  allocation_id = aws_eip.nat.id
  subnet_id     = aws_subnet.public.*.id[0]
}

# Route table privée
route {
  destination_cidr_block = "0.0.0.0/0"
  nat_gateway_id         = aws_nat_gateway.main.id
}

Leçon :

EKS avec subnets privés = NAT Gateway OBLIGATOIRE
Pas de NAT = nodes bloqués, services impossibles
Coût : ~0.045$/h (à destroy le soir !)

INC-011 — ENIs orphelins aprùs terraform destroy

Contexte : terraform destroy (EKS + RDS + VPC)
SymptĂŽme :

DependencyViolation: The network interface 'eni-xxx' is already associated with another allocation

Cause :

ELB créé par Envoy Gateway reste dans la VPC
ENIs = Elastic Network Interfaces rattachées au ELB
Terraform essaie de supprimer VPC avant les ENIs
→ conflit AWS

Fix :

# Nettoyer les ressources K8s AVANT destroy
kubectl delete gateway envoy-gateway-default  # supprime ELB

# Attendre quelques secondes que ELB se libĂšre
sleep 30

# Puis terraform destroy
terraform destroy -auto-approve

Leçon :

Toujours supprimer ressources K8s AVANT terraform destroy
ELB, LoadBalancer services, Gateway API = ENIs
Sans cleanup K8s → ENIs orphelins → destroy bloquĂ©

INC-012 — RDS snapshot non supprimĂ© (coĂ»ts)

Contexte : terraform destroy (RDS module)
SymptĂŽme :

Snapshots RDS toujours facturés 24h aprÚs destroy
InvalidDBInstanceStateFault: Cannot delete DB instance with automated backups

Cause :

RDS configuration : skip_final_snapshot = false
Terraform crée un snapshot avant de supprimer la DB
Snapshot reste dans AWS (coĂ»ts !) mĂȘme aprĂšs destroy

Fix :

resource "aws_db_instance" "postgres" {
  ...
  skip_final_snapshot       = true
  # Ou supprimer manuellement le snapshot
}

Leçon :

En ephemeral Terraform : skip_final_snapshot = true
En production : skip_final_snapshot = false + gestion snapshots
Attention aux coûts cachés (snapshots, EBS volumes, etc)

📊 RĂ©sumĂ© Sprint 2

  • 4 incidents : State lock, imports, NAT Gateway, ENIs orphelins
  • ClĂ© : Comprendre Terraform state, AWS ressources, cleanup order
  • Impact : Montrer que tu sais gĂ©rer l'infrastructure IaC proprement

→ Voir Sprint 3