How to use terraform from Azure cloud shell? Do we need to install an additional package to use terraform? Nope. Azure provides commonly used CLI tools including Linux shell interpreters, PowerShell modules, Azure tools, text editors, source control, build tools (ex: terraform), and more in cloud shell. This makes azure builders to quickly deploy Azure resources using ARM templates or terraform. This article will demonstrate how to use terraform and deploy vNet using a piece of code.
1.Login to Azure portal and launch cloud shell.
2. Create the required directories for terraform. In this example, we will see how to create vNet using terraform code.
UA@Azure:~$ mkdir -p terraform/vnet/ UA@Azure:~$ cd terraform/vnet/ UA@Azure:~/clouddrive/terraform/vnet$ terraform -version Terraform v0.12.25 UA@Azure:~/terraform/vnet$
3. Prepare the terraform code to provision vNet. Use vi editor to create the following file.
provider "azurerm" {
version = "~>2.0"
features {}
}
resource "azurerm_virtual_network" "main" {
name = "UAVnet"
address_space = ["10.125.0.0/16"]
location = "westus2"
resource_group_name = "UArg"
tags = { environment = "UnixArena test" }
}
4. List the newly create file.
UA@Azure:~/clouddrive/terraform/vnet$ ls -lrt total 2 -rwxrwxrwx 1 lingesh lingesh 317 May 24 17:16 create_vnet.tf UA@Azure:~/clouddrive/terraform/vnet$
5. Initialize the terraform binaries. This step will download the required plugins.
UA@Azure:~/terraform/vnet$ terraform init Initializing the backend… Initializing provider plugins… Checking for available provider plugins… Downloading plugin for provider "azurerm" (hashicorp/azurerm) 2.11.0… Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary. UA@Azure:~/terraform/vnet$
6. Validate the terraform plan.
UA@Azure:~/terraform/vnet$ terraform plan Refreshing Terraform state in-memory prior to plan… The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage. An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: create Terraform will perform the following actions: # azurerm_virtual_network.main will be created resource "azurerm_virtual_network" "main" { address_space = [ "10.125.0.0/16", ] guid = (known after apply) id = (known after apply) location = "westus2" name = "UAVnet" resource_group_name = "UArg" subnet = (known after apply) tags = { "environment" = "UnixArena test" } } Plan: 1 to add, 0 to change, 0 to destroy. Note: You didn't specify an "-out" parameter to save this plan, so Terraform can't guarantee that exactly these actions will be performed if "terraform apply" is subsequently run. UA@Azure:~/terraform/vnet$
7. Create the azure resource by executing terraform apply command.
UA@Azure:~/terraform/vnet$ terraform apply An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: create Terraform will perform the following actions: # azurerm_virtual_network.main will be created resource "azurerm_virtual_network" "main" { address_space = [ "10.125.0.0/16", ] guid = (known after apply) id = (known after apply) location = "westus2" name = "UAVnet" resource_group_name = "UArg" subnet = (known after apply) tags = { "environment" = "UnixArena test" } } Plan: 1 to add, 0 to change, 0 to destroy. Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yes azurerm_virtual_network.main: Creating… azurerm_virtual_network.main: Still creating… [10s elapsed] azurerm_virtual_network.main: Creation complete after 12s [id=/subscriptions/585051ec-7aa0-48ab-a172-d1260ad72ee5/resourceGroups/UArg/providers/Microsoft.Network/virtualNetworks/UAVnet] Apply complete! Resources: 1 added, 0 changed, 0 destroyed. UA@Azure:~/terraform/vnet$
8. You could go back and check in the azure portal to validate the resource which we had deployed now.
Hope this article is informative to you. Please share your feedback.
Leave a Reply