Change infrastructure
In the previous tutorial, you created your first infrastructure with Terraform: a resource group. Now you will modify your configuration by defining an additional resource that references your resource group and adding tags to your resource group.
Note
This tutorial builds on configuration from a previous tutorial. If you have not yet completed it, do so now.
Create a new resource
In your main.tf
file, add the resource block below to create a virtual network (VNet).
main.tf
# Create a virtual networkresource "azurerm_virtual_network" "vnet" { name = "myTFVnet" address_space = ["10.0.0.0/16"] location = "westus2" resource_group_name = azurerm_resource_group.rg.name}
To create a new Azure VNet, you have to specify the name of the resource group to contain the VNet. By referencing the resource group, you establish a dependency between the resources. Terraform ensures that resources are created in proper order by constructing a dependency graph for your configuration.
Apply your changes
After changing the configuration, run terraform apply
again to see how
Terraform will apply this change to your infrastructure. Respond yes
to the prompt to confirm the changes.
$ terraform applyazurerm_resource_group.rg: Refreshing state... [id=/subscriptions/c9ed8610-47a3-4107-a2b2-a322114dfb29/resourceGroups/myTFResourceGroup] Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # azurerm_virtual_network.vnet will be created + resource "azurerm_virtual_network" "vnet" { + address_space = [ + "10.0.0.0/16", ] + guid = (known after apply) + id = (known after apply) + location = "westus2" + name = "myTFVnet" + resource_group_name = "myTFResourceGroup" + subnet = (known after apply) + vm_protection_enabled = false } 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.vnet: Creating...azurerm_virtual_network.vnet: Creation complete after 6s [id=/subscriptions/c9ed8610-47a3-4107-a2b2-a322114dfb29/resourceGroups/myTFResourceGroup/providers/Microsoft.Network/virtualNetworks/myTFVnet] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Terraform builds an execution plan by comparing your desired state as described
in the configuration to the current state, which is saved in either the local
terraform.tfstate
file or in a remote state backend depending on your
configuration.
Modify an existing resource
In addition to creating new resources, Terraform can modify existing resources.
Open your main.tf
file. Update the azurerm_resource_group
resource in your configuration by adding the tags block as shown below:
Tip
The below snippet is formatted as a diff to give you context about what in your configuration should change. Add the content in green (exclude the leading +
sign).
main.tf
resource "azurerm_resource_group" "rg" { name = "myTFResourceGroup" location = "westus2" tags = { Environment = "Terraform Getting Started" Team = "DevOps" }}
Apply your changes
Run terraform apply
to modify your infrastructure. Respond yes
to the prompt to confirm the operation.
$ terraform applyazurerm_resource_group.rg: Refreshing state... [id=/subscriptions/c9ed8610-47a3-4107-a2b2-a322114dfb29/resourceGroups/myTFResourceGroup]azurerm_virtual_network.vnet: Refreshing state... [id=/subscriptions/c9ed8610-47a3-4107-a2b2-a322114dfb29/resourceGroups/myTFResourceGroup] Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: ~ update in-place Terraform will perform the following actions: # azurerm_resource_group.rg will be updated in-place ~ resource "azurerm_resource_group" "rg" { id = "/subscriptions/c9ed8610-47a3-4107-a2b2-a322114dfb29/resourceGroups/myTFResourceGroup" name = "myTFResourceGroup" ~ tags = { + "Environment" = "Terraform Getting Started" + "Team" = "DevOps" } # (1 unchanged attribute hidden) } Plan: 0 to add, 1 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_resource_group.rg: Modifying... [id=/subscriptions/c9ed8610-47a3-4107-a2b2-a322114dfb29/resourceGroups/myTFResourceGroup]azurerm_resource_group.rg: Modifications complete after 1s [id=/subscriptions/c9ed8610-47a3-4107-a2b2-a322114dfb29/resourceGroups/myTFResourceGroup] Apply complete! Resources: 0 added, 1 changed, 0 destroyed.
The prefix ~
means that Terraform will update the resource in-place.
Review updates to state
Use terraform show
again to see the new values associated with this resource group.
$ terraform show# azurerm_resource_group.rg:resource "azurerm_resource_group" "rg" { id = "/subscriptions/c9ed8610-47a3-4107-a2b2-a322114dfb29/resourceGroups/myTFResourceGroup" location = "westus2" name = "myTFResourceGroup" tags = { "Environment" = "Terraform Getting Started"i "Team" = "DevOps" }}##...
Run terraform state list
to get the updated list of resources managed in your workspace.
$ terraform state listazurerm_resource_group.rgazurerm_virtual_network.vnet