How to Start with Terraform in Visual Studio Code and Azure from Zero

How to Start with Terraform in Visual Studio Code and Azure from Zero

If you’re new to Terraform and want to use it with Visual Studio Code (VS Code) to manage Azure resources, this guide is for you. Terraform is a powerful tool to automate infrastructure deployment, and learning it from scratch might feel overwhelming. But trust me, it's easier than it seems if you follow the right steps.

Here’s how you can get started:


Step 1: What is Terraform?

Terraform is an Infrastructure as Code (IaC) tool. It helps you define and deploy cloud resources like virtual machines, storage, and networks using code instead of clicking around in the Azure portal.


To Install Terraform on Windows/Mac , refer below link.


Step 2: Set Up Your Environment

Before jumping into coding, ensure you have these installed:

Install Visual Studio Code (VS Code):

  • Download and install VS Code from the official site.
  • It's lightweight and perfect for working with Terraform.


Install Terraform CLI:

  • Download Terraform from the Terraform website.
  • Add it to your system’s PATH (so you can run Terraform commands from anywhere in your terminal).


Install Azure CLI:

  • Download Azure CLI from Microsoft's site.
  • Azure CLI helps you interact with Azure resources and authenticate Terraform.


Install Terraform Extension in VS Code:

  • Open VS Code.
  • Go to the Extensions Marketplace (Ctrl+Shift+X or Cmd+Shift+X).
  • Search for “HashiCorp Terraform” and install it.


Step 3: Set Up an Azure Account

  • Sign up for a free trial if you don’t have an Azure account.
  • You’ll get free credits to practice Terraform.


Step 4: Create Your First Terraform Project

Now that everything is ready, let's create your first Terraform project:

  1. Create a Project Folder: Create a folder on your computer, e.g., MyTerraformProject.
  2. Initialize a Terraform File: Inside the folder, create a new file with the extension .tf. For example, main.tf. This is where you’ll define your infrastructure.

Step 5: Write Your First Terraform Configuration

Let’s create a simple virtual machine in Azure.

  1. Open main.tf in VS Code.
  2. Add the following:

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "my_rg" {
  name     = "my-first-resource-group"
  location = "East US"
}        

  • Provider: Connects Terraform with Azure.
  • Resource Group: Creates a resource group in Azure.


Step 6: Initialize Terraform

  1. Open the terminal in VS Code (Ctrl+ or Cmd+).
  2. Run:

terraform init        

  • This downloads the required provider plugins for Azure.


Step 7: Authenticate Terraform with Azure

  1. Log in to Azure using Azure CLI:

az login        

  1. Follow the instructions to authenticate.


Step 8: Plan and Apply

  1. Run:

terraform plan        

  • This shows what Terraform will create.

2. If everything looks good, run:

terraform apply        

  • Type yes to confirm.


Step 9: Verify Your Resources

  • Go to the Azure portal and check if the resource group is created.


Step 10: Clean Up Resources

To avoid unnecessary charges:

  1. Run:

terraform destroy        

  • Type yes to delete the resources.


Tips for Beginners

  1. Understand the Basics: Don’t try to memorize commands; understand the structure of .tf files.
  2. Keep Practicing: Start with small resources like resource groups, then move to complex setups.
  3. Read the Docs: Terraform's documentation is beginner-friendly.
  4. Learn by Doing: Experiment with different Azure services like VMs, storage accounts, and networking.

To view or add a comment, sign in

Explore topics