1. Create the DevOps project

Azure DevOps (previously VSTS) allows you to automate the deployment of resources, whether ARM template (JSON), PowerShell, CLI, Terraform, etc.

Today, I’m going to focus on the integration with Terraform. Be careful, I’ll not go into Terraform details in this article, so you need to know the technology.

The pricing for Azure DevOps is available here:

https://azure.microsoft.com/en-us/pricing/details/devops/azure-pipelines/

By default, you’ve 5 users free.

Before starting, make sure you’ve an Azure DevOps organization in your subscription:

Open via the URL your Azure DevOps and create a new project by clicking on Create project:

Give it a name and click on Create:

When the project is created, you can invite people, etc.:

On the left, go into Repos > Files and add one or more files in the directory. You can add a README for example, to initialize the project, by clicking on Initialize:

When the initialization is finished, you’ll have at least one file in your repository:

Here, create a folder, with 2 files, main.tf and variables.tf:

2. Terraform code

With following Terraform code, I’ll deploy 1 VNet in Azure, with 2 subnets. As you can see, for some variables, I’m using __ before and after the variable. It’s to be able to use variables, directly in Azure DevOps. The code use dis the following :

Main.tf

terraform { required_version = ">= 0.11" backend "azurerm" { storage_account_name = "__terraformstorageaccount__" container_name = "terraform" key = "terraform.tfstate" access_key = "__storagekey__" } } resource "azurerm_resource_group" "rg" { name = "__resource_group__" location = "__location__" } resource "azurerm_virtual_network" "vnet" { name = "${var.virtual_network_name}" location = "__location__" address_space = ["${var.address_space}"] resource_group_name = "${azurerm_resource_group.rg.name}" } resource "azurerm_subnet" "subnetfrontend" { name = "${var.subnetname_prefixfrontend}" virtual_network_name = "${azurerm_virtual_network.vnet.name}" resource_group_name = "${azurerm_resource_group.rg.name}" address_prefix = "${var.subnet_prefixfrontend}" } resource "azurerm_subnet" "subnetbackend" { name = "${var.subnetname_prefixbackend}" virtual_network_name = "${azurerm_virtual_network.vnet.name}" resource_group_name = "${azurerm_resource_group.rg.name}" address_prefix = "${var.subnet_prefixbackend}" } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 terraform { required_version = ">= 0.11" backend "azurerm" { storage_account_name = "__terraformstorageaccount__" container _ name = "terraform" key = "terraform.tfstate" access _ key = "__storagekey__" } } resource "azurerm_resource_group" "rg" { name = "__resource_group__" location = "__location__" } resource "azurerm_virtual_network" "vnet" { name = "${var.virtual_network_name}" location = "__location__" address _ space = [ "${var.address_space}" ] resource_group_name = "${azurerm_resource_group.rg.name}" } resource "azurerm_subnet" "subnetfrontend" { name = "${var.subnetname_prefixfrontend}" virtual_network_name = "${azurerm_virtual_network.vnet.name}" resource_group _ name = "${azurerm_resource_group.rg.name}" address _ prefix = "${var.subnet_prefixfrontend}" } resource "azurerm_subnet" "subnetbackend" { name = "${var.subnetname_prefixbackend}" virtual_network_name = "${azurerm_virtual_network.vnet.name}" resource_group _ name = "${azurerm_resource_group.rg.name}" address _ prefix = "${var.subnet_prefixbackend}" }

Variables.tf

variable "virtual_network_name" { description = "The name for the virtual network." default = "FLOAPP-vNet-Terra" } variable "address_space" { description = "The address space that is used by the virtual network. You can supply more than one address space. Changing this forces a new resource to be created." default = "10.0.0.0/16" } variable "subnetname_prefixfrontend" { description = "The shortened abbreviation to represent your resource group that will go on the front of some resources." default = "Frontend" } variable "subnetname_prefixbackend" { description = "The shortened abbreviation to represent your resource group that will go on the front of some resources." default = "Backend" } variable "subnet_prefixfrontend" { description = "The address prefix to use for the subnet." default = "10.0.10.0/24" } variable "subnet_prefixbackend" { description = "The address prefix to use for the subnet." default = "10.0.20.0/24" } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 variable "virtual_network_name" { description = "The name for the virtual network." default = "FLOAPP-vNet-Terra" } variable "address_space" { description = "The address space that is used by the virtual network. You can supply more than one address space. Changing this forces a new resource to be created." default = "10.0.0.0/16" } variable "subnetname_prefixfrontend" { description = "The shortened abbreviation to represent your resource group that will go on the front of some resources." default = "Frontend" } variable "subnetname_prefixbackend" { description = "The shortened abbreviation to represent your resource group that will go on the front of some resources." default = "Backend" } variable "subnet_prefixfrontend" { description = "The address prefix to use for the subnet." default = "10.0.10.0/24" } variable "subnet_prefixbackend" { description = "The address prefix to use for the subnet." default = "10.0.20.0/24" }

3. Create your first build

We will now create the build, which will check if resources have been modified in a particular folder, and thus, create a new build for our future release. Click on Pipelines > Builds > New Pipeline:

Here, choose where sources are located and validate:

Choose the template that you want to use, empty job for me:

Give a name, choose a pool where an agent is installed. This pool can be deployed on one of your VMs, to limit costs:

Add a job with type Copy Files. Give a name, choose the folder where you created your files main and variables, and choose to copy all content. The target folder must be the following: $(build.artifactstagingdirectory)/Terraform

Add a job with type Publish Build Artifacts and leave it with default parameters:

Click now on Triggers to activate the CI (Continuous Integration) to launch this build, after each file modification that exists in the master branch:

Save and execute the build:

After few seconds, our build was executed correctly:

You will receive an email to tell you that everything works well with the last build:

VSAN from StarWind eliminates any need for physical shared storage just by mirroring internal flash and storage resources between hypervisor servers. Furthermore, the solution can be run on the off-the-shelf hardware. Such design allows VSAN from StarWind to not only achieve high performance and efficient hardware utilization but also reduce operational and capital expenses. Learn more about ➡ VSAN from StarWind

And with CI activated, the reason of the execution with this new build is Continuous integration:

In the next article, we will see how to build our first release to deploy the Terraform template.

Related materials:

Views All Time Views All Time 3 Views Today Views Today 18

Appreciate how useful this article was to you?

5 out of 5, based on 1 review 5 out of 5, based on 1 review

Loading... Loading...