· Henning Scholand · Blog  · 2 Min. Lesezeit

Azure Infrastructure as Code mit Terraform

Best Practices für die Verwaltung von Azure-Infrastruktur mit Terraform - von der Projektstruktur bis zur CI/CD-Pipeline.

Best Practices für die Verwaltung von Azure-Infrastruktur mit Terraform - von der Projektstruktur bis zur CI/CD-Pipeline.

Infrastructure as Code (IaC) ist nicht optional - es ist die Grundlage für reproduzierbare, versionierte und nachvollziehbare Cloud-Infrastruktur. In diesem Beitrag zeige ich meine bewährte Terraform-Projektstruktur für Azure.

Projektstruktur

Eine saubere Struktur ist entscheidend für wartbaren Code:

terraform/
├── environments/
│   ├── dev/
│   │   ├── main.tf
│   │   ├── variables.tf
│   │   └── terraform.tfvars
│   ├── staging/
│   └── prod/
├── modules/
│   ├── networking/
│   ├── compute/
│   └── security/
└── shared/
    └── backend.tf

State Management

Der Terraform State gehört niemals ins Git-Repository. Nutzen Sie Azure Blob Storage:

terraform {
  backend "azurerm" {
    resource_group_name  = "rg-terraform-state"
    storage_account_name = "stterraformstate"
    container_name       = "tfstate"
    key                  = "prod.terraform.tfstate"
  }
}

Netzwerk-Modul Beispiel

module "networking" {
  source = "../../modules/networking"

  resource_group_name = azurerm_resource_group.main.name
  location            = var.location

  vnet_address_space  = ["10.0.0.0/16"]
  subnets = {
    web = {
      address_prefix = "10.0.1.0/24"
      nsg_rules      = ["http", "https"]
    }
    app = {
      address_prefix = "10.0.2.0/24"
      nsg_rules      = ["app-internal"]
    }
  }
}

CI/CD Integration

Automatisierte Deployments mit Azure DevOps:

stages:
  - stage: Plan
    jobs:
      - job: TerraformPlan
        steps:
          - task: TerraformCLI@2
            inputs:
              command: 'plan'
              workingDirectory: 'environments/prod'

  - stage: Apply
    dependsOn: Plan
    condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/main'))
    jobs:
      - deployment: TerraformApply
        environment: 'production'

Fazit

Terraform und Azure sind ein starkes Team. Investieren Sie Zeit in eine gute Struktur - Ihr zukünftiges Ich wird es Ihnen danken.

Zum Blog

Ähnliche Artikel

Alle Artikel »