diff --git a/quickstart/101-vm-with-infrastructure/main.tf b/quickstart/101-vm-with-infrastructure/main.tf index e69de29b..f323361c 100644 --- a/quickstart/101-vm-with-infrastructure/main.tf +++ b/quickstart/101-vm-with-infrastructure/main.tf @@ -0,0 +1,131 @@ +resource "random_pet" "rg_name" { + prefix = var.resource_group_name_prefix +} + +resource "azurerm_resource_group" "rg" { + location = var.resource_group_location + name = random_pet.rg_name.id +} + +# Create virtual network +resource "azurerm_virtual_network" "myterraformnetwork" { + name = "myVnet" + address_space = ["10.0.0.0/16"] + location = azurerm_resource_group.rg.location + resource_group_name = azurerm_resource_group.rg.name +} + +# Create subnet +resource "azurerm_subnet" "myterraformsubnet" { + name = "mySubnet" + resource_group_name = azurerm_resource_group.rg.name + virtual_network_name = azurerm_virtual_network.myterraformnetwork.name + address_prefixes = ["10.0.1.0/24"] +} + +# Create public IPs +resource "azurerm_public_ip" "myterraformpublicip" { + name = "myPublicIP" + location = azurerm_resource_group.rg.location + resource_group_name = azurerm_resource_group.rg.name + allocation_method = "Dynamic" +} + +# Create Network Security Group and rule +resource "azurerm_network_security_group" "myterraformnsg" { + name = "myNetworkSecurityGroup" + location = azurerm_resource_group.rg.location + 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" "myterraformnic" { + name = "myNIC" + location = azurerm_resource_group.rg.location + resource_group_name = azurerm_resource_group.rg.name + + ip_configuration { + name = "myNicConfiguration" + subnet_id = azurerm_subnet.myterraformsubnet.id + private_ip_address_allocation = "Dynamic" + public_ip_address_id = azurerm_public_ip.myterraformpublicip.id + } +} + +# Connect the security group to the network interface +resource "azurerm_network_interface_security_group_association" "example" { + network_interface_id = azurerm_network_interface.myterraformnic.id + network_security_group_id = azurerm_network_security_group.myterraformnsg.id +} + +# Generate random text for a unique storage account name +resource "random_id" "randomId" { + keepers = { + # Generate a new ID only when a new resource group is defined + resource_group = azurerm_resource_group.rg.name + } + + byte_length = 8 +} + +# Create storage account for boot diagnostics +resource "azurerm_storage_account" "mystorageaccount" { + name = "diag${random_id.randomId.hex}" + location = azurerm_resource_group.rg.location + resource_group_name = azurerm_resource_group.rg.name + account_tier = "Standard" + account_replication_type = "LRS" +} + +# Create (and display) an SSH key +resource "tls_private_key" "example_ssh" { + algorithm = "RSA" + rsa_bits = 4096 +} + +# Create virtual machine +resource "azurerm_linux_virtual_machine" "myterraformvm" { + name = "myVM" + location = azurerm_resource_group.rg.location + resource_group_name = azurerm_resource_group.rg.name + network_interface_ids = [azurerm_network_interface.myterraformnic.id] + size = "Standard_DS1_v2" + + os_disk { + name = "myOsDisk" + caching = "ReadWrite" + storage_account_type = "Premium_LRS" + } + + source_image_reference { + publisher = "Canonical" + offer = "UbuntuServer" + sku = "18.04-LTS" + version = "latest" + } + + computer_name = "myvm" + admin_username = "azureuser" + disable_password_authentication = true + + admin_ssh_key { + username = "azureuser" + public_key = tls_private_key.example_ssh.public_key_openssh + } + + boot_diagnostics { + storage_account_uri = azurerm_storage_account.mystorageaccount.primary_blob_endpoint + } +} \ No newline at end of file diff --git a/quickstart/101-vm-with-infrastructure/outputs.tf b/quickstart/101-vm-with-infrastructure/outputs.tf index e69de29b..6a3a68b6 100644 --- a/quickstart/101-vm-with-infrastructure/outputs.tf +++ b/quickstart/101-vm-with-infrastructure/outputs.tf @@ -0,0 +1,12 @@ +output "resource_group_name" { + value = azurerm_resource_group.rg.name +} + +output "public_ip_address" { + value = azurerm_linux_virtual_machine.myterraformvm.public_ip_address +} + +output "tls_private_key" { + value = tls_private_key.example_ssh.private_key_pem + sensitive = true +} \ No newline at end of file diff --git a/quickstart/101-vm-with-infrastructure/providers.tf b/quickstart/101-vm-with-infrastructure/providers.tf index e69de29b..5343d826 100644 --- a/quickstart/101-vm-with-infrastructure/providers.tf +++ b/quickstart/101-vm-with-infrastructure/providers.tf @@ -0,0 +1,18 @@ +terraform { + required_version = ">=0.12" + + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = "~>2.0" + } + random = { + source = "hashicorp/random" + version = "~>3.0" + } + } +} + +provider "azurerm" { + features {} +} \ No newline at end of file diff --git a/quickstart/101-vm-with-infrastructure/variables.tf b/quickstart/101-vm-with-infrastructure/variables.tf index e69de29b..e8396125 100644 --- a/quickstart/101-vm-with-infrastructure/variables.tf +++ b/quickstart/101-vm-with-infrastructure/variables.tf @@ -0,0 +1,9 @@ +variable "resource_group_location" { + default = "eastus" + description = "Location of the resource group." +} + +variable "resource_group_name_prefix" { + default = "rg" + description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription." +} \ No newline at end of file