Z ‐ CREATE VM IN AZURE USING TERRAFORM - SanjeevOCI/Ocidocs GitHub Wiki
No, Azure Cloud Shell is not mandatory to create a Virtual Machine (VM) using Terraform in Azure. You can use any environment where Terraform is installed and authenticated with Azure. However, Azure Cloud Shell is a convenient option because it comes pre-installed with Terraform, Azure CLI, and other tools, making it a ready-to-use environment for managing Azure resources.
Options to Create a VM Using Terraform in Azure
1. Using Azure Cloud Shell
- Why Use Cloud Shell?
- Pre-installed Terraform and Azure CLI.
- No need to install or configure tools locally.
- Direct access to your Azure subscription.
- Steps:
- Open Azure Cloud Shell from the Azure Portal.
- Choose Bash or PowerShell (Bash is recommended for Terraform).
- Upload your Terraform configuration files (
main.tf,variables.tf, etc.) to the Cloud Shell environment. - Run the Terraform commands:
terraform init terraform plan terraform apply
2. Using Local Machine
- Why Use a Local Machine?
- Full control over the environment.
- Useful for integrating Terraform with version control systems like Git.
- Steps:
- Install Terraform on your local machine from Terraform's official website.
- Install the Azure CLI and authenticate:
az login az account set --subscription "<subscription-id>" - Write your Terraform configuration files (
main.tf,variables.tf, etc.). - Run the Terraform commands from your terminal:
terraform init terraform plan terraform apply
3. Using CI/CD Pipelines
- Why Use CI/CD Pipelines?
- Automates the deployment process.
- Useful for managing infrastructure as code in a collaborative environment.
- Steps:
- Use tools like Azure DevOps Pipelines, GitHub Actions, or Jenkins.
- Define Terraform steps in the pipeline (e.g.,
terraform init,terraform apply). - Store Terraform configuration files in a version control system (e.g., GitHub, Azure Repos).
- Trigger the pipeline to deploy the VM.
When to Use Azure Cloud Shell?
-
Use Cloud Shell if:
- You want a quick and ready-to-use environment.
- You don’t want to install Terraform or Azure CLI locally.
- You are working directly from the Azure Portal.
-
Use Local Machine or CI/CD Pipelines if:
- You need more control over the environment.
- You are working in a team and need version control.
- You want to automate deployments.
Conclusion
Azure Cloud Shell is a convenient option for creating VMs using Terraform, but it is not mandatory. You can use your local machine or CI/CD pipelines depending on your workflow and requirements.
==============================================================================================================
Here are the step-by-step instructions to create a Virtual Machine (VM) in Azure using Terraform:
1. Prerequisites
- Install Terraform: Download and install Terraform from Terraform's official website.
- Install Azure CLI: Install the Azure CLI to authenticate Terraform with Azure.
- Azure Subscription: Ensure you have an active Azure subscription.
- Authenticate with Azure:
az login az account set --subscription "<subscription-id>"
2. Create a Terraform Configuration File
- Create a directory for your Terraform project and navigate to it:
mkdir terraform-azure-vm cd terraform-azure-vm - Create a file named
main.tfand define the configuration.
3. Define the Terraform Configuration
Below is an example configuration to create a VM in Azure:
// Step 1: Specify the Azure provider
provider "azurerm" {
features {}
}
// Step 2: Create a Resource Group
resource "azurerm_resource_group" "example" {
name = "example-resources"
location = "East US"
}
// Step 3: Create a Virtual Network
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
}
// Step 4: Create a Subnet
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"]
}
// Step 5: Create a Network Interface
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"
}
}
// Step 6: Create a Virtual Machine
resource "azurerm_windows_virtual_machine" "example" {
name = "example-vm"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
size = "Standard_B2s"
admin_username = "azureuser"
admin_password = "P@ssw0rd1234!"
network_interface_ids = [azurerm_network_interface.example.id]
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2019-Datacenter"
version = "latest"
}
}
4. Initialize Terraform
- Run the following command to initialize Terraform and download the Azure provider:
terraform init
5. Validate the Configuration
- Validate the configuration to ensure there are no syntax errors:
terraform validate
6. Plan the Deployment
- Generate an execution plan to preview the resources Terraform will create:
terraform plan
7. Apply the Configuration
- Apply the configuration to create the VM and associated resources:
terraform apply - Confirm the prompt by typing
yes.
8. Verify the VM in Azure
- Log in to the Azure Portal and navigate to Resource Groups.
- Check the resource group (
example-resources) to verify the VM and associated resources (e.g., VNet, NIC, etc.) have been created.
9. Clean Up Resources (Optional)
- To destroy the VM and all associated resources, run:
terraform destroy - Confirm the prompt by typing
yes.
Explanation of Key Components
- Provider: Specifies the Azure provider (
azurerm) for Terraform. - Resource Group: Logical container for Azure resources.
- Virtual Network (VNet): Network for the VM.
- Subnet: Subdivision of the VNet.
- Network Interface (NIC): Connects the VM to the network.
- Virtual Machine: The actual VM resource with OS, size, and credentials.
Best Practices
- Secure Credentials: Use Azure Key Vault or environment variables to store sensitive data like passwords.
- Remote State: Store the Terraform state file in Azure Blob Storage for collaboration.
- Modularize Code: Use Terraform modules to organize reusable components.
This setup creates a basic VM in Azure using Terraform. You can extend it by adding storage, public IPs, or additional configurations as needed.