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).
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 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).
Apply your changes
Run terraform apply
to modify your infrastructure. Respond yes
to the prompt to confirm the operation.
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.
Run terraform state list
to get the updated list of resources managed in your workspace.