Skip to content

Commit e18c841

Browse files
committed
terraform/azure: create an ssh key if not provided one
closes #46
1 parent 5c9e906 commit e18c841

File tree

5 files changed

+20
-9
lines changed

5 files changed

+20
-9
lines changed

terraform/azure/azure-linux-vm/main.tf

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ locals {
3030
subnet_id = module.vpc.public_subnet_id
3131
network_security_group_id = azurerm_network_security_group.tailscale_ingress.id
3232
instance_type = "Standard_D2as_v6"
33-
admin_public_key_path = var.admin_public_key_path
33+
admin_public_key = var.admin_public_key_path == "" ? tls_private_key.ssh[0].public_key_pem : file(var.admin_public_key_path)
3434
}
3535

3636
resource "azurerm_resource_group" "main" {
@@ -53,6 +53,11 @@ module "vpc" {
5353
subnet_name_private_dns_resolver = "dns-inbound"
5454
}
5555

56+
resource "tls_private_key" "ssh" {
57+
count = var.admin_public_key_path == "" ? 1 : 0
58+
algorithm = "ED25519"
59+
}
60+
5661
#
5762
# Tailscale instance resources
5863
#
@@ -87,10 +92,10 @@ module "tailscale_azure_linux_virtual_machine" {
8792
network_security_group_id = local.network_security_group_id
8893
public_ip_address_id = azurerm_public_ip.vm.id
8994

90-
machine_name = local.name
91-
machine_size = local.instance_type
92-
admin_public_key_path = local.admin_public_key_path
93-
resource_tags = local.azure_tags
95+
machine_name = local.name
96+
machine_size = local.instance_type
97+
admin_public_key = local.admin_public_key
98+
resource_tags = local.azure_tags
9499

95100
# Variables for Tailscale resources
96101
tailscale_hostname = local.name

terraform/azure/azure-linux-vm/variables.tf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22
# Variables for Azure resources
33
#
44
variable "admin_public_key_path" {
5-
type = string
5+
type = string
6+
description = "Path to the SSH public key to assign to the virtual machine - if omitted, a key will be created"
7+
default = ""
68
}

terraform/azure/azure-linux-vm/versions.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ terraform {
44
source = "hashicorp/azurerm"
55
version = ">= 4.0, < 5.0"
66
}
7+
tls = {
8+
source = "hashicorp/tls"
9+
version = ">= 4.0, < 5.0"
10+
}
711
tailscale = {
812
source = "tailscale/tailscale"
913
version = ">= 0.24"

terraform/azure/internal-modules/azure-linux-vm/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ resource "azurerm_linux_virtual_machine" "tailscale_instance" {
4343
admin_username = var.admin_username
4444
admin_ssh_key {
4545
username = var.admin_username
46-
public_key = file(var.admin_public_key_path)
46+
public_key = var.admin_public_key
4747
}
4848

4949
os_disk {

terraform/azure/internal-modules/azure-linux-vm/variables.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ variable "admin_username" {
3838
type = string
3939
default = "ubuntu"
4040
}
41-
variable "admin_public_key_path" {
42-
description = "The filepath of the SSH public key to assign to the virtual machine"
41+
variable "admin_public_key" {
42+
description = "The SSH public key to assign to the virtual machine"
4343
type = string
4444
}
4545
variable "public_ip_address_id" {

0 commit comments

Comments
 (0)