Use Terraform deploy Azure Resources

"Infrastructure Code" is a method for computing and network infrastructure is defined by the code, it can be applied to any software system. Such a code in the code version control system, has auditability, reusability, and in line with the practice test, also fully comply with the principle of continuous delivery. This method has been widely used over the past decade, the rapid growth of cloud computing platforms, and it will also become the main way the next administration computer infrastructure. For now, if we want to use infrastructure as a code to deploy resources in Azure usually you can select the following tools:

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

In addition to these tools, we can also choose to use Terraform to achieve the deployment of infrastructure and code. So then we will and we can discuss how to deploy a test environment using Terraform start from zero in on Azure. This time we focused on VNet use Terraform deploy a resource group and one with two subnets

Download and install Terraform
To use Terraform achieve IcA we need to download and install Terraform, specifically refer to the following links:
https://www.terraform.io/downloads.html

Document preparation TF

After installing Terraform, we need to create some files:

  • main.tf: contains the call of our resources and some modules to be created
  • Variables.tf: contains the value we are creating resources
    Use Terraform deploy Azure Resources

The following are 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 value of our resources to be deployed:

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"
}

After the above-mentioned documents are ready, we should begin deployment of the command, first of all we will terraform init to initialize the implementation of the project, this process will help us to terraform download for dependency:
Use Terraform deploy Azure Resources

Then we need to do terraform plan to see what action we want to perform in Azure subscription. If we write the tf file in question, are also displayed in this step when:
Use Terraform deploy Azure Resources

Next, we need to run terraform apply to perform the deployment:
Use Terraform deploy Azure Resources

The deployment process, we need to enter yes to confirm the deployment:
Use Terraform deploy Azure Resources

Deployed as shown below:
Use Terraform deploy Azure Resources

At this point we can visit to see whether Azure corresponding resource creation success:
Use Terraform deploy Azure Resources

If you want to delete have been deleted deployment, you can run terraform destroy:
Use Terraform deploy Azure Resources

A few minutes later, all the contents have been deleted:
Use Terraform deploy Azure Resources

Guess you like

Origin blog.51cto.com/wuyvzhang/2479509