Terraform für Anfänger: Infrastructure as Code
Manuelle Cloud-Infrastruktur ist fehleranfällig, nicht reproduzierbar und schwer zu skalieren. Terraform löst dieses Problem: Infrastructure as Code (IaC). Dieser Guide bringt Ihnen die Grundlagen bei.
Was ist Terraform?
Terraform (von HashiCorp) ist ein Open-Source-Tool, mit dem Sie Cloud-Infrastruktur als Code definieren. Statt in der AWS-Konsole herumzuklicken, schreiben Sie Konfigurationsdateien, die Server, Netzwerke und Datenbanken beschreiben.
Vorteile:
Installation
Linux/macOS
``bashMit Homebrew (macOS/Linux)
brew install terraform
Oder direkter Download
Siehe https://developer.hashicorp.com/terraform/downloads
`
Windows
`powershell
choco install terraform
`Verify:
`bash
terraform --version
`Grundkonzepte
1. Provider
Provider sind Plugins, die mit Cloud-Anbietern kommunizieren:
`hcl
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}provider "aws" {
region = "eu-central-1"
}
`2. Resources
Resources sind die eigentlichen Infrastruktur-Objekte:
`hcl
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t3.micro" tags = {
Name = "WebServer"
}
}
`3. Data Sources
Daten, die bereits existieren (z. B. AMIs, VPCs):
`hcl
data "aws_ami" "latest_amazon_linux" {
most_recent = true
owners = ["amazon"] filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-gp2"]
}
}
`4. Variables
Parameterisieren Sie Ihre Konfiguration:
`hcl
variable "instance_count" {
default = 2
type = number
}variable "environment" {
default = "staging"
type = string
}
`5. Outputs
Werte, die nach dem Apply zurückgegeben werden:
`hcl
output "instance_ips" {
value = aws_instance.web[*].public_ip
}
`Der Terraform-Workflow
Schritt 1: Init
`bash
terraform init
`Lädt Provider herunter und initialisiert das Working-Directory.
Schritt 2: Plan
`bash
terraform plan
`Zeigt, was Terraform tun würde, ohne es zu tun. Lesen Sie den Output sorgfältig!
Schritt 3: Apply
`bash
terraform apply
`Führt die Änderungen aus. Bestätigung erforderlich (oder
terraform apply -auto-approve).Schritt 4: Destroy
`bash
terraform destroy
`Löscht alle Ressourcen. Vorsicht: Das ist destruktiv!
Ein praktisches Beispiel: EC2-Instanz auf AWS
`hcl
main.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = "eu-central-1"
}
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t3.micro"
key_name = "my-keypair"
vpc_security_group_ids = [aws_security_group.web.id]
user_data = <<-EOF
#!/bin/bash
apt update && apt install -y nginx
EOF
tags = {
Name = "WebServer"
Environment = "production"
}
}
resource "aws_security_group" "web" {
name = "allow-http-https"
description = "Allow HTTP and HTTPS"
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
output "public_ip" {
value = aws_instance.web.public_ip
}
`Terraform State
Terraform speichert den Zustand in
terraform.tfstate. Diese Datei ist essenziell — ohne sie weiß Terraform nicht, was existiert.Best Practices:
Remote State: Nie lokal speichern (produktiv). Nutzen Sie S3 + DynamoDB Locking: `hcl
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "prod/terraform.tfstate"
region = "eu-central-1"
encrypt = true
dynamodb_table = "terraform-locks"
}
}
`Nie manuell editieren — immer nur über Terraform-Befehle
Nicht committen — .gitignore hinzufügen (für lokalen State)
State locking — verhindert parallele Apply-Operationen Module
Module sind wiederverwendbare Terraform-Pakete:
`hcl
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "5.0.0" cidr = "10.0.0.0/16"
azs = ["eu-central-1a", "eu-central-1b"]
public_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
private_subnets = ["10.0.3.0/24", "10.0.4.0/24"]
}
`Nutzen Sie den Terraform Registry für Community-Module.
FAQ
Ist Terraform kostenlos?
Ja. Terraform ist Open Source (seit 2023 auch für alle Nutzungen nach HashiCorp-Lizenzwechsel). Die Cloud-Ressourcen, die Sie erstellen, kosten natürlich.Terraform Cloud — brauche ich das?
Für Teams: ja. Es bietet Remote-State, Policy-as-Code und Collaboration. Für Einzelpersonen: optional.Was, wenn ich Ressourcen manuell ändere?
terraform plan zeigt den Drift. Mit terraform apply wird der Zustand zurückgesetzt. Nutzen Sie lifecycle { ignore_changes = [...] } für Felder, die sich häufig ändern.Kubernetes mit Terraform?
Ja, möglich. hashicorp/kubernetes Provider und hashicorp/helm` für Helm-Releases. Für komplexere K8s-Konfigurationen ist Helm oder Kustomize meist besser.Fazit
Terraform ist der Goldstandard für Infrastructure as Code. Die Lernkurve ist moderat, der Mehrwert enorm: reproduzierbare, versionierte, automatisierte Infrastruktur. Starten Sie mit einer einfachen EC2-Instanz, verstehen Sie den Workflow (init → plan → apply) und arbeiten Sie sich zu Modulen und Remote-State hoch.