Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”. This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money.

In the previous article, Cloud Infrastructure Creation -Azure Resource Group, we used TerraForm to create our first cloud infrastructure Resouce Group. In this article, we will modify the deployed infrastructure by defining additional resources that reference and add tags to resource groups.

One, code coding virtual network

Add a new resource block in the main.tf file above. The resource is the virtual network.

Resource "azurerm_virtual_network" "vnet" {name = "myTFVnet" address_space = ["10.0.0.0/16"] location = "westus2" resource_group_name = azurerm_resource_group.rg.name }Copy the code

To create a new Azure VNet, Azure requires that you specify the name of the resource group that contains VNet, the resource we created in the previous article. By referring to resource groups, we can establish dependencies between resources. TerraForm ensures that resources are created in the proper order by constructing dependency diagrams for configurations.

2. New deployment changes

After changing the configuration, we will run Terraform to deploy again, and before we officially deploy, we preview the following changes.

# Preview the deployment plan and view the change item $TerraForm PlanCopy the code

The following output is displayed:

The diagram shows that there is a resource added, no changes, no destruction. Next, execute the deployment plan.

$terraForm ApplyCopy the code

Enter yes to get the following output:

The virtual network resource Vnet is successfully created. Tip: It should be noted that in Plan and Apply, the waiting time will increase significantly as the number of resources increases, requiring patient waiting. Check the Azure Portal, as shown in the following figure, and the resource is successfully created.



Then check the relationship between the resources that can be usedterraform showCommand.

$terraform show
Copy the code

The following output is displayed: