diff --git a/quickstart/101-aks-cluster-windows/README.md b/quickstart/101-aks-cluster-windows/README.md new file mode 100644 index 00000000..071782ad --- /dev/null +++ b/quickstart/101-aks-cluster-windows/README.md @@ -0,0 +1,25 @@ +# Windows-based Azure Kubernetes Service (AKS) cluster + +This template deploys an AKS cluster with Windows nodes. + +## 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) +- [random_string](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string) +- [azurerm_virtual_network](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_network) +- [azurerm_kubernetes_cluster](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/kubernetes_cluster) +- [azurerm_kubernetes_cluster_node_pool](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/kubernetes_cluster_node_pool) + +## 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 | +| `node_count_linux` | Initial quantity of Linux nodes for the node pool. | 1 | +| `node_count_windows` | Initial quantity of Windows nodes for the node pool. | 1 | +| `admin_username` | Admin username for the Windows node pool. | azureuser | +| `admin_password` | Admin password for the Windows node pool. | Passw0rd1234Us! | + +## Example diff --git a/quickstart/101-aks-cluster-windows/main.tf b/quickstart/101-aks-cluster-windows/main.tf new file mode 100644 index 00000000..3795214c --- /dev/null +++ b/quickstart/101-aks-cluster-windows/main.tf @@ -0,0 +1,73 @@ +# Generate random resource group name +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 +} + +resource "random_pet" "azurerm_kubernetes_cluster_name" { + prefix = "cluster" +} + +resource "random_pet" "azurerm_kubernetes_cluster_dns_prefix" { + prefix = "dns" +} + +resource "random_string" "azurerm_kubernetes_cluster_node_pool" { + length = 6 + special = false + numeric = false + lower = true + upper = false +} + +resource "azurerm_virtual_network" "vnet" { + name = "myvnet" + location = azurerm_resource_group.rg.location + resource_group_name = azurerm_resource_group.rg.name + address_space = ["10.1.0.0/16"] + + subnet { + name = "subnet1" + address_prefix = "10.1.1.0/24" + } +} + +resource "azurerm_kubernetes_cluster" "aks" { + name = random_pet.azurerm_kubernetes_cluster_name.id + location = azurerm_resource_group.rg.location + resource_group_name = azurerm_resource_group.rg.name + dns_prefix = random_pet.azurerm_kubernetes_cluster_dns_prefix.id + + identity { + type = "SystemAssigned" + } + + default_node_pool { + name = "agentpool" + vm_size = "Standard_D2_v2" + node_count = var.node_count_linux + vnet_subnet_id = element(tolist(azurerm_virtual_network.vnet.subnet), 0).id + } + + windows_profile { + admin_username = var.admin_username + admin_password = var.admin_password + } + + network_profile { + network_plugin = "azure" + load_balancer_sku = "standard" + } +} + +resource "azurerm_kubernetes_cluster_node_pool" "win" { + name = random_string.azurerm_kubernetes_cluster_node_pool.result + kubernetes_cluster_id = azurerm_kubernetes_cluster.aks.id + vm_size = "Standard_D4s_v3" + node_count = var.node_count_windows + os_type = "Windows" +} diff --git a/quickstart/101-aks-cluster-windows/outputs.tf b/quickstart/101-aks-cluster-windows/outputs.tf new file mode 100644 index 00000000..7067a430 --- /dev/null +++ b/quickstart/101-aks-cluster-windows/outputs.tf @@ -0,0 +1,20 @@ +output "resource_group_name" { + value = azurerm_resource_group.rg.name +} + +output "kubernetes_cluster_name" { + value = azurerm_kubernetes_cluster.aks.name +} + +output "kubernetes_cluster_dns_prefix" { + value = azurerm_kubernetes_cluster.aks.dns_prefix +} + +output "kubernetes_cluster_node_pool_name" { + value = azurerm_kubernetes_cluster_node_pool.win.name +} + +output "kubernetes_cluster_kube_config_raw" { + value = azurerm_kubernetes_cluster.aks.kube_config_raw + sensitive = true +} \ No newline at end of file diff --git a/quickstart/101-aks-cluster-windows/providers.tf b/quickstart/101-aks-cluster-windows/providers.tf new file mode 100644 index 00000000..a0155e16 --- /dev/null +++ b/quickstart/101-aks-cluster-windows/providers.tf @@ -0,0 +1,19 @@ +terraform { + required_version = ">= 1.0" + + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = "~>3.0" + } + random = { + source = "hashicorp/random" + version = "~>3.0" + } + } +} + +provider "azurerm" { + features { + } +} diff --git a/quickstart/101-aks-cluster-windows/variables.tf b/quickstart/101-aks-cluster-windows/variables.tf new file mode 100644 index 00000000..2cf992b9 --- /dev/null +++ b/quickstart/101-aks-cluster-windows/variables.tf @@ -0,0 +1,35 @@ +variable "resource_group_location" { + type = string + default = "eastus" + description = "Location of the resource group." +} + +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 "node_count_linux" { + type = number + description = "The initial quantity of Linux nodes for the node pool." + default = 1 +} + +variable "node_count_windows" { + type = number + description = "The initial quantity of Windows nodes for the node pool." + default = 1 +} + +variable "admin_username" { + type = string + description = "The admin username for the Windows node pool." + default = "azureuser" +} + +variable "admin_password" { + type = string + description = "The admin password for the Windows node pool." + default = "Passw0rd1234Us!" +} \ No newline at end of file diff --git a/quickstart/101-app-service-backup/README.md b/quickstart/101-app-service-backup/README.md new file mode 100644 index 00000000..7be08270 --- /dev/null +++ b/quickstart/101-app-service-backup/README.md @@ -0,0 +1,22 @@ +# Azure Windows Web App with Backup + +This template deploys an Azure Windows Web App with a backup configured. + +## 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) +- [random_string](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string) +- [azurerm_storage_account](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/storage_account) +- [azurerm_storage_container](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/storage_container) +- [azurerm_service_plan](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/service_plan) +- [azurerm_windows_web_app](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/windows_web_app) + +## Variables + +| Name | Description | Default value | +|-|-|-| +| `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 \ No newline at end of file diff --git a/quickstart/101-app-service-backup/main.tf b/quickstart/101-app-service-backup/main.tf new file mode 100644 index 00000000..874e50af --- /dev/null +++ b/quickstart/101-app-service-backup/main.tf @@ -0,0 +1,121 @@ +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 +} + +resource "random_string" "storage_account_name" { + length = 8 + lower = true + numeric = false + special = false + upper = false +} + +resource "azurerm_storage_account" "example" { + name = random_string.storage_account_name.result + resource_group_name = azurerm_resource_group.rg.name + location = azurerm_resource_group.rg.location + account_tier = "Standard" + account_replication_type = "LRS" +} + +resource "random_string" "storage_container_name" { + length = 8 + lower = true + numeric = false + special = false + upper = false +} + +resource "azurerm_storage_container" "example" { + name = random_string.storage_container_name.result + storage_account_name = azurerm_storage_account.example.name + container_access_type = "private" +} + +resource "random_string" "service_plan_name" { + length = 8 + lower = true + numeric = false + special = false + upper = false +} + +resource "azurerm_service_plan" "example" { + name = random_string.service_plan_name.result + location = azurerm_resource_group.rg.location + resource_group_name = azurerm_resource_group.rg.name + os_type = "Windows" + sku_name = "S1" +} + +data "azurerm_storage_account_sas" "example" { + connection_string = azurerm_storage_account.example.primary_connection_string + https_only = true + + resource_types { + service = false + container = false + object = true + } + + services { + blob = true + queue = false + table = false + file = false + } + + # Please change the start_date variable (in variables.tf) to the appropriate + # value for your environment. + start = formatdate(var.start_date, timestamp()) + expiry = formatdate(var.start_date, timeadd(timestamp(), "8765h")) + + permissions { + read = false + write = true + delete = false + list = false + add = false + create = false + update = false + process = false + tag = false + filter = false + } +} + +resource "random_string" "windows_web_app_name" { + length = 8 + lower = true + numeric = false + special = false + upper = false +} + +resource "azurerm_windows_web_app" "example" { + name = random_string.windows_web_app_name.result + location = azurerm_resource_group.rg.location + resource_group_name = azurerm_resource_group.rg.name + service_plan_id = azurerm_service_plan.example.id + + backup { + name = "Example" + storage_account_url = "https://${azurerm_storage_account.example.name}.blob.core.windows.net/${azurerm_storage_container.example.name}${data.azurerm_storage_account_sas.example.sas}&sr=b" + schedule { + frequency_interval = 30 + frequency_unit = "Day" + } + } + + site_config { + application_stack { + dotnet_version = "v6.0" + current_stack = "dotnet" + } + } +} \ No newline at end of file diff --git a/quickstart/101-app-service-backup/outputs.tf b/quickstart/101-app-service-backup/outputs.tf new file mode 100644 index 00000000..e4cbe491 --- /dev/null +++ b/quickstart/101-app-service-backup/outputs.tf @@ -0,0 +1,23 @@ +output "resource_group_name" { + value = azurerm_resource_group.rg.name +} + +output "storage_account_name" { + value = azurerm_storage_account.example.name +} + +output "storage_container_name" { + value = azurerm_storage_container.example.name +} + +output "service_plan_name" { + value = azurerm_service_plan.example.name +} + +output "windows_web_app_name" { + value = azurerm_windows_web_app.example.name +} + +output "windows_web_app_default_hostname" { + value = azurerm_windows_web_app.example.default_hostname +} diff --git a/quickstart/101-app-service-backup/providers.tf b/quickstart/101-app-service-backup/providers.tf new file mode 100644 index 00000000..058b6871 --- /dev/null +++ b/quickstart/101-app-service-backup/providers.tf @@ -0,0 +1,18 @@ +terraform { + required_version = ">=1.0" + + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = "~>3.0" + } + random = { + source = "hashicorp/random" + version = "~>3.0" + } + } +} + +provider "azurerm" { + features {} +} \ No newline at end of file diff --git a/quickstart/101-app-service-backup/variables.tf b/quickstart/101-app-service-backup/variables.tf new file mode 100644 index 00000000..5320e399 --- /dev/null +++ b/quickstart/101-app-service-backup/variables.tf @@ -0,0 +1,18 @@ +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 "resource_group_location" { + type = string + default = "eastus" + description = "Location of the resource group." +} + + +variable "start_date" { + type = string + default = "2024-06-01" + description = "Start date." +} \ No newline at end of file diff --git a/quickstart/101-authn-managed-identity/main.tf b/quickstart/101-authn-managed-identity/main.tf new file mode 100644 index 00000000..aef2cdee --- /dev/null +++ b/quickstart/101-authn-managed-identity/main.tf @@ -0,0 +1,80 @@ +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 +} + +data "azurerm_subscription" "current" {} + +resource "azurerm_virtual_network" "example" { + name = "myVnet" + address_space = ["10.0.0.0/16"] + location = azurerm_resource_group.rg.location + resource_group_name = azurerm_resource_group.rg.name +} + +resource "azurerm_subnet" "example" { + name = "mySubnet" + resource_group_name = azurerm_resource_group.rg.name + virtual_network_name = azurerm_virtual_network.example.name + address_prefixes = ["10.0.2.0/24"] +} + +resource "azurerm_network_interface" "example" { + name = "myNic" + location = azurerm_resource_group.rg.location + resource_group_name = azurerm_resource_group.rg.name + + ip_configuration { + name = "internal" + subnet_id = azurerm_subnet.example.id + private_ip_address_allocation = "Dynamic" + } +} + +resource "azurerm_linux_virtual_machine" "example" { + name = "myVm" + resource_group_name = azurerm_resource_group.rg.name + location = azurerm_resource_group.rg.location + size = "Standard_F2" + network_interface_ids = [ + azurerm_network_interface.example.id, + ] + + computer_name = "hostname" + admin_username = var.username + + admin_ssh_key { + username = var.username + public_key = azapi_resource_action.ssh_public_key_gen.output.publicKey + } + + identity { + type = "SystemAssigned" + } + + os_disk { + caching = "ReadWrite" + storage_account_type = "Standard_LRS" + } + + source_image_reference { + publisher = "Canonical" + offer = "0001-com-ubuntu-server-jammy" + sku = "22_04-lts" + version = "latest" + } +} + +data "azurerm_role_definition" "contributor" { + name = "Contributor" +} + +resource "azurerm_role_assignment" "example" { + scope = data.azurerm_subscription.current.id + role_definition_name = "Contributor" + principal_id = azurerm_linux_virtual_machine.example.identity[0].principal_id +} diff --git a/quickstart/101-authn-managed-identity/outputs.tf b/quickstart/101-authn-managed-identity/outputs.tf new file mode 100644 index 00000000..a008ee50 --- /dev/null +++ b/quickstart/101-authn-managed-identity/outputs.tf @@ -0,0 +1,7 @@ +output "resource_group_name" { + value = azurerm_resource_group.rg.name +} + +output "azurerm_linux_virtual_machine_name" { + value = azurerm_linux_virtual_machine.example.name +} diff --git a/quickstart/101-authn-managed-identity/providers.tf b/quickstart/101-authn-managed-identity/providers.tf new file mode 100644 index 00000000..93ab8819 --- /dev/null +++ b/quickstart/101-authn-managed-identity/providers.tf @@ -0,0 +1,22 @@ +terraform { + required_version = ">=0.12" + + required_providers { + azapi = { + source = "azure/azapi" + version = "~>1.5" + } + 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-authn-managed-identity/readme.md b/quickstart/101-authn-managed-identity/readme.md new file mode 100644 index 00000000..e5c22bbe --- /dev/null +++ b/quickstart/101-authn-managed-identity/readme.md @@ -0,0 +1,25 @@ +# Authentication using managed identities for Azure services + +This template deploys a Linux virtual machine (VM) to show an example of how to use managed identities for Azure services. + +## 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_network_interface](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_interface) +- [azurerm_linux_virtual_machine](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/linux_virtual_machine) +- [azurerm_role_assignment](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/role_assignment) + +## 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 [Authenticate Terraform using Managed Identity for Azure services](https://docs.microsoft.com/azure/developer/terraform/authenticate-to-azure-using-msi). diff --git a/quickstart/101-authn-managed-identity/ssh.tf b/quickstart/101-authn-managed-identity/ssh.tf new file mode 100644 index 00000000..7dbe345a --- /dev/null +++ b/quickstart/101-authn-managed-identity/ssh.tf @@ -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" { + value = azapi_resource_action.ssh_public_key_gen.output.publicKey +} \ No newline at end of file diff --git a/quickstart/101-authn-managed-identity/variables.tf b/quickstart/101-authn-managed-identity/variables.tf new file mode 100644 index 00000000..27bfc0a7 --- /dev/null +++ b/quickstart/101-authn-managed-identity/variables.tf @@ -0,0 +1,17 @@ +variable "resource_group_location" { + type = string + description = "Location of the resource group." + default = "eastus" +} + +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 "username" { + type = string + description = "The username for the local account that will be created on the new VM." + default = "azureadmin" +} diff --git a/quickstart/101-firewall-standard/main.tf b/quickstart/101-firewall-standard/main.tf index 2ab26001..542ec575 100644 --- a/quickstart/101-firewall-standard/main.tf +++ b/quickstart/101-firewall-standard/main.tf @@ -1,3 +1,7 @@ +resource "random_pet" "prefix" { + prefix = var.prefix + length = 1 +} resource "azurerm_resource_group" "rg" { name = "${random_pet.prefix.id}-rg" location = var.resource_group_location @@ -93,8 +97,3 @@ resource "azurerm_firewall_network_rule_collection" "net-rc" { ] } } - -resource "random_pet" "prefix" { - prefix = var.prefix - length = 1 -} \ No newline at end of file diff --git a/quickstart/101-firewall-standard/outputs.tf b/quickstart/101-firewall-standard/outputs.tf index c765da63..ca1afd65 100644 --- a/quickstart/101-firewall-standard/outputs.tf +++ b/quickstart/101-firewall-standard/outputs.tf @@ -1,3 +1,27 @@ output "resource_group_name" { value = azurerm_resource_group.rg.name -} \ No newline at end of file +} + +output "virtual_network_name" { + value = azurerm_virtual_network.vnet.name +} + +output "subnet_name" { + value = azurerm_subnet.subnet.name +} + +output "public_ip" { + value = azurerm_public_ip.pip.ip_address +} + +output "firewall_name" { + value = azurerm_firewall.main.name +} + +output "firewall_application_rule_collection_name" { + value = azurerm_firewall_application_rule_collection.app-rc.name +} + +output "firewall_network_rule_collection" { + value = azurerm_firewall_network_rule_collection.net-rc.name +} diff --git a/quickstart/101-nat-gateway-create/main.tf b/quickstart/101-nat-gateway-create/main.tf index fef31a83..440c9e32 100644 --- a/quickstart/101-nat-gateway-create/main.tf +++ b/quickstart/101-nat-gateway-create/main.tf @@ -141,7 +141,7 @@ resource "azurerm_linux_virtual_machine" "my_terraform_vm" { admin_ssh_key { username = var.username - public_key = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey + public_key = azapi_resource_action.ssh_public_key_gen.output.publicKey } boot_diagnostics { diff --git a/quickstart/101-nat-gateway-create/ssh.tf b/quickstart/101-nat-gateway-create/ssh.tf index b7a8a2e5..7dbe345a 100644 --- a/quickstart/101-nat-gateway-create/ssh.tf +++ b/quickstart/101-nat-gateway-create/ssh.tf @@ -20,5 +20,5 @@ resource "azapi_resource" "ssh_public_key" { } output "key_data" { - value = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey + value = azapi_resource_action.ssh_public_key_gen.output.publicKey } \ No newline at end of file diff --git a/quickstart/101-vm-cluster-linux/main.tf b/quickstart/101-vm-cluster-linux/main.tf index 06200241..d1a7cc08 100644 --- a/quickstart/101-vm-cluster-linux/main.tf +++ b/quickstart/101-vm-cluster-linux/main.tf @@ -102,7 +102,7 @@ resource "azurerm_linux_virtual_machine" "test" { admin_ssh_key { username = var.username - public_key = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey + public_key = azapi_resource_action.ssh_public_key_gen.output.publicKey } os_disk { diff --git a/quickstart/101-vm-cluster-linux/ssh.tf b/quickstart/101-vm-cluster-linux/ssh.tf index b7a8a2e5..7dbe345a 100644 --- a/quickstart/101-vm-cluster-linux/ssh.tf +++ b/quickstart/101-vm-cluster-linux/ssh.tf @@ -20,5 +20,5 @@ resource "azapi_resource" "ssh_public_key" { } output "key_data" { - value = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey + value = azapi_resource_action.ssh_public_key_gen.output.publicKey } \ No newline at end of file diff --git a/quickstart/101-vm-with-infrastructure/main.tf b/quickstart/101-vm-with-infrastructure/main.tf index e8c27392..9482a95f 100644 --- a/quickstart/101-vm-with-infrastructure/main.tf +++ b/quickstart/101-vm-with-infrastructure/main.tf @@ -115,7 +115,7 @@ resource "azurerm_linux_virtual_machine" "my_terraform_vm" { admin_ssh_key { username = var.username - public_key = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey + public_key = azapi_resource_action.ssh_public_key_gen.output.publicKey } boot_diagnostics { diff --git a/quickstart/101-vm-with-infrastructure/ssh.tf b/quickstart/101-vm-with-infrastructure/ssh.tf index b7a8a2e5..7dbe345a 100644 --- a/quickstart/101-vm-with-infrastructure/ssh.tf +++ b/quickstart/101-vm-with-infrastructure/ssh.tf @@ -20,5 +20,5 @@ resource "azapi_resource" "ssh_public_key" { } output "key_data" { - value = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey + value = azapi_resource_action.ssh_public_key_gen.output.publicKey } \ No newline at end of file diff --git a/quickstart/201-azfw-with-ipgroups/main.tf b/quickstart/201-azfw-with-ipgroups/main.tf index 68d506a0..79da8555 100644 --- a/quickstart/201-azfw-with-ipgroups/main.tf +++ b/quickstart/201-azfw-with-ipgroups/main.tf @@ -210,7 +210,7 @@ resource "azurerm_linux_virtual_machine" "vm_server" { admin_username = var.admin_username admin_ssh_key { username = var.admin_username - public_key = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey + public_key = azapi_resource_action.ssh_public_key_gen.output.publicKey } network_interface_ids = [azurerm_network_interface.vm_server_nic.id] os_disk { @@ -241,7 +241,7 @@ resource "azurerm_linux_virtual_machine" "vm_jump" { } admin_ssh_key { username = var.admin_username - public_key = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey + public_key = azapi_resource_action.ssh_public_key_gen.output.publicKey } source_image_reference { publisher = "Canonical" diff --git a/quickstart/201-azfw-with-ipgroups/ssh.tf b/quickstart/201-azfw-with-ipgroups/ssh.tf index fcdb482b..3ff75ab4 100644 --- a/quickstart/201-azfw-with-ipgroups/ssh.tf +++ b/quickstart/201-azfw-with-ipgroups/ssh.tf @@ -20,6 +20,6 @@ resource "azapi_resource" "ssh_public_key" { } output "key_data" { - value = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey + value = azapi_resource_action.ssh_public_key_gen.output.publicKey } diff --git a/quickstart/201-k8s-cluster-with-tf-and-aks/main.tf b/quickstart/201-k8s-cluster-with-tf-and-aks/main.tf index 014d7b12..4c06f732 100644 --- a/quickstart/201-k8s-cluster-with-tf-and-aks/main.tf +++ b/quickstart/201-k8s-cluster-with-tf-and-aks/main.tf @@ -35,7 +35,7 @@ resource "azurerm_kubernetes_cluster" "k8s" { admin_username = var.username ssh_key { - key_data = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey + key_data = azapi_resource_action.ssh_public_key_gen.output.publicKey } } network_profile { diff --git a/quickstart/201-k8s-cluster-with-tf-and-aks/ssh.tf b/quickstart/201-k8s-cluster-with-tf-and-aks/ssh.tf index b7a8a2e5..7dbe345a 100644 --- a/quickstart/201-k8s-cluster-with-tf-and-aks/ssh.tf +++ b/quickstart/201-k8s-cluster-with-tf-and-aks/ssh.tf @@ -20,5 +20,5 @@ resource "azapi_resource" "ssh_public_key" { } output "key_data" { - value = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey + value = azapi_resource_action.ssh_public_key_gen.output.publicKey } \ No newline at end of file diff --git a/quickstart/201-private-link-sql-database/main.tf b/quickstart/201-private-link-sql-database/main.tf index eb36986c..7e4ff3d9 100644 --- a/quickstart/201-private-link-sql-database/main.tf +++ b/quickstart/201-private-link-sql-database/main.tf @@ -146,7 +146,7 @@ resource "azurerm_linux_virtual_machine" "my_terraform_vm" { admin_ssh_key { username = var.username - public_key = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey + public_key = azapi_resource_action.ssh_public_key_gen.output.publicKey } boot_diagnostics { diff --git a/quickstart/201-private-link-sql-database/ssh.tf b/quickstart/201-private-link-sql-database/ssh.tf index b7a8a2e5..7dbe345a 100644 --- a/quickstart/201-private-link-sql-database/ssh.tf +++ b/quickstart/201-private-link-sql-database/ssh.tf @@ -20,5 +20,5 @@ resource "azapi_resource" "ssh_public_key" { } output "key_data" { - value = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey + value = azapi_resource_action.ssh_public_key_gen.output.publicKey } \ No newline at end of file diff --git a/quickstart/201-vmss-packer-jumpbox/main.tf b/quickstart/201-vmss-packer-jumpbox/main.tf index e5e4dc4f..8525398a 100644 --- a/quickstart/201-vmss-packer-jumpbox/main.tf +++ b/quickstart/201-vmss-packer-jumpbox/main.tf @@ -181,7 +181,7 @@ resource "azurerm_virtual_machine_scale_set" "vmss" { ssh_keys { path = "/home/azureuser/.ssh/authorized_keys" - key_data = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey + key_data = azapi_resource_action.ssh_public_key_gen.output.publicKey } } @@ -256,7 +256,7 @@ resource "azurerm_virtual_machine" "jumpbox" { ssh_keys { path = "/home/azureuser/.ssh/authorized_keys" - key_data = jsondecode(azapi_resource_action.ssh_public_key_gen.output).publicKey + key_data = azapi_resource_action.ssh_public_key_gen.output.publicKey } } diff --git a/test/e2e/quickstart_test.go b/test/e2e/quickstart_test.go index 54ad1237..a6459f7c 100644 --- a/test/e2e/quickstart_test.go +++ b/test/e2e/quickstart_test.go @@ -9,10 +9,13 @@ import ( "testing" helper "github.com/Azure/terraform-module-test-helper" + "github.com/agiledragon/gomonkey/v2" "github.com/gruntwork-io/terratest/modules/files" "github.com/gruntwork-io/terratest/modules/packer" + "github.com/gruntwork-io/terratest/modules/shell" "github.com/gruntwork-io/terratest/modules/terraform" test_structure "github.com/gruntwork-io/terratest/modules/test-structure" + terratest "github.com/gruntwork-io/terratest/modules/testing" "github.com/stretchr/testify/require" ) @@ -128,6 +131,21 @@ func test201VmssPackerJumpbox(t *testing.T) { if tenantId := os.Getenv("ARM_TENANT_ID"); !useMsi && tenantId != "" { packerVars["tenant_id"] = tenantId } + patches := gomonkey.ApplyFunc(shell.RunCommandAndGetOutputE, func(t terratest.TestingT, command shell.Command) (string, error) { + output, err := shell.RunCommandAndGetStdOutE(t, command) + if err != nil { + return output, err + } + + if len(command.Args) == 1 && command.Args[0] == "-version" { + output = strings.TrimPrefix(output, "Packer ") + output = strings.TrimPrefix(output, "v") + output = strings.Split(output, "\n")[0] + } + return output, nil + }) + defer patches.Reset() + _, err := packer.BuildArtifactE(t, &packer.Options{ Template: pkrCfg, Vars: packerVars, diff --git a/test/go.mod b/test/go.mod index f0d81550..87fec874 100644 --- a/test/go.mod +++ b/test/go.mod @@ -4,6 +4,7 @@ go 1.19 require ( github.com/Azure/terraform-module-test-helper v0.16.0 + github.com/agiledragon/gomonkey/v2 v2.11.0 github.com/gruntwork-io/terratest v0.43.9 github.com/stretchr/testify v1.8.4 )