kajenk / Automating-Windows-VM-Deployment-with-Custom-Packer-Images-and-Terraform-via-Azure-DevOps-Pipelines

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Automating-Windows-VM-Deployment-with-Custom-Packer-Images-and-Terraform-via-Azure-DevOps-Pipelines

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.

Packer Image Automation Diagram

Let's get started!

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)

Repository Explanation:

In this repository, you'll find the following components:

Project Task and Steps:

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

Creating Custom Image with Packer Explanation

Step 1: Preparing the Packer Image

In this step, we utilize Packer's Builders (azure-arm) and Provisioners to configure and install software components using PowerShell scripts.

Screenshot 2023-12-21 at 1 22 01 PM

Step 2: Setting Up Branch Policies and Packer Pipelines

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.

Screenshot 2023-12-21 at 11 44 31 AM

Screenshot 2023-12-21 at 11 43 03 AM

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 :)

Screenshot 2023-12-21 at 12 04 05 PM

Step 3: Checking the Image

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.

Screenshot 2023-12-21 at 12 07 06 PM

Creating Azure Virtual Machine from Built Packer Image

Step 1: Preparing Azure Resources for VM Deployment

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

Step 2: Branch Policies and Terraform Pipelines

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.

Screenshot 2023-12-21 at 11 44 31 AM copy

Please see the Pipeline jobs/tasks.

Screenshot 2023-12-21 at 12 26 23 PM

It's okay if Checkov stage is failing as this is demo :) Configured to continue on ERROR.

Screenshot 2023-12-21 at 12 29 41 PM

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.

Screenshot 2023-12-21 at 9 20 56 AM

Step 3: Configuring the CD (Release) 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.

Screenshot 2023-12-21 at 9 21 15 AM

Step 4: Terraform Apply and Resource Deployment

After receiving approval, the Terraform Apply command deploys the defined resources. You can monitor the deployment's status in the Azure Portal.

Screenshot 2023-12-21 at 1 58 53 PM

Step 5: Connecting to the Deployed VM via RDP

The final step involves connecting to the deployed VM through Remote Desktop Protocol (RDP) to verify the proper installation of software and tools.

Screenshot 2023-12-21 at 12 33 33 PM

RESULT

With this comprehensive automation process, you can efficiently create Windows VMs from custom Packer images and deploy essential resources, all managed through Azure DevOps Pipelines.

Screenshot 2023-12-21 at 11 35 43 AM

About


Languages

Language:HCL 92.6%Language:PowerShell 7.4%