# hkaanturgut/automating-windows-vm-deployment-with-custom-packer-images-and-terraform-via-azure-devops-pipelines architecture

> Architecture diagram of the GitHub repository hkaanturgut/automating-windows-vm-deployment-with-custom-packer-images-and-terraform-via-azure-devops-pipelines, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hkaanturgut/automating-windows-vm-deployment-with-custom-packer-images-and-terraform-via-azure-devops-pipelines
- Repository: https://github.com/hkaanturgut/automating-windows-vm-deployment-with-custom-packer-images-and-terraform-via-azure-devops-pipelines

## Overview

##### Project type and purpose
- This is an Infrastructure-as-Code (IaC) automation project that uses Packer to build custom Windows VM images and Terraform to deploy Azure infrastructure, all orchestrated via Azure DevOps CI/CD pipelines.
- Purpose: Automate creation of a “golden” Windows VM image with preinstalled tools (Git, VS Code, Terraform, Azure CLI) and then deploy Azure networking and VM resources based on that image.

##### File structure
- Top-level YAML pipelines:
- Packer-Golden-Image-Build.yml – defines the CI pipeline for building the custom image.
- Terraform-Packer-VM-Automation.yml – defines the CI pipeline for Terraform validations.
- Packer artifacts:
- golden-image-windows-2019.json & windows-variables.json – Packer build templates and variables.
- scripts/ – PowerShell provisioning scripts invoked by Packer (install-azcli, git, terraform, vscode, prepare-vm).
- Terraform/ – directory containing Terraform code, organized as modules:
- modules/image – references the custom image.
- modules/network_interface, network_security_group, subnet, public_ip, virtual_network, etc.
- modules/windows_virtual_machine – ties everything into the target VM.
- Terraform root configs: provider.tf, network.tf, windows-vm.tf, plus tfvars for dev and network.

##### README for additional insights
- Two CI pipelines with branch policies (one for Packer JSON changes, one for Terraform code).
- A CD (release) pipeline that runs Terraform Plan, awaits approval, then Terraform Apply.

a. Main components
1. Code Repository (Azure Repos/Git)
2. Azure DevOps Pipelines:
– CI Pipeline “Packer Build”
– CI Pipeline “Terraform Validate”
– CD Pipeline “Terraform Plan & Apply”
3. Packer Builder & Provisioners (JSON templates + scripts)
4. Terraform Engine & Modules
5. Azure Components:
– Storage Account (remote tfstate)
– Resource Group
– Virtual Network → Subnet → NSG → NIC → Public IP
– Windows VM (using the custom image)
6. Service Principal / Service Connection
b. Relationships & interactions
- CI Packer pipeline pulls Packer templates & scripts → builds image → publishes image to Azure.
- CI Terraform pipeline triggers on Terraform folder changes → runs plan/validate.
- CD pipeline fetches artifacts → Terraform Plan against remote state → manual approval → Terraform Apply → Azure resource creation.
- Service Principal credentials flow into both pipelines to authenticate with Azure.
c. Architectural patterns & principles
- Separation of concerns: Packer image build vs. Infrastructure deployment.
- Modular Terraform code (each Azure resource is a reusable module).
- Git-Ops style triggers: branch policies → pipelines.
- Immutable infrastructure: create golden image, then deploy VMs from it.
d. Key technologies & frameworks
- Azure DevOps Pipelines (YAML)
- HashiCorp Packer (azure-arm builder, PowerShell provisioners)
- HashiCorp Terraform (AzureRM provider, modules)
- Azure Resource Manager (for actual resource creation)
- PowerShell scripts for in-image software installation

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Repository Lane
    subgraph "Repository" 
        direction TB
        CodeRepo["Azure Repos/Git"]:::repo
        subgraph "Packer Templates & Scripts"
            direction TB
            goldenJson["golden-image-windows-2019.json"]:::code
            windowsVars["windows-variables.json"]:::code
            subgraph "scripts/" 
                direction TB
                installAzcli["install-azcli.ps1"]:::script
                installGit["install-git.ps1"]:::script
                installTerraform["install-terraform.ps1"]:::script
                installVSCode["install-vscode.ps1"]:::script
                prepareVM["prepare-vm.ps1"]:::script
            end
        end
        subgraph "Terraform Configs & Modules"
            direction TB
            providerTf["provider.tf"]:::code
            networkTf["network.tf"]:::code
            windowsVmTf["windows-vm.tf"]:::code
            appVarsTf["app-variables.tf"]:::code
            netVarsTf["network-variables.tf"]:::code
            vmVarsTf["windows-vm-variables.tf"]:::code
            devTfvars["dev.tfvars"]:::code
            subgraph "modules/" 
                direction TB
                modImage["image"]:::module
                modRG["resource_group"]:::module
                modVNet["virtual_network"]:::module
                modSubnet["subnet"]:::module
                modNSG["network_security_group"]:::module
                modNSRule["network_security_rule"]:::module
                modAssoc["subnet_network_security_group_association"]:::module
                modNIC["network_interface"]:::module
                modPubIP["public_ip"]:::module
                modWinVM["windows_virtual_machine"]:::module
            end
        end
    end

    %% Azure DevOps Lane
    subgraph "Azure DevOps" 
        direction TB
        SP["Service Principal"]:::auth
        PackerCI["CI: Packer Build"]:::pipeline
        TerraformCI["CI: Terraform Validate"]:::pipeline
        CDPipeline["CD: Terraform Plan & Apply"]:::pipeline
    end

    %% Azure Cloud Lane
    subgraph "Azure Cloud"
        direction TB
        Storage["Storage Account (tfstate)"]:::azure
        subgraph "Resource Group" 
            direction TB
            VNet["Virtual Network"]:::azure
            Subnet["Subnet"]:::azure
            NSG["Network Security Group"]:::azure
            NIC["Network Interface"]:::azure
            PubIP["Public IP"]:::azure
            WinVM["Windows VM (Custom Image)"]:::azure
        end
    end

    %% Relationships
    CodeRepo -->|push/PR triggers| PackerCI
    CodeRepo -->|push/PR triggers| TerraformCI
    CodeRepo -->|push/merge triggers| CDPipeline
    PackerCI -->|uses templates & scripts| goldenJson
    PackerCI --> windowsVars
    PackerCI --> installAzcli
    PackerCI --> installGit
    PackerCI --> installTerraform
    PackerCI --> installVSCode
    PackerCI --> prepareVM
    PackerCI -->|builds image| WinVM
    SP --> PackerCI
    SP --> TerraformCI
    SP --> CDPipeline
    TerraformCI --> providerTf
    TerraformCI --> networkTf
    TerraformCI --> windowsVmTf
    TerraformCI --> appVarsTf
    TerraformCI --> netVarsTf
    TerraformCI --> vmVarsTf
    TerraformCI --> devTfvars
    TerraformCI --> Storage
    CDPipeline --> Storage
    CDPipeline --> providerTf
    CDPipeline --> networkTf
    CDPipeline --> windowsVmTf
    CDPipeline --> appVarsTf
    CDPipeline --> netVarsTf
    CDPipeline --> vmVarsTf
    CDPipeline --> devTfvars
    CDPipeline -->|applies modules| modImage
    CDPipeline --> modRG
    CDPipeline --> modVNet
    CDPipeline --> modSubnet
    CDPipeline --> modNSG
    CDPipeline --> modNSRule
    CDPipeline --> modAssoc
    CDPipeline --> modNIC
    CDPipeline --> modPubIP
    CDPipeline --> modWinVM
    CDPipeline -->|creates| VNet
    VNet --> Subnet
    Subnet --> NSG
    NSG --> NIC
    NIC --> PubIP
    NIC --> WinVM

    %% Click Events
    click PackerCI "https://github.com/hkaanturgut/automating-windows-vm-deployment-with-custom-packer-images-and-terraform-via-azure-devops-pipelines/blob/main/Packer-Golden-Image-Build.yml"
    click TerraformCI "https://github.com/hkaanturgut/automating-windows-vm-deployment-with-custom-packer-images-and-terraform-via-azure-devops-pipelines/blob/main/Terraform-Packer-VM-Automation.yml"
    click goldenJson "https://github.com/hkaanturgut/automating-windows-vm-deployment-with-custom-packer-images-and-terraform-via-azure-devops-pipelines/blob/main/golden-image-windows-2019.json"
    click windowsVars "https://github.com/hkaanturgut/automating-windows-vm-deployment-with-custom-packer-images-and-terraform-via-azure-devops-pipelines/blob/main/windows-variables.json"
    click installAzcli "https://github.com/hkaanturgut/automating-windows-vm-deployment-with-custom-packer-images-and-terraform-via-azure-devops-pipelines/blob/main/scripts/install-azcli.ps1"
    click installGit "https://github.com/hkaanturgut/automating-windows-vm-deployment-with-custom-packer-images-and-terraform-via-azure-devops-pipelines/blob/main/scripts/install-git.ps1"
    click installTerraform "https://github.com/hkaanturgut/automating-windows-vm-deployment-with-custom-packer-images-and-terraform-via-azure-devops-pipelines/blob/main/scripts/install-terraform.ps1"
    click installVSCode "https://github.com/hkaanturgut/automating-windows-vm-deployment-with-custom-packer-images-and-terraform-via-azure-devops-pipelines/blob/main/scripts/install-vscode.ps1"
    click prepareVM "https://github.com/hkaanturgut/automating-windows-vm-deployment-with-custom-packer-images-and-terraform-via-azure-devops-pipelines/blob/main/scripts/prepare-vm.ps1"
    click providerTf "https://github.com/hkaanturgut/automating-windows-vm-deployment-with-custom-packer-images-and-terraform-via-azure-devops-pipelines/blob/main/Terraform/provider.tf"
    click networkTf "https://github.com/hkaanturgut/automating-windows-vm-deployment-with-custom-packer-images-and-terraform-via-azure-devops-pipelines/blob/main/Terraform/network.tf"
    click windowsVmTf "https://github.com/hkaanturgut/automating-windows-vm-deployment-with-custom-packer-images-and-terraform-via-azure-devops-pipelines/blob/main/Terraform/windows-vm.tf"
    click appVarsTf "https://github.com/hkaanturgut/automating-windows-vm-deployment-with-custom-packer-images-and-terraform-via-azure-devops-pipelines/blob/main/Terraform/app-variables.tf"
    click netVarsTf "https://github.com/hkaanturgut/automating-windows-vm-deployment-with-custom-packer-images-and-terraform-via-azure-devops-pipelines/blob/main/Terraform/network-variables.tf"
    click vmVarsTf "https://github.com/hkaanturgut/automating-windows-vm-deployment-with-custom-packer-images-and-terraform-via-azure-devops-pipelines/blob/main/Terraform/windows-vm-variables.tf"
    click devTfvars "https://github.com/hkaanturgut/automating-windows-vm-deployment-with-custom-packer-images-and-terraform-via-azure-devops-pipelines/blob/main/Terraform/dev.tfvars"
    click modImage "https://github.com/hkaanturgut/automating-windows-vm-deployment-with-custom-packer-images-and-terraform-via-azure-devops-pipelines/tree/main/Terraform/modules/image/"
    click modRG "https://github.com/hkaanturgut/automating-windows-vm-deployment-with-custom-packer-images-and-terraform-via-azure-devops-pipelines/tree/main/Terraform/modules/resource_group/"
    click modVNet "https://github.com/hkaanturgut/automating-windows-vm-deployment-with-custom-packer-images-and-terraform-via-azure-devops-pipelines/tree/main/Terraform/modules/virtual_network/"
    click modSubnet "https://github.com/hkaanturgut/automating-windows-vm-deployment-with-custom-packer-images-and-terraform-via-azure-devops-pipelines/tree/main/Terraform/modules/subnet/"
    click modNSG "https://github.com/hkaanturgut/automating-windows-vm-deployment-with-custom-packer-images-and-terraform-via-azure-devops-pipelines/tree/main/Terraform/modules/network_security_group/"
    click modNSRule "https://github.com/hkaanturgut/automating-windows-vm-deployment-with-custom-packer-images-and-terraform-via-azure-devops-pipelines/tree/main/Terraform/modules/network_security_rule/"
    click modAssoc "https://github.com/hkaanturgut/automating-windows-vm-deployment-with-custom-packer-images-and-terraform-via-azure-devops-pipelines/tree/main/Terraform/modules/subnet_network_security_group_association/"
    click modNIC "https://github.com/hkaanturgut/automating-windows-vm-deployment-with-custom-packer-images-and-terraform-via-azure-devops-pipelines/tree/main/Terraform/modules/network_interface/"
    click modPubIP "https://github.com/hkaanturgut/automating-windows-vm-deployment-with-custom-packer-images-and-terraform-via-azure-devops-pipelines/tree/main/Terraform/modules/public_ip/"
    click modWinVM "https://github.com/hkaanturgut/automating-windows-vm-deployment-with-custom-packer-images-and-terraform-via-azure-devops-pipelines/tree/main/Terraform/modules/windows_virtual_machine/"

    %% Styles
    classDef repo fill:#f9f,stroke:#333,stroke-width:1px
    classDef code fill:#ccf,stroke:#333,stroke-width:1px
    classDef script fill:#cfc,stroke:#333,stroke-width:1px
    classDef module fill:#ffc,stroke:#333,stroke-width:1px
    classDef pipeline fill:#fcf,stroke:#333,stroke-width:1px
    classDef azure fill:#cff,stroke:#333,stroke-width:1px
    classDef auth fill:#fdd,stroke:#333,stroke-width:1px
```
