Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User Story 125263 #238

Merged
merged 8 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions quickstart/101-vm-cluster-linux/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ resource "azurerm_linux_virtual_machine" "test" {
}

admin_ssh_key {
username = coalesce(var.username, "azureuser")
username = var.username
public_key = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey
}

Expand All @@ -112,7 +112,7 @@ resource "azurerm_linux_virtual_machine" "test" {
}

computer_name = "hostname"
admin_username = coalesce(var.username, "azureuser")
admin_username = var.username
}

resource "azurerm_managed_disk" "test" {
Expand Down
19 changes: 9 additions & 10 deletions quickstart/101-vm-cluster-linux/ssh.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@ resource "random_pet" "ssh_key_name" {
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"]
response_export_values = ["publicKey", "privateKey"]
}

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

output "key_data" {
value = azapi_resource.ssh_public_key.body
sensitive = true
value = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey
}
15 changes: 4 additions & 11 deletions quickstart/101-vm-with-infrastructure/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,6 @@ resource "azurerm_storage_account" "my_storage_account" {
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" "my_terraform_vm" {
name = "myVM"
Expand All @@ -116,13 +110,12 @@ resource "azurerm_linux_virtual_machine" "my_terraform_vm" {
version = "latest"
}

computer_name = "myvm"
admin_username = "azureuser"
disable_password_authentication = true
computer_name = "hostname"
admin_username = var.username

admin_ssh_key {
username = "azureuser"
public_key = tls_private_key.example_ssh.public_key_openssh
username = var.username
public_key = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey
}

boot_diagnostics {
Expand Down
5 changes: 0 additions & 5 deletions quickstart/101-vm-with-infrastructure/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,4 @@ output "resource_group_name" {

output "public_ip_address" {
value = azurerm_linux_virtual_machine.my_terraform_vm.public_ip_address
}

output "tls_private_key" {
value = tls_private_key.example_ssh.private_key_pem
sensitive = true
}
8 changes: 4 additions & 4 deletions quickstart/101-vm-with-infrastructure/providers.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ terraform {
required_version = ">=0.12"

required_providers {
azapi = {
source = "azure/azapi"
version = "~>1.5"
}
azurerm = {
source = "hashicorp/azurerm"
version = "~>2.0"
Expand All @@ -10,10 +14,6 @@ terraform {
source = "hashicorp/random"
version = "~>3.0"
}
tls = {
source = "hashicorp/tls"
version = "~>4.0"
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions quickstart/101-vm-with-infrastructure/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ This template deploys a Linux virtual machine (VM) with infrastructure that incl
- [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/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/tls/latest/docs/resources/private_key)
- [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 |
| `username` | The username for the local account that will be created on the new VM. | azureadmin |

## 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).
To see how to run this example, see [Quickstart: Configure a Linux virtual machine in Azure using Terraform](https://learn.microsoft.com/azure/virtual-machines/linux/quick-create-terraform).
24 changes: 24 additions & 0 deletions quickstart/101-vm-with-infrastructure/ssh.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
resource "random_pet" "ssh_key_name" {
prefix = "ssh"
separator = ""
}

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", "privateKey"]
}

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

output "key_data" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we move this output block to outputs.tf file?

And I've tried this output, it's value is empty. If we want to export the generated public key data as output, we can use the following code snippet:

output "key_data" {
  value     = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey
}

Since public key data is not a secret and meant to be shared, I just removed sensitive = true.

Copy link
Collaborator Author

@TomArcherMsft TomArcherMsft Jul 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now, the ssh.tf file is fully encapsulated, meaning that I can drop it into any configuration and it works. I want to keep this pattern instead of having functionality spread across multiple files.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now, the ssh.tf file is fully encapsulated, meaning that I can drop it into any configuration and it works. I want to keep this pattern instead of having functionality spread across multiple files.

That makes sense to me.

But I think we still need to ensure that value = azapi_resource.ssh_public_key.body could output the public key, and I still think we could remove this sensitive = true because the public key is meant to be shared.

value = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey
}
6 changes: 6 additions & 0 deletions quickstart/101-vm-with-infrastructure/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ variable "resource_group_name_prefix" {
type = string
default = "rg"
description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription."
}

variable "username" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we've set default value for this variable I think we can set nullable = false for this block too.

type = string
description = "The username for the local account that will be created on the new VM."
default = "azureadmin"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default value is different than the corresponding default value in coalesce function.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

}
2 changes: 1 addition & 1 deletion quickstart/201-k8s-cluster-with-tf-and-aks/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ resource "azurerm_kubernetes_cluster" "k8s" {
node_count = var.node_count
}
linux_profile {
admin_username = "ubuntu"
admin_username = var.username

ssh_key {
key_data = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey
Expand Down
1 change: 1 addition & 0 deletions quickstart/201-k8s-cluster-with-tf-and-aks/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ This template provisions an [AKS / Azure Kubernetes service (also known as a Man
| `resource_group_location` | Location of the resource group. | eastus |
| `node_count` | Initial number of nodes which should exist in this Node Pool. Value must be between 1 and 1000. | 3 |
| `msi_id` | The Managed Service Identity ID. Set this value if you're running this example using Managed Identity as the authentication method. | null |
| `username` | The username for the new cluster. | azureadmin |

## Example

Expand Down
19 changes: 9 additions & 10 deletions quickstart/201-k8s-cluster-with-tf-and-aks/ssh.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@ resource "random_pet" "ssh_key_name" {
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"]
response_export_values = ["publicKey", "privateKey"]
}

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

output "key_data" {
value = azapi_resource.ssh_public_key.body
sensitive = true
value = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey
}
6 changes: 6 additions & 0 deletions quickstart/201-k8s-cluster-with-tf-and-aks/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ variable "msi_id" {
type = string
description = "The Managed Service Identity ID. Set this value if you're running this example using Managed Identity as the authentication method."
default = null
}

variable "username" {
type = string
description = "The admin username for the new cluster."
default = "azureadmin"
}