Terraform Grundlagen
Was ist Terraform?
Terraform ist ein Infrastructure as Code (IaC) Tool:
- Deklarative Konfiguration
- Multi-Cloud fähig
- State Management
- Entwickelt von HashiCorp
HCL Syntax (HashiCorp Configuration Language)
# Provider Konfiguration
provider "aws" {
region = "eu-central-1"
}
# Resource Definition
resource "aws_instance" "web" {
ami = "ami-0123456789"
instance_type = "t3.micro"
tags = {
Name = "WebServer"
}
}
# Output
output "public_ip" {
value = aws_instance.web.public_ip
}
Core Workflow
- Init: Provider herunterladen
- Plan: Änderungen vorschauen
- Apply: Änderungen anwenden
- Destroy: Ressourcen löschen
Provider
Major Cloud Provider
- AWS Provider
- Azure Provider (azurerm)
- Google Cloud Provider
Weitere
- Kubernetes Provider
- Helm Provider
- GitHub Provider
- 3000+ Community Provider
State Management
Local State
- terraform.tfstate Datei
- Für Einzelentwickler
- Nicht für Teams
Remote State
- S3 + DynamoDB (AWS)
- Azure Blob Storage
- Terraform Cloud
- State Locking
Modules
Definition
- Wiederverwendbare Komponenten
- Input Variables
- Outputs
- Versioning
Struktur
modules/
├── vpc/
│ ├── main.tf
│ ├── variables.tf
│ └── outputs.tf
└── ec2/
├── main.tf
├── variables.tf
└── outputs.tf
Variables und Outputs
# Variable Definition
variable "environment" {
type = string
description = "Environment name"
default = "dev"
}
# Variable Nutzung
resource "aws_instance" "web" {
tags = {
Environment = var.environment
}
}
Workspaces
- Mehrere State-Instanzen
- Environment-Trennung
- terraform workspace new/select
Best Practices
- Remote State verwenden
- Module nutzen
- Variablen für Konfiguration
- Version Pinning
- Formatierung (terraform fmt)
- Validierung (terraform validate)
Terraform Cloud
- Remote State
- Remote Runs
- Policy as Code (Sentinel)
- Team Collaboration
CFTools Software implementiert Terraform-basierte Infrastruktur.