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:
- Ensure the Remote - WSL extension is installed in VS Code.
- Launch VS Code and press `Ctrl + `` to open the terminal.
- 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:
|
|
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:
|
|
If you need to change to a different subscription, you can use the “az account set” command followed by the subscription ID:
|
|
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:
- In your WSL terminal within VS Code, navigate to the desired directory for your Terraform projects.
- Create a new folder using
mkdir my_first_terraform_project
. - 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:
|
|
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:
- Platform Communication: The provider translates Terraform’s general instructions into actions that the target platform can execute.
- Resource Management: Declaring a provider enables Terraform to understand the specific resources and services available on that platform.
- 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:
|
|
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:
|
|
Planning Your Changes ๐
Execute terraform plan
to preview the infrastructure changes without applying them. The output will show you the proposed changes:
|
|
In this sample output:
- The
+
symbol means that these resources will be created. - Properties like
name
andlocation
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
:
|
|
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:
|
|
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:
|
|
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! ๐