In this section, we will explore how to use Terraform to provision resources on Microsoft Azure. By the end of this module, you will be able to create, manage, and destroy Azure resources using Terraform.
Key Concepts
- Azure Provider: The Azure provider is used to interact with the many resources supported by Azure.
- Resource Group: A container that holds related resources for an Azure solution.
- Virtual Network (VNet): A representation of your own network in the cloud.
- Subnets: Segments of a VNet that can contain resources.
- Virtual Machines (VMs): Compute resources that can run applications and services.
Prerequisites
- An active Azure subscription.
- Azure CLI installed and configured.
- Basic understanding of Terraform configuration files.
Step-by-Step Guide
- Setting Up the Azure Provider
First, you need to configure the Azure provider in your Terraform configuration file.
- Creating a Resource Group
A resource group is a logical container for Azure resources.
- Creating a Virtual Network (VNet)
A VNet is a fundamental building block for your private network in Azure.
resource "azurerm_virtual_network" "example" { name = "example-vnet" address_space = ["10.0.0.0/16"] location = azurerm_resource_group.example.location resource_group_name = azurerm_resource_group.example.name }
- Creating Subnets
Subnets are segments of a VNet that can contain resources.
resource "azurerm_subnet" "example" { name = "example-subnet" resource_group_name = azurerm_resource_group.example.name virtual_network_name = azurerm_virtual_network.example.name address_prefixes = ["10.0.1.0/24"] }
- Creating a Virtual Machine (VM)
A VM is a compute resource that can run applications and services.
resource "azurerm_network_interface" "example" { name = "example-nic" location = azurerm_resource_group.example.location resource_group_name = azurerm_resource_group.example.name ip_configuration { name = "internal" subnet_id = azurerm_subnet.example.id private_ip_address_allocation = "Dynamic" } } resource "azurerm_virtual_machine" "example" { name = "example-vm" location = azurerm_resource_group.example.location resource_group_name = azurerm_resource_group.example.name network_interface_ids = [azurerm_network_interface.example.id] vm_size = "Standard_DS1_v2" storage_os_disk { name = "example-os-disk" caching = "ReadWrite" create_option = "FromImage" managed_disk_type = "Standard_LRS" } storage_image_reference { publisher = "Canonical" offer = "UbuntuServer" sku = "18.04-LTS" version = "latest" } os_profile { computer_name = "example-vm" admin_username = "adminuser" admin_password = "Password1234!" } os_profile_linux_config { disable_password_authentication = false } }
- Applying the Configuration
To apply the configuration and create the resources, run the following commands:
Practical Exercise
Exercise: Create a simple Azure infrastructure with a resource group, a virtual network, a subnet, and a virtual machine.
- Create a new directory for your Terraform configuration files.
- Write the configuration files as shown in the examples above.
- Initialize Terraform using
terraform init
. - Apply the configuration using
terraform apply
.
Solution:
provider "azurerm" { features {} } resource "azurerm_resource_group" "example" { name = "example-resources" location = "West Europe" } resource "azurerm_virtual_network" "example" { name = "example-vnet" address_space = ["10.0.0.0/16"] location = azurerm_resource_group.example.location resource_group_name = azurerm_resource_group.example.name } resource "azurerm_subnet" "example" { name = "example-subnet" resource_group_name = azurerm_resource_group.example.name virtual_network_name = azurerm_virtual_network.example.name address_prefixes = ["10.0.1.0/24"] } resource "azurerm_network_interface" "example" { name = "example-nic" location = azurerm_resource_group.example.location resource_group_name = azurerm_resource_group.example.name ip_configuration { name = "internal" subnet_id = azurerm_subnet.example.id private_ip_address_allocation = "Dynamic" } } resource "azurerm_virtual_machine" "example" { name = "example-vm" location = azurerm_resource_group.example.location resource_group_name = azurerm_resource_group.example.name network_interface_ids = [azurerm_network_interface.example.id] vm_size = "Standard_DS1_v2" storage_os_disk { name = "example-os-disk" caching = "ReadWrite" create_option = "FromImage" managed_disk_type = "Standard_LRS" } storage_image_reference { publisher = "Canonical" offer = "UbuntuServer" sku = "18.04-LTS" version = "latest" } os_profile { computer_name = "example-vm" admin_username = "adminuser" admin_password = "Password1234!" } os_profile_linux_config { disable_password_authentication = false } }
Common Mistakes and Tips
- Incorrect Provider Configuration: Ensure the Azure provider is correctly configured with the necessary features.
- Resource Naming: Use meaningful names for resources to avoid confusion.
- Location Consistency: Ensure all resources are created in the same location to avoid latency and additional costs.
- Security: Never hard-code sensitive information like passwords in your configuration files. Use environment variables or secret management tools.
Conclusion
In this section, you learned how to provision Azure resources using Terraform. You created a resource group, a virtual network, a subnet, and a virtual machine. This foundational knowledge will enable you to build more complex Azure infrastructures using Terraform. In the next module, we will explore provisioning resources on Google Cloud Platform (GCP).
Terraform Course
Module 1: Introduction to Terraform
Module 2: Terraform Configuration Language
Module 3: State Management
Module 4: Terraform Modules
Module 5: Provisioning Resources
- Provisioning Basics
- Provisioning AWS Resources
- Provisioning Azure Resources
- Provisioning GCP Resources
Module 6: Advanced Terraform Features
Module 7: Terraform Best Practices
Module 8: Terraform in CI/CD
- Integrating Terraform with CI/CD
- Automating Terraform with Jenkins
- Using Terraform with GitHub Actions
- Terraform Cloud and Enterprise