Skip to content

Instantly share code, notes, and snippets.

@buniumasta
Created December 21, 2020 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buniumasta/e4c76a17445aede88f472ba3f1997b3a to your computer and use it in GitHub Desktop.
Save buniumasta/e4c76a17445aede88f472ba3f1997b3a to your computer and use it in GitHub Desktop.
Terraform Single Linux VM with SSH access.
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "main" {
name = "${var.prefix}-resources"
location = var.location
}
resource "azurerm_virtual_network" "main" {
name = "${var.prefix}-network"
address_space = ["10.30.0.0/16"]
location = azurerm_resource_group.main.location
resource_group_name = azurerm_resource_group.main.name
}
resource "azurerm_subnet" "internal" {
name = "internal"
resource_group_name = azurerm_resource_group.main.name
virtual_network_name = azurerm_virtual_network.main.name
address_prefixes = ["10.30.2.0/24"]
}
# Create public IPs
resource "azurerm_public_ip" "terrademopublicip" {
name = "${var.prefix}-public-ip-nic"
resource_group_name = azurerm_resource_group.main.name
location = azurerm_resource_group.main.location
allocation_method = "Dynamic"
tags = {
environment = "Terraform Demo"
}
}
resource "azurerm_network_interface" "mypublic" {
name = "${var.prefix}-public-nic"
resource_group_name = azurerm_resource_group.main.name
location = azurerm_resource_group.main.location
ip_configuration {
name = "public"
subnet_id = azurerm_subnet.internal.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.terrademopublicip.id
}
}
resource "azurerm_network_interface" "main" {
name = "${var.prefix}-nic"
resource_group_name = azurerm_resource_group.main.name
location = azurerm_resource_group.main.location
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.internal.id
private_ip_address_allocation = "Dynamic"
}
}
resource "azurerm_network_security_group" "public" {
name = "${var.prefix}.SecurityGroupPublic"
location = azurerm_resource_group.main.location
resource_group_name = azurerm_resource_group.main.name
security_rule {
name = "AllowSSH"
priority = 300
direction = "Inbound"
access = "Allow"
protocol = "*"
source_port_range = "*"
destination_port_range = "22"
source_address_prefix = "*"
destination_address_prefix = "*"
}
tags = {
prefix = var.prefix,
environment = "Production"
}
}
resource "azurerm_network_interface_security_group_association" "public" {
network_interface_id = azurerm_network_interface.mypublic.id
network_security_group_id = azurerm_network_security_group.public.id
}
resource "azurerm_linux_virtual_machine" "main" {
name = "${var.prefix}-vm"
resource_group_name = azurerm_resource_group.main.name
location = azurerm_resource_group.main.location
size = "Standard_F2"
admin_username = var.username
admin_password = var.password
disable_password_authentication = false
network_interface_ids = [
azurerm_network_interface.mypublic.id,
azurerm_network_interface.main.id
]
source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}
os_disk {
storage_account_type = "Standard_LRS"
caching = "ReadWrite"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment