Deploy an Azure DevOps Pipeline by using Terraform

Janeth Fernando
FAUN — Developer Community 🐾
4 min readAug 9, 2021

--

In this article, I’m going to share how we can implement an Azure DevOps pipeline by using Terraform. To easily understand, I will first create a pipeline by using the Azure DevOps portal. Then I will create the same pipeline by using Terraform.

Here is what we are going to do:- We are going to create a new project on Azure DevOps, and under the new project we are going to create a new pipeline. As for the configurations of the pipeline, we are going to use a YAML file that is on a GitHub account. The YAML file has all the steps to follow when running the pipeline.

Prerequisites:- You need to have an Azure DevOps account.

Creating an Azure DevOps Pipeline by using the portal

a. You can create or use an existing Azure DevOps Organization. When you open the Azure DevOps portal, you will see something like the following window.

b. Create a new private/public project under a selected organization. Once you have successfully created a new project, you will see a window like below.

c. Now we can create a pipeline by selecting the pipeline tab from the left side of the window. Select use the classic editor. Then you will be asked to point to the source(configuration) of the pipeline that you want to create.

d. As I said before, I am going to use GitHub as my source of configuration. Then you will be asked to create a GitHub service connection. For this task, you need to get a Personal Access Token(PAT) from your GitHub account. Paste your PAT in the prompted dialog.

e. Afterwards, select the repository and the branch that contains the pipeline configuration YAML file.

Your Pipeline is now created! Once the pipeline is triggered it will work as per the workflow mentioned on the YAML file that is on your Github account.

Creating an Azure DevOps Pipeline by using Terraform

Here we are going to accomplish the exact same operation that was done by using the Azure DevOps portal. I assume that you have already installed Terraform on your machine.

Prerequisites for Terraform pipeline creation:- You need to have a PAT from your Azure DevOps account.

a) Now let’s start creating the Terraform YAMLs. We are going to define the resources in the main.tf file, the variable definitions in the variables.tf file, the values of the variables in the conf.variables.tfvar file and the secrets in the conf.secrets.tfvar file.

b) First, in the main.tf file, we are going to point to microsoft/azuredevops as the source. Then we are going to create the project, the GitHub service endpoint, and the pipeline accordingly.

c) Now let's create the variables.tf file to validate the variables.

d) We can now define the variables in the conf.variables.tfvars file. The variable devops_org_service_url is the URL of the organization you have under your Azure DevOps account.

e) There are two secret variables that needs to be defined in order to provision the related resources. Those secret variables can be managed in a separate file eg:- conf.secrets.tfvars.

Now we are ready to apply these changes. You first need to login to Azure by using the Azure CLI(do an az loginon the terminal). Follow the underneath commands to apply the changes on Azure DevOps by using Terraform.

  1. Initialize terraform on the source directory.
$ terraform init

2. Apply the changes

$ terraform apply -var-file=conf.variables.tfvars -var-file=conf.secrets.tfvars

3. You will see the plan as follows

4. To confirm that you need these resources you can type “yes” and see the resources being created.

Now you can check your Azure DevOps portal and see that your pipeline has got created.

Cheers 🍻!

Join FAUN: Website 💻|Podcast 🎙️|Twitter 🐦|Facebook 👥|Instagram 📷|Facebook Group 🗣️|Linkedin Group 💬| Slack 📱|Cloud Native News 📰|More.

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author 👇

--

--