Skip to content

Commit

Permalink
Replaced password with SSH key
Browse files Browse the repository at this point in the history
  • Loading branch information
TomArcherMsft committed Jul 12, 2023
1 parent 7c224d7 commit 1a20e49
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 40 deletions.
25 changes: 7 additions & 18 deletions quickstart/101-vm-cluster-linux/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,6 @@ resource "random_pet" "azurerm_linux_virtual_machine_name" {
prefix = "vm"
}

resource "random_password" "password" {
count = var.password == null ? 1 : 0
length = 20
special = true
min_numeric = 1
min_upper = 1
min_lower = 1
min_special = 1
}

locals {
password = try(random_password.password[0].result, var.password)
}

resource "azurerm_linux_virtual_machine" "test" {
count = 2
name = "${random_pet.azurerm_linux_virtual_machine_name.id}${count.index}"
Expand All @@ -114,16 +100,19 @@ resource "azurerm_linux_virtual_machine" "test" {
version = "latest"
}

admin_ssh_key {
username = "adminuser"
public_key = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey
}

os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
name = "myosdisk${count.index}"
}

computer_name = "hostname"
admin_username = var.user_name
admin_password = local.password
disable_password_authentication = false
computer_name = "hostname"
admin_username = "adminuser"
}

resource "azurerm_managed_disk" "test" {
Expand Down
5 changes: 0 additions & 5 deletions quickstart/101-vm-cluster-linux/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,4 @@ output "subnet_name" {

output "linux_virtual_machine_names" {
value = [for s in azurerm_linux_virtual_machine.test : s.name[*]]
}

output "linux_virtual_machine_password" {
sensitive = true
value = local.password
}
4 changes: 4 additions & 0 deletions quickstart/101-vm-cluster-linux/providers.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
terraform {
required_version = ">=1.0"
required_providers {
azapi = {
source = "azure/azapi"
version = "~>1.5"
}
azurerm = {
source = "hashicorp/azurerm"
version = "~>3.0"
Expand Down
5 changes: 2 additions & 3 deletions quickstart/101-vm-cluster-linux/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@ This template deploys a Linux VM cluster on Azure.
- [azurerm_network_interface](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_interface)
- [azurerm_managed_disk](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/managed_disk)
- [azurerm_availability_set](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/availability_set)
- [random_password](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/password)
- [azurerm_linux_virtual_machine](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/linux_virtual_machine)
- [azapi_resource](https://registry.terraform.io/providers/Azure/azapi/latest/docs/resources/azapi_resource)
- [azapi_resource_action](https://registry.terraform.io/providers/Azure/azapi/latest/docs/resources/azapi_resource_action)

## Variables

| 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 |
| `user_name` | The username for the local account that will be created on the new VM. | |
| `password` | The password for the local account that will be created on the new VM. | |

## Example

Expand Down
25 changes: 25 additions & 0 deletions quickstart/101-vm-cluster-linux/ssh.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
resource "random_pet" "ssh_key_name" {
prefix = "ssh"
separator = ""
}

resource "azapi_resource" "ssh_public_key" {
type = "Microsoft.Compute/sshPublicKeys@2022-11-01"
name = random_pet.ssh_key_name.id
location = "westus3"
parent_id = azurerm_resource_group.rg.id
}

resource "azapi_resource_action" "ssh_public_key_gen" {
type = "Microsoft.Compute/sshPublicKeys@2022-11-01"
resource_id = azapi_resource.ssh_public_key.id
action = "generateKeyPair"
method = "POST"

response_export_values = ["publicKey"]
}

output "key_data" {
value = azapi_resource.ssh_public_key.body
sensitive = true
}
15 changes: 1 addition & 14 deletions quickstart/101-vm-cluster-linux/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,4 @@ variable "resource_group_name_prefix" {
type = string
description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription."
default = "rg"
}

variable "user_name" {
type = string
description = "The username for the local account that will be created on the new vm."
default = "azureadmin"
}

variable "password" {
type = string
description = "The password for the local account that will be created on the new vm."
sensitive = true
default = null
}
}

0 comments on commit 1a20e49

Please sign in to comment.