Create Azure DevOps Resources with Terraform using the new Azure DevOps Provider
If you follow Hashicorp, they recently announced a new provider for Azure DevOps. As an avid fan of Azure DevOps and Terraform, I couldn’t be more thrilled.
I have been experimenting with it for the past couple days. Thankfully, the set up is pretty straight forward. Let’s just jump into this.
Click Here for the official Terraform documentation page for Azure DevOps Provider.
Prerequisites:
- You need to have an Azure DevOps account and an organization is created. If you dont have an account, you can create one by following this guide.
https://docs.microsoft.com/en-us/azure/devops/user-guide/sign-up-invite-teammates?view=azure-devops
2. Hashicorp Terraform installed on your machine. You can download it from here if you don’t have it.
The Set Up:
The Provider Block:
org_service_url should be in this format → “https://dev.azure.com/<organization-name>/"
We need to create a new Personal access token to connect to the organization. Login to Azure DevOps Organization, Click on User settings on the top right hand corner and click on Personal access tokens.
The account corresponding to the token will need “owner” privileges for this organization.
You can also use environment variables for these just like we do with a Service Principal in Azure.
Set AZDO_ORG_SERVICE_URL for org_service_url
Set AZDO_PERSONAL_ACCESS_TOKEN for personal_access_token.
Alright! once, we are done with the set up, we can proceed with resource creations. For this demo, I have created following resources.
- A Project
- An Azure Git Repo
- Build definition
- Couple of users
- A group (Team)
- Group membership for the users
The terraform code for these resources is in my github repo. Click here. Feel free to fork or copy it.
Once, we are done with writing the code, we can run the usual terraform commands.
- terraform init — To initialise terraform in the root directory and download azure devops provider
- terraform validate — To check if there are any syntax errors
- terraform plan — To check the execution blueprint
- terraform apply — To create the resources
Note: I tested this with terraform 0.13-beta1.
I had to run terraform 0.13upgrade before terraform init.
When I ran terraform init, a new file called versions.tf was created in the root directory. I guess Hashicorp suggests using namespaces in the provider block. The contents of that file are
Once, you run terraform apply and everything goes well, you can see your resources by logging into your azure devops organization.
Project:
Login to your azure devops organization, the project will appear in Projects.
Azure Git Repo:
Click on the project → Click on Repos.
Build Definition:
Click on Pipelines to see the build.
Users and Group:
Go to Project Settings → Click on Teams on the left menu.
This is it. This is how we can use Terraform to create resources in Azure DevOps organization. Hope you like the article.
You can reach out to me on LinkedIn at https://www.linkedin.com/in/vamsikrishna-burugadda/
Until the next one, Namaste!