Introduction

As we all know DevOps has become the gold standard in today’s IT world. That means we have a lot of DevOps tools to implement different DevOps stages or best practices. Azure DevOps is one such tool that not only helps us implement our build and release pipeline, but organizes the projects with the help of boards, and sprints. Since Azure has seen exponential growth in the last few years. Azure DevOps has also become a standard as it helps organizations manage everything centrally.

In this article, I explain how you can automate Azure DevOps implementation with the help of Terraform. Check out my GitHub link for the terraform code.

Assumptions in the deployment:

  1. You have already created an ADO organization. The project will be deployed in an existing organization.
  2. Your account has an existing PAT with required permissions or you can create a new PAT.
  3. Terraform will use PAT to authenticate and create resources in the ADO Organization. This is updated in main.tf file under the provider section.

Explanation

Since Terraform documentation for azure DevOps provider isn’t detailed and leaves a lot to the imagination, I can’t use conditionals in the variables. This is because we don’t know what all values it expects. If you have seen my previous articles, you would know I am a big fan of parameterizing the code and using variables. I have used variables for the values that I could. And all these variable values can be updated in the terraform.tfvars file. List of resources created as part of this deployment :

  1. Azure DevOps project, this is declared under main.tf file.
  2. Azure DevOps Repository, is declared under repository.tf file. As part of the process, we are also importing it from a sample GitHub repository.
  3. Azure DevOps build pipeline created under this project, this is declared under main.tf file. I have also used YAML file for the build. This code expects this yaml file to exist on the GitHub repository. You can find a sample yaml file in the GitHub repo used in the second step.
  4. All the variables are declared in variables.tf file. If you plan on using additional variables, update the values in this file.
  5. When we run terraform plan, it fetches the value of these variables from terraform.tfvars file. Change the values in this file to suit your need.

Usage :

To run this example, simply follow to steps below:

  1. Navigate to the folder where you stored these files and use :
  terraform init
  terraform plan
  terraform apply

Explanation :

  1. Terraform init -> It initializes the directory and downloads the required provider along with configuring the module.
  2. Terraform plan -> This helps you verify the code is going to deploy the resources as expected. This also ensures we don’t face any unwanted surprises. This isn’t mandatory, but a recommended step.
  3. Terraform apply -> This step applies the resources specified in this code. This will ask you to approve this later. We can skip manual approval by using –auto-approve parameter.

Best Practices & Recommendations

  1. Use Terraform workspaces for easier management of the deployments. This can also help us manage Dev, UAT, and Production deployments instead of creating multiple state files\directories.
  2. If you are creating new resources\variables. Ensure the naming convention is easily relatable since we have a lot of variables in this code.
  3. Use conditionals to avoid unwanted surprises.