20 ‐ Azure ‐ Terraform - SanjeevOCI/Azure GitHub Wiki
Steps to Create Azure Resources Using Terraform in Azure Cloud Shell
1. Open Azure Cloud Shell
- Log in to the Azure Portal.
- Click on the Cloud Shell icon in the top-right corner.
- Select Bash as the shell environment.
2. Initialize Terraform in Azure Cloud Shell
- Verify Terraform is pre-installed by running:
terraform --version
- Create a working directory for your Terraform files:
mkdir terraform-azure && cd terraform-azure
3. Create a Terraform Configuration File
- Create a file named
main.tf
:vi main.tf
- Add the following configuration to create Azure resources:
provider "azurerm" {
features {}
subscription_id = "024da94d-4ab2-429f-9b42-a588276a504b"
}
variable "resource_group_name" {
default = "hub1_rg"
}
variable "location" {
default = "East US"
}
variable "vnet_name" {
default = "hub1_vnet"
}
variable "subnet_name" {
default = "hub1_pub_subnet"
}
# Create Resource Group
resource "azurerm_resource_group" "rg" {
name = var.resource_group_name
location = var.location
}
# Create Virtual Network
resource "azurerm_virtual_network" "vnet" {
name = var.vnet_name
address_space = ["172.1.0.0/24"]
location = var.location
resource_group_name = azurerm_resource_group.rg.name
}
# Create Subnet
resource "azurerm_subnet" "subnet" {
name = var.subnet_name
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = ["172.1.0.0/27"]
}
# Public IP
resource "azurerm_public_ip" "vm_public_ip" {
name = "linux-vm1-public-ip"
location = var.location
resource_group_name = azurerm_resource_group.rg.name
allocation_method = "Static"
sku = "Basic"
}
# NIC
resource "azurerm_network_interface" "vm_nic" {
name = "linuxserver1-nic"
location = var.location
resource_group_name = azurerm_resource_group.rg.name
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.subnet.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.vm_public_ip.id
}
}
# Linux VM
resource "azurerm_linux_virtual_machine" "vm" {
name = "LinuxServer1"
resource_group_name = azurerm_resource_group.rg.name
location = var.location
size = "Standard_B1ls"
admin_username = "azureuser"
admin_password = "Testuser@2025"
disable_password_authentication = false
network_interface_ids = [azurerm_network_interface.vm_nic.id]
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}
}
- Get the subscription id by navigating to Subscriptions directory
- Save and exit the editor.
4. Create a Terraform Output File
- Create a file named
output.tf
:vi output.tf
- Add the following in the output.tf file:
output "resource_group_name" {
value = azurerm_resource_group.rg.name
description = "The name of the resource group"
}
output "virtual_network_name" {
value = azurerm_virtual_network.vnet.name
description = "The name of the virtual network"
}
output "public_subnet_name" {
value = azurerm_subnet.subnet.name
description = "The name of the public subnet"
}
output "linux_vm_name" {
value = azurerm_linux_virtual_machine.vm.name
description = "The name of the Linux virtual machine"
}
output "linux_vm_public_ip" {
value = azurerm_public_ip.vm_public_ip.ip_address
description = "The public IP address of the Linux VM"
}
output "linux_vm_private_ip" {
value = azurerm_network_interface.vm_nic.ip_configuration[0].private_ip_address
description = "The private IP address of the Linux VM"
}
output "linux_vm_nic_id" {
value = azurerm_network_interface.vm_nic.id
description = "The ID of the network interface attached to the Linux VM"
}
5. Initialize Terraform
- Run the following command to initialize Terraform and download the Azure provider:
terraform init
6. Plan the Deployment
- Generate an execution plan to preview the resources Terraform will create:
terraform plan
7. Apply the Configuration
-
Deploy the resources to Azure:
terraform apply
-
Confirm the deployment by typing
yes
when prompted.
8. Verify the Resources
Check the created resources in the Azure Portal:
9. Clean Up Resources
- To delete the resources created by Terraform, run:
terraform destroy
- Confirm the destruction by typing
yes
when prompted.
Key Notes
- Azure Cloud Shell comes pre-installed with Terraform, so no additional setup is required.
- Always use
terraform plan
to preview changes before applying them. - Store your Terraform files in a version control system like GitHub for better management.
This step-by-step guide ensures you can quickly create and manage Azure resources using Terraform in Azure Cloud Shell.