To run a Terraform plan, use the following command:
$ terraform plan
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_resource_group.rg will be created + resource “azurerm_resource_group” “rg” {
+ | id | = (known after apply) | |
+ | location | = “westeurope” | |
+ name | = “terraform-exercise” | ||
} | |||
Plan: | 1 | to add, 0 to change, 0 to destroy. |
Note: You didn’t use the -out option to save this plan, so Terraform can’t guarantee to take exactly these actions if you run terraform apply now.
The plan output tells us that if we run terraform apply immediately, it will create a single terraform_exercise resource group. It also outputs a note that since we did not save this plan, the subsequent application is not guaranteed to result in the same action. Meanwhile, things might have changed; therefore, Terraform will rerun plan and prompt us for yes when applying. Thus, you should save the plan to a file if you don’t want surprises.
Tip
Always save terraform plan output to a file and use the file to apply the changes. This is to avoid any last-minute surprises with things that might have changed in the background and apply not doing what it is intended to do, especially when your plan is reviewed as a part of your process.
So, let’s go ahead and save the plan to a file first using the following command:
$ terraform plan -out rg_terraform_exercise.tfplan
This time, the plan is saved to a file calledrg_terraform_exercise.tfplan. We can use this file to apply the changes subsequently.
terraform apply
To apply the changes using the plan file, run the following command:
$ terraform apply “rg_terraform_exercise.tfplan”
azurerm_resource_group.rg: Creating…
azurerm_resource_group.rg: Creation complete after 2s [id=/subscriptions/id/ resourceGroups/terraform-exercise]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
And that’s it! Terraform has applied the configuration. Let’s use the Azure CLI to verify whether the resource group is created.
Run the following command to list all resource groups within your subscription:
$ az group list
…
“id”: “/subscriptions/id/resourceGroups/terraform-exercise”,
“location”: “westeurope”,
“name”: “terraform-exercise”,
…
We see that our resource group is created and within the list.
There might be instances when apply is partially successful. In that case, Terraform will automatically taint resources it believes weren’t created successfully. Such resources will be recreated automatically in the next run. If you want to taint a resource for recreation manually, you can use the terraform taint command:
$ terraform taint <resource>
Suppose we want to destroy the resource group as we no longer need it. We can use terraform destroy for that.