💡Azure DevOps resources using Terraform

Apurv upadhyay
3 min readOct 12, 2024

--

Azure Devops Creation Using Terraform

As an enthusiast of automation, I’ve always sought ways to eliminate manual steps (goodbye, ClickOps!) when setting up infrastructure. Why manually create a new Azure DevOps project, repositories, and branch policies when you can define everything as code? That’s where Terraform’s Azure DevOps provider comes into play! It allows you to interact with the Azure DevOps REST API to manage resources seamlessly using Infrastructure as Code (IaC).

Let’s dive into how you can use Terraform to automate the creation of Azure DevOps resources, complete with code examples! 💪

🌐 Setting Up the Azure DevOps Provider

First, configure the Azure DevOps provider in your Terraform project.

Provider Configuration (main.tf):

terraform {
required_version = ">= 0.13"
required_providers {
azuredevops = {
source = "microsoft/azuredevops"
version = "~> 0.4.0"
}
}
}

provider "azuredevops" {
org_service_url = var.azure_devops_org_url
personal_access_token = var.azure_devops_pat
}

Variables (variables.tf):

variable "azure_devops_org_url" {
type = string
description = "The Azure DevOps organization URL (e.g., https://dev.azure.com/YourOrganization)"
}

variable "azure_devops_pat" {
type = string
description = "Azure DevOps Personal Access Token"
sensitive = true
}

Set Environment Variables:

export TF_VAR_azure_devops_org_url="https://dev.azure.com/YourOrganization"
export TF_VAR_azure_devops_pat="your_personal_access_token"

📁 Defining Project and Repository Variables

Define input variables for your project settings.

Variables (variables.tf):

variable "project_name" {
type = string
description = "Name of the Azure DevOps project"
default = "SampleProject"
}

variable "project_description" {
type = string
description = "Description of the project"
default = "A sample project created with Terraform."
}

variable "project_visibility" {
type = string
description = "Project visibility (private or public)"
default = "private"
}

variable "repository_name" {
type = string
description = "Name of the repository"
default = "SampleRepo"
}

🏗️ Creating the Azure DevOps Project and Repository

Use Terraform resources to create a project and a Git repository.

Project and Repository Resources (main.tf):

resource "azuredevops_project" "project" {
name = var.project_name
description = var.project_description
visibility = var.project_visibility
version_control = "Git"
work_item_template = "Agile"
}

resource "azuredevops_git_repository" "repo" {
project_id = azuredevops_project.project.id
name = var.repository_name
initialization {
init_type = "Clean"
}
}

🔒 Adding Branch Policies

Implement branch policies to enforce code quality standards.

Branch Policy Resource (main.tf):

resource "azuredevops_branch_policy_min_reviewers" "branch_policy" {
project_id = azuredevops_project.project.id
repository_id = azuredevops_git_repository.repo.id
branch = "refs/heads/main"
enabled = true
blocking = true
minimum_reviewer_count = 2
creator_vote_counts = false
reset_on_source_push = true
require_author_submission = false
}

📦 Uploading Local Code to the Repository

Use a null_resource with a local-exec provisioner to push local code.

Code Upload Resource (main.tf):

resource "null_resource" "upload_code" {
depends_on = [azuredevops_git_repository.repo]

provisioner "local-exec" {
command = <<EOT
cd ./local-repo-files
git init
git remote add origin ${azuredevops_git_repository.repo.remote_url}
git add .
git commit -m "Initial commit"
git push -u origin main
EOT
}
}

Ensure your local code resides in the ./local-repo-files directory.

🔄 Understanding Dependencies

Terraform handles resource creation in the correct order based on dependencies:

• The repository depends on the project.

• Branch policies depend on the repository.

• Code upload depends on the repository being available.

✅ Applying Your Terraform Configuration

Initialize and apply your Terraform configuration to create resources.

terraform init
terraform plan
terraform apply

🔍 Verifying the Setup

Project Creation: Check your Azure DevOps organization for the new project.

Repository Initialization: Verify that the repository exists and is initialized.

Branch Policies: Confirm that the branch policies are enforced.

Code Upload: Ensure your local code is pushed to the repository.

🎯 Key Takeaways

Efficiency: Automate Azure DevOps resource creation to save time and reduce errors.

Consistency: Use Infrastructure as Code to maintain consistent environments.

Integration: Seamlessly integrate with Azure DevOps REST API using Terraform.

❤️ Share Your Thoughts!

Using Terraform to manage Azure DevOps resources makes automation straightforward and efficient. Give it a try and let me know how it enhances your workflow!

Feel free to share feedback or ask questions. Until next time, happy automating! 🚀

#AzureDevOps #Terraform #DevOps #InfrastructureAsCode #Automation #CI/CD #Cloud

--

--

Apurv upadhyay
Apurv upadhyay

Written by Apurv upadhyay

Principal Software Engineer at PeerIslands • Microsoft Azure Certified Architect Expert & DevOps Specialist • 7x Azure Certified • ex-Microsoft, Bosch

Responses (2)