Skip to content

Instantly share code, notes, and snippets.

@southwolf
Created December 21, 2019 04:24
Show Gist options
  • Save southwolf/270e4ac4219aa9ea64e86b6b3b8bbd1a to your computer and use it in GitHub Desktop.
Save southwolf/270e4ac4219aa9ea64e86b6b3b8bbd1a to your computer and use it in GitHub Desktop.
Create azure vm
provider "azurerm" {
version = "=1.38.0"
subscription_id = "91e51453-1db3-46d0-8329-423542539317"
}
# Create a resource group
resource "azurerm_resource_group" "rg" {
name = "tf_resource_group"
location = "westus2"
tags = {
environment = "TF Sandbox"
}
}
# Create virtual network
resource "azurerm_virtual_network" "vnet" {
depends_on = [azurerm_resource_group.rg]
name = "tf_vnet"
address_space = ["10.0.0.0/16"]
location = "westus2"
resource_group_name = azurerm_resource_group.rg.name
}
# Create subnet
resource "azurerm_subnet" "subnet" {
name = "tf_subnet"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefix = "10.0.1.0/24"
}
# Create public IP
resource "azurerm_public_ip" "publicip" {
name = "tf_public_ip"
location = "westus2"
resource_group_name = azurerm_resource_group.rg.name
allocation_method = "Dynamic"
}
# Create Network Security Group and rule
resource "azurerm_network_security_group" "nsg" {
name = "tf_nsg"
location = "westus2"
resource_group_name = azurerm_resource_group.rg.name
security_rule {
name = "SSH"
priority = 1001
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "22"
source_address_prefix = "*"
destination_address_prefix = "*"
}
}
# Create network interface
resource "azurerm_network_interface" "nic" {
name = "my_nic"
location = "westus2"
resource_group_name = azurerm_resource_group.rg.name
network_security_group_id = azurerm_network_security_group.nsg.id
ip_configuration {
name = "my_nic_config"
subnet_id = azurerm_subnet.subnet.id
private_ip_address_allocation = "dynamic"
public_ip_address_id = azurerm_public_ip.publicip.id
}
}
# Create a Linux virtual machine
resource "azurerm_virtual_machine" "vm" {
name = "myvm"
location = "westus2"
resource_group_name = azurerm_resource_group.rg.name
network_interface_ids = [azurerm_network_interface.nic.id]
vm_size = "Standard_B1ls"
storage_os_disk {
name = "my_disk"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Premium_LRS"
}
storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04.0-LTS"
version = "latest"
}
os_profile {
computer_name = "myvm"
admin_username = "MYUSER"
admin_password = "MY_PASSWORD"
}
os_profile_linux_config {
disable_password_authentication = false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment