Featured image of post Taking the Plunge: Your First Terraform Project on Azure - Terraform on Azure Part 3

Taking the Plunge: Your First Terraform Project on Azure - Terraform on Azure Part 3

Mastering Azure Infrastructure with Terraform: Your Ultimate Guide to Initial Setup, Resource Management, and Clean-Up!

Taking the Plunge - Your First Terraform Project on Azure ๐ŸŒŠ

Kickstarting Our Terraform Journey on Azure ๐Ÿš€

Welcome back, everyone! ๐ŸŒŸ We’re continuing our exciting Terraform journey. In Part 1, we covered the fundamentals of Terraform and why it’s a game-changer for infrastructure management. In Part 2, we set up our local development environment. Today, we’re diving deep into hands-on experience with Terraform configurations tailored for Azure.

Prerequisites: What You Need ๐Ÿ“‹

  • Azure Subscription: If you don’t have one, create a free account using this link. Hands-on practice is crucial for mastering Terraform!
  • Tools: This post uses VS Code, Azure CLI, and WSL. To set up your local development environment, check out my previous blog post. Alternatively, Azure CloudShell can also be used, as discussed in the prior article.

Connecting VS Code with WSL: Seamless Development ๐Ÿ”—

Integrating VS Code with WSL enhances your development experience and provides seamless access to Linux-based tools. Here’s how to set it up:

  1. Ensure the Remote - WSL extension is installed in VS Code.
  2. Launch VS Code and press `Ctrl + `` to open the terminal.
  3. In the terminal, type wsl to switch to the WSL environment.

VS Code is now connected to WSL, enabling a more integrated development workflow.

Logging Into Azure: Your Gateway to the Cloud โ˜๏ธ

To interact with Azure resources via the command line, you first need to log into your Azure account using the az login command. Here’s how it works:

1
2
3
$ az login

To sign in, use a web browser to open the page <https://microsoft.com/devicelogin> and enter the code ABCD1234 to authenticate.

After signing in via the browser, wait for the terminal to confirm a successful login before closing the browser. The command line will display your connected Azure subscriptions.

Note: If you’re using Azure CloudShell, you’re already logged in through the Azure portal, so there’s no need to run az login. However, always check which subscription you’re working in.

If you have access to multiple subscriptions. Always verify that you are in the correct subscription before proceeding to avoid any accidental changes. You can use the “az account show” command to view details about the subscription you are currently using:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
$ az account show

{
  "environmentName": "AzureCloud",
  "id": "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
  "isDefault": true,
  "name": "Free Trial",
  "state": "Enabled",
  "tenantId": "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
  "user": {
    "name": "example@example.com",
    "type": "user"
  }
}

If you need to change to a different subscription, you can use the “az account set” command followed by the subscription ID:

1
$ az account set --subscription="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"

Creating Your Terraform Project Space: Organizing for Success ๐Ÿ“

Organizing your Terraform projects in separate folders is a best practice for better code management and reduced configuration conflicts. Here’s how to set up your project folder:

  1. In your WSL terminal within VS Code, navigate to the desired directory for your Terraform projects.
  2. Create a new folder using mkdir my_first_terraform_project.
  3. Navigate into the folder with cd my_first_terraform_project.

Your First Configuration File: Defining the Azure Provider ๐Ÿ› ๏ธ

Create a new file named main.tf in your project folder and add the following configuration:

1
2
3
4
provider "azurerm" {
  version = "=3.72.0"
  features {}
}

This configuration tells Terraform to use the Azure provider (azurerm) version 3.72.0. The provider acts as a bridge between Terraform and Azure, translating Terraform’s instructions into actions Azure can execute.

What Role Does a Provider Play? ๐ŸŒ‰

A provider in Terraform serves multiple functions:

  1. Platform Communication: The provider translates Terraform’s general instructions into actions that the target platform can execute.
  2. Resource Management: Declaring a provider enables Terraform to understand the specific resources and services available on that platform.
  3. Versioning: Providers get updated over time, introducing new features, fixes, or even breaking changes. Specifying a provider version ensures that your infrastructure remains consistent.

For a deeper understanding of providers, refer to the official Terraform documentation.

Creating Your First Resource: An Azure Resource Group ๐Ÿ—„๏ธ

Let’s create an Azure Resource Group, which serves as a container for other resources. Add the following code to your main.tf file:

1
2
3
4
resource "azurerm_resource_group" "my_rg" {
  name     = "iaMachsRG"
  location = "East US"
}

This code declares a new resource of type azurerm_resource_group with the name “iaMachsRG” in the “East US” location. In real-world scenarios, choose meaningful names and appropriate locations for your resources.

Getting Terraform Ready: Initializing and Planning ๐ŸŽฌ

Before applying any configurations, you need to initialize your Terraform workspace and review the execution plan.

Initializing Terraform ๐Ÿš€

Run terraform init to install the required provider plugins and initialize the working directory. The output should look similar to:

1
2
3
4
5
6
7
8
Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/azurerm...
- Installing hashicorp/azurerm v3.72.0...
- Installed hashicorp/azurerm v3.72.0 (signed by HashiCorp)

Terraform has been successfully initialized!

Planning Your Changes ๐Ÿ“

Execute terraform plan to preview the infrastructure changes without applying them. The output will show you the proposed changes:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
Terraform will perform the following actions:

  # azurerm_resource_group.my_rg will be created
  + resource "azurerm_resource_group" "my_rg" {
      + id       = (known after apply)
      + location = "East US"
      + name     = "iaMachsRG"
    }

Plan: 1 to add, 0 to change, 0 to delete.

In this sample output:

  • The + symbol means that these resources will be created.
  • Properties like name and location are displayed, indicating how the new resource will be configured.
  • The summary at the end Plan: 1 to add, 0 to change, 0 to delete gives us a quick overview of what changes will occur when you apply this configurationin this case 1 new resource will be added, Zero changed, Zero deleted.

Always run these commands before applying changes to ensure a smooth and predictable deployment.

Making Terraform Changes Real: Applying Your Configuration ๐Ÿš€

To finalize the changes and create the resource group, run terraform apply:

1
2
3
4
5
6
7
$ terraform apply

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

Review the execution plan carefully before entering yes to proceed. Some resource changes are irreversible, so make sure you understand the implications.

Verifying Your Work: Ensuring Resource Provisioning โœ…

After applying your configuration, verify that the resource group was created successfully using Azure CLI:

1
2
3
4
5
$ az group list --output table

Name               Location    Status
-----------------  ----------  --------
iaMachsRG          East US     Succeeded

Regular verification is a good practice to ensure resources are provisioned as expected.

Wrapping Up and Cleaning: Responsible Resource Management ๐Ÿงน

Once you’re done experimenting, it’s crucial to clean up your resources to avoid unexpected costs. Run terraform destroy to remove the infrastructure you’ve set up:

1
2
3
4
5
6
7
$ terraform destroy

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

Terraform will ask for confirmation before destroying the resources. Enter yes to proceed with the clean-up.

Summary ๐Ÿ“˜

In this article, we covered:

  • Setting up your Terraform project and integrating VS Code with WSL.
  • Logging into Azure and selecting the correct subscription.
  • Creating a Terraform configuration file with the Azure provider.
  • Understanding the role of providers in Terraform.
  • Provisioning an Azure Resource Group using Terraform.
  • Initializing, planning, and applying Terraform configurations.
  • Verifying resource creation and cleaning up after experimentation.

This hands-on introduction should give you a solid foundation for managing Azure infrastructure with Terraform. Stay tuned for future posts where we’ll explore more advanced Terraform concepts and real-world use cases! ๐ŸŒŸ

Happy Terraforming! ๐Ÿš€