To create an Azure virtual machine (VM) using Terraform, you need to follow these general steps:
Set up Azure credentials: Before you begin, you'll need to set up your Azure credentials to authenticate Terraform with your Azure account. You can create a service principal or use other authentication methods supported by Azure.
Create a Terraform configuration file: Create a file with a
.tf
extension (e.g.,main.tf
) to define your Terraform configuration. In this file, you'll specify the desired state of your Azure VM and other related resources.
Here's an example of a basic Terraform configuration file to create an Azure VM:
provider "azurerm" { features {} } resource "azurerm_resource_group" "example" { name = "my-resource-group" location = "East US" } resource "azurerm_virtual_network" "example" { name = "my-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 = "my-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 = "my-nic" location = azurerm_resource_group.example.location resource_group_name = azurerm_resource_group.example.name ip_configuration { name = "my-ipconfig" subnet_id = azurerm_subnet.example.id private_ip_address_allocation = "Dynamic" } } resource "azurerm_virtual_machine" "example" { name = "my-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_image_reference { publisher = "Canonical" offer = "UbuntuServer" sku = "16.04-LTS" version = "latest" } storage_os_disk { name = "my-os-disk" caching = "ReadWrite" create_option = "FromImage" managed_disk_type = "Standard_LRS" } os_profile { computer_name = "my-vm" admin_username = "adminuser" admin_password = "Password1234!" } os_profile_linux_config { disable_password_authentication = false } }
- Initialize and apply the Terraform configuration: Run the following commands in the directory where you have your Terraform configuration file:
terraform init terraform apply
The terraform init
command initializes the Terraform working directory and downloads the necessary provider plugins. The terraform apply
command creates or updates the Azure resources defined in your configuration based on the desired state.
Note: Make sure you have Terraform and the Azure provider installed before running these commands.
This is a basic example, and you can customize it further based on your specific requirements for the VM, such as specifying the VM size, storage options, networking configuration, and more. Refer to the Azure provider documentation in the Terraform website for more details and additional configuration options.
Remember to review and understand the changes that Terraform will make to your Azure resources before confirming the execution
No comments:
Post a Comment