From 9072d1d114846520ab674911b065f3db36937929 Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Mon, 29 Aug 2022 15:30:41 -0700 Subject: [PATCH 1/7] Moving create-linux-vm-with-infrastructure article code to engineering team sample repo --- quickstart/101-vm-with-infrastructure/main.tf | 0 quickstart/101-vm-with-infrastructure/outputs.tf | 0 quickstart/101-vm-with-infrastructure/providers.tf | 0 quickstart/101-vm-with-infrastructure/readme.md | 0 quickstart/101-vm-with-infrastructure/variables.tf | 0 5 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 quickstart/101-vm-with-infrastructure/main.tf create mode 100644 quickstart/101-vm-with-infrastructure/outputs.tf create mode 100644 quickstart/101-vm-with-infrastructure/providers.tf create mode 100644 quickstart/101-vm-with-infrastructure/readme.md create mode 100644 quickstart/101-vm-with-infrastructure/variables.tf diff --git a/quickstart/101-vm-with-infrastructure/main.tf b/quickstart/101-vm-with-infrastructure/main.tf new file mode 100644 index 00000000..e69de29b diff --git a/quickstart/101-vm-with-infrastructure/outputs.tf b/quickstart/101-vm-with-infrastructure/outputs.tf new file mode 100644 index 00000000..e69de29b diff --git a/quickstart/101-vm-with-infrastructure/providers.tf b/quickstart/101-vm-with-infrastructure/providers.tf new file mode 100644 index 00000000..e69de29b diff --git a/quickstart/101-vm-with-infrastructure/readme.md b/quickstart/101-vm-with-infrastructure/readme.md new file mode 100644 index 00000000..e69de29b diff --git a/quickstart/101-vm-with-infrastructure/variables.tf b/quickstart/101-vm-with-infrastructure/variables.tf new file mode 100644 index 00000000..e69de29b From 62b49e2a914de45a8d48af41ac0cd056cf5ce01a Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Mon, 29 Aug 2022 15:43:40 -0700 Subject: [PATCH 2/7] Added code --- quickstart/101-vm-with-infrastructure/main.tf | 131 ++++++++++++++++++ .../101-vm-with-infrastructure/outputs.tf | 12 ++ .../101-vm-with-infrastructure/providers.tf | 18 +++ .../101-vm-with-infrastructure/variables.tf | 9 ++ 4 files changed, 170 insertions(+) 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 From 0a1a441dc9a34bc2ae01b13393823191078feb9a Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Mon, 29 Aug 2022 16:24:11 -0700 Subject: [PATCH 3/7] Updated readme with terraform types being utilized --- .../101-vm-with-infrastructure/readme.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/quickstart/101-vm-with-infrastructure/readme.md b/quickstart/101-vm-with-infrastructure/readme.md index e69de29b..2f320394 100644 --- a/quickstart/101-vm-with-infrastructure/readme.md +++ b/quickstart/101-vm-with-infrastructure/readme.md @@ -0,0 +1,30 @@ +# Azure resource group + +This template deploys a Linux virtual machine (VM) with infrastructure that includes a virtual network, subnet, public IP address, and more. + +## Terraform resource types + +- [random_pet](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet) +- [azurerm_resource_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group) + +- [azurerm_virtual_network](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_network) +- [azurerm_subnet](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/subnet) +- [azurerm_public_ip](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/public_ip) +- [azurerm_network_security_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_security_group) +- [azurerm_network_interface](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_interface) +- [azurerm_network_interface_security_group_association](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_interface_security_group_association) +- [random_id](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/id) +- [azurerm_storage_account](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/storage_account) +- [tls_private_key](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/private_key) +- [azurerm_linux_virtual_machine](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/linux_virtual_machine) + +## Variables + +| Name | Description | +|-|-| +| `resource_group_name_prefix` | (Optional) Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription. Value defaults to: rg| +| `resource_group_location` | (Optional) Azure Region in which to deploy these resources. Value defaults to: eastus | + +## Example + +To see how to run this example, see [Quickstart: Configure a Linux virtual machine in Azure using Terraform](https://docs.microsoft.com/azure/developer/terraform/create-linux-virtual-machine-with-infrastructure). \ No newline at end of file From 7cf420ddb59be7e390b631103f541ed7d27ee1e6 Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Mon, 29 Aug 2022 16:41:46 -0700 Subject: [PATCH 4/7] Updated readme for several samples --- quickstart/101-attestation-provider/readme.md | 4 ++-- quickstart/101-resource-group/readme.md | 8 ++++---- quickstart/101-vm-with-infrastructure/readme.md | 10 +++++----- quickstart/201-mysql-fs-db/readme.md | 4 ++-- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/quickstart/101-attestation-provider/readme.md b/quickstart/101-attestation-provider/readme.md index f42ce260..41ff569a 100644 --- a/quickstart/101-attestation-provider/readme.md +++ b/quickstart/101-attestation-provider/readme.md @@ -12,8 +12,8 @@ This template deploys an [Attestation provider](/azure/attestation/overview) on | Name | Description | Default | |-|-|-| -| `resource_group_name_prefix` | (Optional) Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription.| rg | -| `resource_group_location` | (Optional) Azure Region in which to deploy these resources.| eastus | +| `resource_group_name_prefix` | Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription. | rg | +| `resource_group_location` | Location of the resource group. | eastus | | `attestation_provider_name` | Name of the Attestation provider | attestationprovider007 | ## Example diff --git a/quickstart/101-resource-group/readme.md b/quickstart/101-resource-group/readme.md index 84273e40..377234a3 100644 --- a/quickstart/101-resource-group/readme.md +++ b/quickstart/101-resource-group/readme.md @@ -9,10 +9,10 @@ This template deploys an Azure resource group with a random name beginning with ## Variables -| Name | Description | -|-|-| -| `resource_group_name_prefix` | (Optional) Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription. Value defaults to: rg| -| `resource_group_location` | (Optional) Azure Region in which to deploy these resources. Value defaults to: eastus | +| Name | Description | Default | +|-|-|-| +| `resource_group_name_prefix` | Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription. | rg | +| `resource_group_location` | Location of the resource group. | eastus | ## Example diff --git a/quickstart/101-vm-with-infrastructure/readme.md b/quickstart/101-vm-with-infrastructure/readme.md index 2f320394..02772c5e 100644 --- a/quickstart/101-vm-with-infrastructure/readme.md +++ b/quickstart/101-vm-with-infrastructure/readme.md @@ -13,17 +13,17 @@ This template deploys a Linux virtual machine (VM) with infrastructure that incl - [azurerm_network_security_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_security_group) - [azurerm_network_interface](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_interface) - [azurerm_network_interface_security_group_association](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_interface_security_group_association) -- [random_id](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/id) +- [random_id](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/id) - [azurerm_storage_account](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/storage_account) -- [tls_private_key](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/private_key) +- [tls_private_key](https://registry.terraform.io/providers/hashicorp/tls/latest/docs/resources/private_key) - [azurerm_linux_virtual_machine](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/linux_virtual_machine) ## Variables -| Name | Description | +| Name | Description | Default | |-|-| -| `resource_group_name_prefix` | (Optional) Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription. Value defaults to: rg| -| `resource_group_location` | (Optional) Azure Region in which to deploy these resources. Value defaults to: eastus | +| `resource_group_name_prefix` | Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription. | rg | +| `resource_group_location` | Location of the resource group. | eastus | ## Example diff --git a/quickstart/201-mysql-fs-db/readme.md b/quickstart/201-mysql-fs-db/readme.md index bac940f1..286650fc 100644 --- a/quickstart/201-mysql-fs-db/readme.md +++ b/quickstart/201-mysql-fs-db/readme.md @@ -19,8 +19,8 @@ This template deploys an [Azure MySQL Flexible Server Database](https://registry | Name | Description | Default | |-|-|-| -| `resource_group_name_prefix` | (Optional) Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription. Value defaults to: rg| -| `resource_group_location` | (Optional) Azure Region in which to deploy these resources. Value defaults to: eastus | +| `resource_group_name_prefix` | Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription. | rg | +| `resource_group_location` | Location of the resource group. | eastus | ## Example From 85f9e9a803da2e7bac4e97ce637fe1f6abb1b1ac Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Mon, 29 Aug 2022 17:45:43 -0700 Subject: [PATCH 5/7] Added tls provider block --- quickstart/101-vm-with-infrastructure/providers.tf | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/quickstart/101-vm-with-infrastructure/providers.tf b/quickstart/101-vm-with-infrastructure/providers.tf index 5343d826..098cece6 100644 --- a/quickstart/101-vm-with-infrastructure/providers.tf +++ b/quickstart/101-vm-with-infrastructure/providers.tf @@ -10,6 +10,10 @@ terraform { source = "hashicorp/random" version = "~>3.0" } + tls = { + source = "hashicorp/tls" + version = "4.0.1" + } } } From 71e54587ebe700a2cb9b7748cde5a8ce84d29a93 Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Mon, 29 Aug 2022 18:16:52 -0700 Subject: [PATCH 6/7] Changed resource names to snake case --- quickstart/101-vm-with-infrastructure/main.tf | 34 +++++++++---------- .../101-vm-with-infrastructure/outputs.tf | 2 +- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/quickstart/101-vm-with-infrastructure/main.tf b/quickstart/101-vm-with-infrastructure/main.tf index f323361c..978e5f06 100644 --- a/quickstart/101-vm-with-infrastructure/main.tf +++ b/quickstart/101-vm-with-infrastructure/main.tf @@ -8,7 +8,7 @@ resource "azurerm_resource_group" "rg" { } # Create virtual network -resource "azurerm_virtual_network" "myterraformnetwork" { +resource "azurerm_virtual_network" "my_terraform_network" { name = "myVnet" address_space = ["10.0.0.0/16"] location = azurerm_resource_group.rg.location @@ -16,15 +16,15 @@ resource "azurerm_virtual_network" "myterraformnetwork" { } # Create subnet -resource "azurerm_subnet" "myterraformsubnet" { +resource "azurerm_subnet" "my_terraform_subnet" { name = "mySubnet" resource_group_name = azurerm_resource_group.rg.name - virtual_network_name = azurerm_virtual_network.myterraformnetwork.name + virtual_network_name = azurerm_virtual_network.my_terraform_network.name address_prefixes = ["10.0.1.0/24"] } # Create public IPs -resource "azurerm_public_ip" "myterraformpublicip" { +resource "azurerm_public_ip" "my_terraform_public_ip" { name = "myPublicIP" location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name @@ -32,7 +32,7 @@ resource "azurerm_public_ip" "myterraformpublicip" { } # Create Network Security Group and rule -resource "azurerm_network_security_group" "myterraformnsg" { +resource "azurerm_network_security_group" "my_terraform_nsg" { name = "myNetworkSecurityGroup" location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name @@ -51,27 +51,27 @@ resource "azurerm_network_security_group" "myterraformnsg" { } # Create network interface -resource "azurerm_network_interface" "myterraformnic" { +resource "azurerm_network_interface" "my_terraform_nic" { 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 + name = "my_nic_configuration" + subnet_id = azurerm_subnet.my_terraform_subnet.id private_ip_address_allocation = "Dynamic" - public_ip_address_id = azurerm_public_ip.myterraformpublicip.id + public_ip_address_id = azurerm_public_ip.my_terraform_public_ip.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 + network_interface_id = azurerm_network_interface.my_terraform_nic.id + network_security_group_id = azurerm_network_security_group.my_terraform_nsg.id } # Generate random text for a unique storage account name -resource "random_id" "randomId" { +resource "random_id" "random_id" { keepers = { # Generate a new ID only when a new resource group is defined resource_group = azurerm_resource_group.rg.name @@ -81,8 +81,8 @@ resource "random_id" "randomId" { } # Create storage account for boot diagnostics -resource "azurerm_storage_account" "mystorageaccount" { - name = "diag${random_id.randomId.hex}" +resource "azurerm_storage_account" "my_storage_account" { + name = "diag${random_id.random_id.hex}" location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name account_tier = "Standard" @@ -96,11 +96,11 @@ resource "tls_private_key" "example_ssh" { } # Create virtual machine -resource "azurerm_linux_virtual_machine" "myterraformvm" { +resource "azurerm_linux_virtual_machine" "my_terraform_vm" { name = "myVM" location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name - network_interface_ids = [azurerm_network_interface.myterraformnic.id] + network_interface_ids = [azurerm_network_interface.my_terraform_nic.id] size = "Standard_DS1_v2" os_disk { @@ -126,6 +126,6 @@ resource "azurerm_linux_virtual_machine" "myterraformvm" { } boot_diagnostics { - storage_account_uri = azurerm_storage_account.mystorageaccount.primary_blob_endpoint + storage_account_uri = azurerm_storage_account.my_storage_account.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 6a3a68b6..545f6482 100644 --- a/quickstart/101-vm-with-infrastructure/outputs.tf +++ b/quickstart/101-vm-with-infrastructure/outputs.tf @@ -3,7 +3,7 @@ output "resource_group_name" { } output "public_ip_address" { - value = azurerm_linux_virtual_machine.myterraformvm.public_ip_address + value = azurerm_linux_virtual_machine.my_terraform_vm.public_ip_address } output "tls_private_key" { From 03f800dbe4776ea3dfd8bac76e0a2f9fdc888a87 Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Mon, 29 Aug 2022 18:19:05 -0700 Subject: [PATCH 7/7] Relaxing version constraint on tls provider --- quickstart/101-vm-with-infrastructure/providers.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quickstart/101-vm-with-infrastructure/providers.tf b/quickstart/101-vm-with-infrastructure/providers.tf index 098cece6..0234a678 100644 --- a/quickstart/101-vm-with-infrastructure/providers.tf +++ b/quickstart/101-vm-with-infrastructure/providers.tf @@ -12,7 +12,7 @@ terraform { } tls = { source = "hashicorp/tls" - version = "4.0.1" + version = "~>4.0" } } }