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:
- You have already created an ADO organization. The project will be deployed in an existing organization.
- Your account has an existing PAT with required permissions or you can create a new PAT.
- Terraform will use PAT to authenticate and create resources in the ADO Organization. This is updated in main.tf file under the provider section.
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 :
- Azure DevOps project, this is declared under main.tf file.
- Azure DevOps Repository, is declared under repository.tf file. As part of the process, we are also importing it from a sample GitHub repository.
- 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.
- All the variables are declared in variables.tf file. If you plan on using additional variables, update the values in this file.
- 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.
To run this example, simply follow to steps below:
- Navigate to the folder where you stored these files and use :
terraform init terraform plan terraform apply
- Terraform init -> It initializes the directory and downloads the required provider along with configuring the module.
- 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.
- 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.
- 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.
- If you are creating new resources\variables. Ensure the naming convention is easily relatable since we have a lot of variables in this code.
- Use conditionals to avoid unwanted surprises.