Following my previous article about resources deployment on Azure using terraform, I will now explain how you could deploy those resources through Azure DevOps.
Then you will have to create a resource group and a storage account inside. You will need the storage account to create a container that will hold you tfstate file.
Under your storage account select containers and add a new container. I created one named terraform that will be used in the pipeline.
Pipeline explanation:
Pipeline contains three tasks. The first task will install terraform tools. The second one will initialize terraform in your working directory. Based on the user input provided in the pipeline the appropriate resource will be deployed. The code on the github contains two separate directories, one for windows and one for linux machines.
Selecting windows will deploy a windows VM using terraform
The third task will apply your terraform configuration. The working directory is generated automatically from the provided parameter.
Terraform init variables:
- task: TerraformTaskV2@2
displayName: terraform init
inputs:
provider: 'azurerm'
command: 'init'
workingDirectory: '$(Build.SourcesDirectory)/${{ parameters.image }}' // your working terraform directory
backendServiceArm: 'AzureMSDN' // azure devops service connection with your subscription
backendAzureRmResourceGroupName: 'terraform' //resource group name
backendAzureRmStorageAccountName: 'geralexgrstorageaccount' //storage account name
backendAzureRmContainerName: 'terraform' //container inside storage account
backendAzureRmKey: 'terraform.tfstate' //state file name inside your storage account
In this article I will demonstrate how one can create different environments for development needs through terraform modules. Modules provide great extensibility and code reuse. In this example I will use an appservice resource on Azure cloud.
The result of the deployment will be two different resource groups with two app services. The code of the demonstration is located at the bottom of the page.
You should first init your module. Navigate to the module folder and perform init
cd modules; terraform init
cd .. ; terraform init
Then validate your terraform code
terraform validate
The last step is to apply your configuration
terraform apply
You may encounter an error during the creation of the resources because of the app service name. It should be globally unique.
Change your name and perform a terraform apply again. Terraform will read your .tfstate file and will only implement the differences on infrastructure.
After the successful run you will see the green result output from terraform cli.
On Azure there should be two resource groups created. One for prod environment and one for test.
Inside each resource group there should be a different app service the one that it is created through the module according with the settings provided.
Terraform is one of the best automation providers for DevOps purposes used by hundred of Engineers. It is an open source tool that can be used by anyone for free. In this article I will explain how to deploy windows and linux virtual machines on Azure using a Terraform template.
First things first you will need to have the az cli installed. Then you will have to set your subscription on your current powershell session.
az account set --subscription "12abc123-4567-1234-12345-asdr4334fsd"
Then you will need to create an app role assignment for your subscription. This will be used from terraform for the provision of the resources.
az ad sp create-for-rbac --role="Contributor" --scopes="/subscriptions/12abc123-4567-1234-12345-asdr4334fsd"
That’s all. You can now deploy your resources through terraform. In the links below I have provided my Github repository along with instructions for the template use.
A tricky part of the deployment is the vm image selection. In order to locate the available azure images names you can use: