Search
StarWind is a hyperconverged (HCI) vendor with focus on Enterprise ROBO, SMB & Edge

[Azure DevOps] Terraform deployment with Azure DevOps – Part 2

  • April 11, 2019
  • 7 min read
Cloud and Virtualization Architect. Florent is specializing in public, hybrid, and private cloud technologies. He is a Microsoft MVP in Cloud and Datacenter Management and an MCSE in Private Cloud.
Cloud and Virtualization Architect. Florent is specializing in public, hybrid, and private cloud technologies. He is a Microsoft MVP in Cloud and Datacenter Management and an MCSE in Private Cloud.

A close up of a map Description automatically generated

After seeing how to create our first build with a terraform file in the part 1, we will see how to create a release to deploy our Terraform template.

Now that we’ve our build, select Pipelines > Releases and click on New pipeline:

Pipelines > Releases and click on New pipeline

Select Empty job in next screen:

wp-image-11951

Rename your pipeline, choose the build source pipeline and as version, select the lastest build available, to integrate our CI/CD:

wp-image-11952

Click on the lightning and activate the CD (Continuous Deployment):

A screenshot of a cell phone Description automatically generated

Don’t forget to save. Open now your Stage 1 and add the following tasks, by clicking on the + :

  • Azure CLI
  • Azure PowerShell
  • Replace Tokens
  • Run Terraform
  • Run Terraform
  • Run Terraform

Be careful, Replace tokens and Run Terraform must be installed from the store (free) before using them:

A screenshot of a cell phone Description automatically generated

We’ll now configuring each step. For the first step, Azure CLI, you can rename it, and after, add a subscription, with a service principal account, that has rights to deploy resources in the subscription:

wp-image-11955

Choose Inline script and use the following script, to create the resource group and the storage account that we will use to store our tfstate file, that gives us an overview of the deployment and give the possibility to continue the deployment after each modification:

You should have this:
A screenshot of a cell phone Description automatically generated

Open the second step, Azure PowerShell, and give it a name. Choose the Azure Connection Type => Azure Resource Manager and the subscription that you created previously. Choose Inline Script and insert the following script, to get the access key of the storage account:

Choose to use the last installed version of Powershell and Save. You should see this:

A screenshot of a cell phone Description automatically generated

For the third step, Replace Tokens, modify Target files by including every .tf files and, in Advanced, modify Token prefix and Token suffix by __ that we included in main.tf file. With that, it will take variables of our release, directly in Azure DevOps and not in the variables.tf file. You should see this:

A screenshot of a cell phone Description automatically generated

In the fourth step, Run Terraform, provide a name, choose the path to the template (the artifact, built in the build) by clicking on … and provide the argument init. Check the box to Install terraform, with the latest version, and check the box to use an Azure service principal, and choose your Azure subscription:

A screenshot of a cell phone Description automatically generated

In the fifth step, it’s the same as the fourth step, except for the argument, it’s plan instead of init:

A screenshot of a cell phone Description automatically generated

For the last step, it’s same as before, except for argument part. You must replace plan by apply -auto-approve:

A screenshot of a cell phone Description automatically generated

Save. Navigate to the Variables tab and click on Add:

A screenshot of a cell phone Description automatically generated

Add the following values, with your own values:

  • location
  • resource_group
  • terraformstorageaccount
  • terraformstoragerg

A screenshot of a cell phone Description automatically generated

My subscription is empty as you can see below:

A screenshot of a cell phone screen with text Description automatically generated

To start the deployment, just modify the variable file in the repo. When you’ll commit the file, the build will start (CI) and when the build is done, the release will be created (CD):

wp-image-11965

A screenshot of a cell phone Description automatically generated

A screenshot of a cell phone Description automatically generated

A screenshot of a cell phone Description automatically generated

A screenshot of a cell phone Description automatically generated

wp-image-11970

A screenshot of text Description automatically generated

A screenshot of a cell phone Description automatically generated

As you can see, everything worked fine. And on Azure, I’ve all of my resources deployed:

wp-image-11973

wp-image-11974

As you can see, it’s very easy now to deploy Terraform template with automation from beginning to end. It works with modifications and so on, so during the build/release of your change, you can do other work or drink a coffee 🙂

Hey! Found Florent’s insights useful? Looking for a cost-effective, high-performance, and easy-to-use hyperconverged platform?
Taras Shved
Taras Shved StarWind HCI Appliance Product Manager
Look no further! StarWind HCI Appliance (HCA) is a plug-and-play solution that combines compute, storage, networking, and virtualization software into a single easy-to-use hyperconverged platform. It's designed to significantly trim your IT costs and save valuable time. Interested in learning more? Book your StarWind HCA demo now to see it in action!