Azure

Terraform On Azure – Creating A Azure Digital Machine

Introduction

Azure Digital Machine is a well-liked Infrastructure-as-a-service providing and you should utilize it to host on-premises functions with only a raise and shift method. This is among the often used Azure providers. On this article, we’ll learn to spin up Azure Digital Machine utilizing Terraform.

Within the earlier articles, we realized the fundamentals of Terraform and we created an Azure WebApp utilizing Terraform. The next are the hyperlinks to the earlier articles.

Create Azure Digital Machine utilizing Terraform 

Allow us to create a easy Home windows-based Azure Digital Machine. The next are the sources that we should create earlier than spinning out a Digital Machine. 

  • Useful resource Group
  • Digital Community with Subnet
  • Community Interface card

Let begin with creating the conditions sources after which we will create the Azure Digital Machine.

Log in to the Azure portal at https://portal.azure.com. Allow us to use Azure Cloud Shell to create sources utilizing Terraform. Azure Cloud Shell has Terraform put in and you needn’t do any set up or configuration to work with Terraform. 

As soon as the Azure Cloud Shell opens up, choose Bash. Allow us to begin creating scripts to create an Azure Digital Machine. We are able to use a nano editor to create the Infrastructure as Code script for the Digital Machine utilizing Terraform. 

Terraform On Azure - Creating A Azure Virtual Machine

Execute the next command to open a nano editor and create a file named myterraformscript.tf.

nano myterraformscript.tf

Add the next code to the nano editor. This might create a Useful resource Group. Substitute {ResourceGroup} with the identify of your Useful resource Group that you’re planning to create.

terraform {  
  required_providers {  
    azurerm = {  
      supply = "hashicorp/azurerm"  
    }  
  }  
}  
supplier "azurerm" {  
  options {}  
}  
useful resource "azurerm_resource_group" "{ResourceGroup}" {  
  identify = "{ResourceGroup}"  
  location = "eastus"  
} 

Add the next code in nano editor to create a Digital Community and a Subnet. Substitute {ResourceGroup} with the identify of your Useful resource Group, {VirtualNetwork} with the identify of your Digital Community and {Subnet} with the identify of your Subnet. 

useful resource "azurerm_virtual_network" "{VirtualNetwork}" {
  identify                = "{VirtualNetwork}"
  address_space       = ["10.1.0.0/24"]
  location            = azurerm_resource_group.{ResourceGroup}.location
  resource_group_name = azurerm_resource_group.{ResourceGroup}.identify
}

useful resource "azurerm_subnet" "{Subnet}" {
  identify                 = "{Subnet}"
  resource_group_name  = azurerm_resource_group.{ResourceGroup}.identify
  virtual_network_name = azurerm_virtual_network.{VirtualNetwork}.identify
  address_prefixes     = ["10.1.0.0/26"]
}

Add the next code to create a Community Interface Card. Substitute {ResourceGroup} with the identify of your Useful resource Group, {NIC} with the identify of your Community Interface Card, {Subnet} with the identify of the Subnet, and {IP} with the identify of your IP useful resource related to the Community Interface Card.

useful resource "azurerm_network_interface" "{NIC}" {
  identify                = "{NIC}"
  location            = azurerm_resource_group.{ResourceGroup}.location
  resource_group_name = azurerm_resource_group.{ResourceGroup}.identify

  ip_configuration {
    identify                          = "{IP}"
    subnet_id                     = azurerm_subnet.{Subnet}.id
    private_ip_address_allocation = "Dynamic"
  }
}

Add the next code to create a Digital Machine. Substitute {ResourceGroup} with the identify of your Useful resource Group, {NIC} with the identify of your Community Interface Card. You want to present the VM SKU, Working System, and credentials within the script. 

useful resource "azurerm_windows_virtual_machine" "{VirtualMachine}" {
  identify                = "{VirtualMachine}"
  resource_group_name = azurerm_resource_group.{ResourceGroup}.identify
  location            = azurerm_resource_group.{ResourceGroup}.location
  dimension                = "Standard_F2"
  admin_username      = "[Provide a Username]"
  admin_password      = "[Provide your Password]"
  network_interface_ids = [
    azurerm_network_interface.{NIC}.id,
  ]

  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }

  source_image_reference {
    writer = "MicrosoftWindowsServer"
    supply     = "WindowsServer"
    sku       = "2016-Datacenter"
    model   = "newest"
  }
}

The next is the script file that you just created. You’ll be able to discuss with the hooked up script file and check out the pattern.

terraform {  
  required_providers {  
    azurerm = {  
      supply = "hashicorp/azurerm"  
    }  
  }  
}  
supplier "azurerm" {  
  options {}  
}  
useful resource "azurerm_resource_group" "{ResourceGroup}" {  
  identify = "{ResourceGroup}"  
  location = "eastus"  
} 
useful resource "azurerm_virtual_network" "{VirtualNetwork}" {
  identify                = "{VirtualNetwork}"
  address_space       = ["10.1.0.0/24"]
  location            = azurerm_resource_group.{ResourceGroup}.location
  resource_group_name = azurerm_resource_group.{ResourceGroup}.identify
}

useful resource "azurerm_subnet" "{Subnet}" {
  identify                 = "{Subnet}"
  resource_group_name  = azurerm_resource_group.{ResourceGroup}.identify
  virtual_network_name = azurerm_virtual_network.{VirtualNewtwork}.identify
  address_prefixes     = ["10.1.0.0/26"]
}

useful resource "azurerm_network_interface" "{NIC}" {
  identify                = "{NIC}"
  location            = azurerm_resource_group.{ResourceGroup}.location
  resource_group_name = azurerm_resource_group.{ResourceGroup}.identify

  ip_configuration {
    identify                          = "{IP}"
    subnet_id                     = azurerm_subnet.{Subnet}.id
    private_ip_address_allocation = "Dynamic"
  }
}

useful resource "azurerm_windows_virtual_machine" "{VirtualMachine}" {
  identify                = "{VirtualMachine}"
  resource_group_name = azurerm_resource_group.{ResourceGroup}.identify
  location            = azurerm_resource_group.{ResourceGroup}.location
  dimension                = "Standard_F2"
  admin_username      = "Present username"
  admin_password      = "Present Password"
  network_interface_ids = [
    azurerm_network_interface.{NIC}.id,
  ]

  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }

  source_image_reference {
    writer = "MicrosoftWindowsServer"
    supply     = "WindowsServer"
    sku       = "2016-Datacenter"
    model   = "newest"
  }
}

Run the next command to provoke Terraform. This might fetch all dependencies wanted to execute the Terraform script.

terraform init

Now allow us to create an execution plan for Terraform. Allow us to present the identify of the execution plan within the out parameter.

terraform plan -out myvm.tfplan

Execute the execution plan utilizing the next command. The Digital Machine will get created.

terraform apply "myvm.tfplan"

Conclusion

On this article, we realized the best way to create an Azure Digital Machine. Within the subsequent article, we’ll learn to create a Storage Account utilizing Terraform scripts. 

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button