0% found this document useful (0 votes)
48 views2 pages

Terraform

This document defines Terraform configuration to deploy an Azure virtual machine (VM) along with associated networking resources. It defines variables for location, resource group name, VM name, and credentials. It then creates a resource group, VM, network interface, virtual network, and subnet, assigning them default names and connecting them according to common conventions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views2 pages

Terraform

This document defines Terraform configuration to deploy an Azure virtual machine (VM) along with associated networking resources. It defines variables for location, resource group name, VM name, and credentials. It then creates a resource group, VM, network interface, virtual network, and subnet, assigning them default names and connecting them according to common conventions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

# Define the provider and its configuration

provider "azurerm" {
features {}
}

# Define variables
variable "location" {
description = "Azure region where the resources will be created"
default = "eastus" # Change this to your preferred region
}

variable "resource_group_name" {
description = "Name of the Azure resource group"
default = "my-resource-group" # Change this to your preferred name
}

variable "vm_name" {
description = "Name of the virtual machine"
default = "my-vm" # Change this to your preferred name
}

variable "admin_username" {
description = "Admin username for the virtual machine"
default = "adminuser" # Change this to your preferred username
}

variable "admin_password" {
description = "Admin password for the virtual machine"
default = "Password12345!" # Change this to your preferred password
}

# Create a resource group


resource "azurerm_resource_group" "my_resource_group" {
name = var.resource_group_name
location = var.location
}

# Create a virtual machine


resource "azurerm_virtual_machine" "my_vm" {
name = var.vm_name
location = azurerm_resource_group.my_resource_group.location
resource_group_name = azurerm_resource_group.my_resource_group.name
network_interface_ids = [azurerm_network_interface.my_nic.id]

vm_size = "Standard_DS1_v2"
delete_os_disk_on_termination = true

storage_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2019-Datacenter"
version = "latest"
}

storage_os_disk {
name = "${var.vm_name}-osdisk"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}

os_profile {
computer_name = var.vm_name
admin_username = var.admin_username
admin_password = var.admin_password
}

os_profile_windows_config {
provision_vm_agent = true
}

tags = {
environment = "dev"
}
}

# Create a network interface


resource "azurerm_network_interface" "my_nic" {
name = "${var.vm_name}-nic"
location = azurerm_resource_group.my_resource_group.location
resource_group_name = azurerm_resource_group.my_resource_group.name

ip_configuration {
name = "my-nic-ipconfig"
subnet_id = azurerm_subnet.my_subnet.id
private_ip_address_allocation = "Dynamic"
}
}

# Create a virtual network


resource "azurerm_virtual_network" "my_vnet" {
name = "my-vnet"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.my_resource_group.location
resource_group_name = azurerm_resource_group.my_resource_group.name

tags = {
environment = "dev"
}
}

# Create a subnet
resource "azurerm_subnet" "my_subnet" {
name = "my-subnet"
resource_group_name = azurerm_resource_group.my_resource_group.name
virtual_network_name = azurerm_virtual_network.my_vnet.name
address_prefixes = ["10.0.1.0/24"]
}

You might also like