Photo by Timelab Pro

Create your Azure DevOps repositories with Terraform

Quickly generate your repositories

Posted by Damien Aicheh on 10/25/2022 · 5 mins

In this tutorial we are going to create the repositories associated with our Azure DevOps project using Terraform!

This tutorial is part of a full series of tutorials on configuring Azure DevOps using Terraform. You can download the project from the previous part and follow along.

Add a new variable

In the file, we will add the ability to configure a list of repositories. To give to the repositories names a bit of consistency we will create a nomenclature which will be: domain_type_application, where: type can be dev or infra.

So for instance you can have: nature_dev_vegetables which stands for the dev repository of the vegetables api for the nature domain. Another example can be: nature_dev_animals etc.. Of course find the best nomenclature for your need. You can also add the ability to change the default_branch for the repository.

This will look like this:

variable "repositories" {
  type = list(object({
    application    = string
    type           = string
    domain         = string
    default_branch = string
  default = []

Define the repositories

Let’s create a new file called and using the azuredevops_git_repository resource you can iterate to create all the repositories with the nomenclature define above:

resource "azuredevops_git_repository" "this" {
  count          = length(var.repositories)
  project_id     =
  name           = "${var.repositories[count.index].domain}_${var.repositories[count.index].type}_${var.repositories[count.index].application}"
  default_branch = var.repositories[count.index].default_branch == "" ? "refs/heads/develop" : var.repositories[count.index].default_branch
  initialization {
    init_type = "Clean"
  lifecycle {
    ignore_changes = [

As you can see you can concat all the informations to create the repository name and you can use ternary conditions to define whether the default_branch value should be set. More configure options are available in the documentation.

Define an environment variable file

To avoid passing all the arguments inside the command line you need to create a dedicated file. So inside a new file called env.tfvars we will declare the name of the project like we did in the previous tutorial and we will create a list of respositories. Here is an example:

project_name = "Demo Project"
repositories = [
    domain         = "domain1"
    type           = "dev"
    application    = "api"
    default_branch = ""
    domain         = "domain2"
    type           = "infra"
    application    = "func"
    default_branch = ""

At the time I’m writing this article the optional parameters for Terraform are still in experimental, so you need to define the default_branch to avoid any error.

Run Terraform

Now you can run the new Terraform plan command like this:

terraform plan -var-file=env.tfvars --out=plan.out

And then apply it:

terraform apply plan.out

As result you should see your repositories created in your project inside Azure DevOps!

Final touch

You have now your project setup with all your repositories using Terraform. You will find full source code in this Github repository.

What’s next?

In the next tutorial of this series we will focus on adding default files to your repositories!

Do not hesitate to follow me on to not miss my next tutorial!