Welcome to this comprehensive guide on automating the creation of Windows VMs with Custom (Golden) Images through Packer and Terraform, orchestrated by Azure DevOps CI/CD Pipelines.
Prerequisites :
- An Azure account with an active subscription. Create an account for free.
- Azure DevOps Account, Organization and a Project
- Service Principal and Service Connection
- Storage Account & Container for remote tf state file
- Code Repository (Azure Repo)
-
In the repository, I have Terraform Folder which has Terraform codes and configs.
-
golden-image-windows-2019.json & windows-variables.json are Packer Image files written in Json format.
-
scripts folder has PowerShell scripts to install the desired softwares & tools into the custom image.
We aim to automate the installation of the following software and tools within the Packer image, crucial for any Cloud & DevOps professional:
- Git
- VS Code
- Terraform
- Azure CLI
In this step, we utilize Packer's Builders (azure-arm) and Provisioners to configure and install software components using PowerShell scripts.
To emulate real-world work environments, we establish branch policies linked to pipelines. These policies trigger Packer Pipelines upon Pull Requests (PRs) to the main branch, ensuring proper handling of changes in .json Packer files.
Please see the Pipeline jobs/tasks. Image build process approximately takes 15-20min
- You can also see the detailed info outputs of the created image as I configured that in the pipeline that way :)
After the Packer image build process, it's essential to verify the newly created custom image. You can easily access it from the Azure Portal under your Resource Group (RG) or Images.
Before deploying a new VM, we need to configure several dependent resources, including Resource Groups, Virtual Networks, Subnets, Network Security Groups (NSG), Network Interface Cards (NIC), and Public IP addresses (Pip). Our Terraform code, structured in modules, automates this setup.
Terraform code based on MODULE structure is ready for these reasources in the Terraform Folder
To mirror real-world scenarios, branch policies tied to pipelines trigger Terraform Pipelines upon PRs to the main branch when changes occur in the Terraform folder.
Please see the Pipeline jobs/tasks.
We are going to deploy our resources on Release pipeline. For this, we need to have a artifact to pass our files over to Releases.
This is configured in the pipeline.
Upon successful completion of the CI pipeline, the CD (Release) pipeline activates automatically. We ensure that the latest artifact from the CI pipeline is used for continuous deployment.
Additionally, we conduct a Terraform Plan to validate the configuration and await approval before applying the changes.
After receiving approval, the Terraform Apply command deploys the defined resources. You can monitor the deployment's status in the Azure Portal.
The final step involves connecting to the deployed VM through Remote Desktop Protocol (RDP) to verify the proper installation of software and tools.