How to run Terraform Code

In this tutorial, we will be executing Terraform code to create a new Azure Resource Group.


Code for creating a Resource Group

resource "azurerm_resource_group" "web_server_rg" {

Apply Changes

Within Microsoft Visual Studio Code;

  1. Right-click our TF file. In this example, it is Main.TF.
  2. Click Open in Terminal.
  3. Within the bottom of Microsoft Visual Studio Code, we have now received a terminal.
  4. Type terraform init and Press Enter. This initialises a new or existing Terraform configuration.
  5. Now, we can pilot our Terraform code to determine the changes it will commit to our azure environment using the command terraform plan. Press Enter.
  6. You will receive output within terminal advising of changes determined;
  7. If you are satisfied with pilot results, we can commit our changes to production with the command terraform apply. Press Enter.
  8. It will prompt you with the changes to be implemented; Type yes and press Enter to commit changes to production.
  9. Our Terraform Apply code completed successfully!
  10. Upon checking Azure Portal, we can now validate that our new Resource Group was successfully created.

Revert Changes

Now if you wish to revert changes you have made, you can simply execute command terraform destroy within our Visual Studio Code terminal.

Now we can validate within Azure Portal that our resource group was successfully deleted.