Deploying Azure resources using Terraform

Infrastructure as Code is a method of defining computing and network infrastructure through code that can be applied to any software system.Such code, placed in a code version control system, is reviewable, reusable, conforms to testing conventions, and fully adheres to the principle of continuous delivery.This approach has been widely used in rapidly growing cloud computing platforms over the past decade and will become the main way to manage computer infrastructure in the future.For now, if you want to deploy resources in Azure using infrastructure as code, you can usually choose the following tools:

  • PowerShell
  • Ansible
  • Puppet
  • Chef
  • Azure Resource Manager
  • Etc.

In addition to the above tools, we have the option to deploy infrastructure and code using Terraform.Next we'll discuss how to deploy the test environment on Azure from 0 using Terraform.This time we will focus on deploying a resource group and a VNet with two subnets using Terraform

Download and install Terraform
To implement IcA using Terraform, we need to download and install Terraform first, using the following links:
https://www.terraform.io/downloads.html

Prepare TF file

After installing Terraform, we need to create some files:

  • main.tf: Contains the resources we want to create and calls to some modules
  • Variables.tf: Contains the value of the resource we want to create

Here is main.tf, which contains a resource group and a virtual network with two subnets:

provider "azurerm" {
    version = "2.0.0"
    features {}
}

resource "azurerm_resource_group" "rg" {
    name = var.resource_group_name
    location = var.location
}

resource "azurerm_virtual_network" "vnet" {
    name = var.virtual_network_name
    location = var.location
    resource_group_name = azurerm_resource_group.rg.name
    address_space = [element(var.address_space, 1)]
}

resource "azurerm_subnet" "infra" {
    name = var.subnetname_infra
    virtual_network_name = azurerm_virtual_network.vnet.name
    resource_group_name = azurerm_resource_group.rg.name
    address_prefix = var.subnet_prefix_infra
}

resource "azurerm_subnet" "infra_subnet" {
    name = var.subnet_name_DB
    virtual_network_name = azurerm_virtual_network.vnet.name
    resource_group_name = azurerm_resource_group.rg.name
    address_prefix = var.subnet_prefix_DB
}

The variables.tf file will contain the resource values that we will deploy:

variable "resource_group_name" {
    default = "TF-RG01"
}

variable "location" {
    default = "southeastasia"
}

variable  "virtual_network_name" {
    default = "Prod-VNet"
}

variable "address_space" {
    type = list(string)
    default = [
        "10.0.0.0/16"
    ]
}

variable "subnetname_infra" {
    default = "infra"
}

variable "subnet_name_DB" {
    default = "DB"
}

variable "subnet_prefix_infra" {
    default = "10.0.1.0/24"
}

variable "subnet_prefix_DB" {
    default = "10.0.2.0/24"
}

Once the above files are ready, we should start executing the deployment commands. First, we will execute terraform init to initialize the project, and in the process terraform will help us download the dependencies for:

Then we need to execute the terraform plan to see what we want to do with our Azure subscription.If we have problems writing our tf file, it will also show up at this stage:

Next, we need to run terraform apply to perform the deployment:

The deployment process requires us to enter yes to confirm the deployment:

The deployment is complete as shown in the following figure:

At this point we can log in to Azure to see if the corresponding resource was created successfully:

If you want to delete a deployment that has already been deleted, you can run terraform destroy:

After a few minutes, everything was deleted:

Tags: network ansible

Posted on Tue, 17 Mar 2020 15:33:50 -0400 by k_ind