From e21bbc4764698da5bedda44764a8637788b151ee Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Sat, 22 Apr 2023 04:20:18 -0700 Subject: [PATCH 1/3] Part of POC to test generating sample code and articles using OpenAI. --- quickstart/101-aks-cluster/main.tf | 63 +++++++++++++++++++++++++ quickstart/101-aks-cluster/outputs.tf | 20 ++++++++ quickstart/101-aks-cluster/providers.tf | 20 ++++++++ quickstart/101-aks-cluster/readme.md | 24 ++++++++++ quickstart/101-aks-cluster/variables.tf | 29 ++++++++++++ 5 files changed, 156 insertions(+) create mode 100644 quickstart/101-aks-cluster/main.tf create mode 100644 quickstart/101-aks-cluster/outputs.tf create mode 100644 quickstart/101-aks-cluster/providers.tf create mode 100644 quickstart/101-aks-cluster/readme.md create mode 100644 quickstart/101-aks-cluster/variables.tf diff --git a/quickstart/101-aks-cluster/main.tf b/quickstart/101-aks-cluster/main.tf new file mode 100644 index 000000000..7bd0f1645 --- /dev/null +++ b/quickstart/101-aks-cluster/main.tf @@ -0,0 +1,63 @@ +resource "random_pet" "rg_name" { + prefix = "rg" +} + +resource "azurerm_resource_group" "rg" { + name = random_pet.rg_name.id + location = var.resource_group_location +} + +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"] +} + +resource "random_pet" "azurerm_kubernetes_cluster_name" { + prefix = "cluster" +} + +resource "random_pet" "azurerm_kubernetes_dns_prefix" { + prefix = "dns" +} + +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_dns_prefix.id + + identity { + type = "SystemAssigned" + } + + default_node_pool { + name = "agentpool" + node_count = var.agent_count + vm_size = var.agent_vm_size + os_disk_size_gb = var.os_disk_size_gb + } + + linux_profile { + admin_username = var.linux_admin_username + + ssh_key { + key_data = jsondecode(azapi_resource_action.ssh_public_key_gen.output)["publicKey"] + } + } +} \ No newline at end of file diff --git a/quickstart/101-aks-cluster/outputs.tf b/quickstart/101-aks-cluster/outputs.tf new file mode 100644 index 000000000..ede4f9e49 --- /dev/null +++ b/quickstart/101-aks-cluster/outputs.tf @@ -0,0 +1,20 @@ +output "resource_group_name" { + value = azurerm_resource_group.rg.name +} + +output "ssh_key_name" { + value = azapi_resource_action.ssh_public_key_gen.resource_id +} + +output "azurerm_kubernetes_cluster_name" { + value = azurerm_kubernetes_cluster.aks.name +} + +output "azurerm_kubernetes_dns_prefix" { + value = azurerm_kubernetes_cluster.aks.dns_prefix +} + +output "control_plane_fqdn" { + value = azurerm_kubernetes_cluster.aks.kube_config[0].host + sensitive = true +} diff --git a/quickstart/101-aks-cluster/providers.tf b/quickstart/101-aks-cluster/providers.tf new file mode 100644 index 000000000..b5075a8a0 --- /dev/null +++ b/quickstart/101-aks-cluster/providers.tf @@ -0,0 +1,20 @@ +terraform { + required_version = ">=1.0" + required_providers { + azapi = { + source = "azure/azapi" + version = "~>1.5" + } + 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-aks-cluster/readme.md b/quickstart/101-aks-cluster/readme.md new file mode 100644 index 000000000..9a58b332e --- /dev/null +++ b/quickstart/101-aks-cluster/readme.md @@ -0,0 +1,24 @@ +# Azure Kubernetes Service + +This template deploys an Azure Kubernetes Service cluster. + +## 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_kubernetes_cluster](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/kubernetes_cluster) + +## Variables + +| Name | Description | Default | +|-|-|-| +| `resource_group_location` | Location of the resource group. | eastus | +| `agent_count` | The number of nodes for the cluster. | 3 | +| `agent_vm_size` | The size of the Virtual Machine. | standard_d2s_v3 | +| `os_disk_size_gb` | Disk size (in GB) to provision for each of the agent pool nodes. This value ranges from 0 to 1023. Specifying 0 will apply the default disk size for that agentVMSize. | 50 | +| `linux_admin_username` | User name for the Linux Virtual Machines. | azureadmin | + +## Example + +To see how to run this example, see [Deploy an Azure Kubernetes Service (AKS) cluster using Terraform](https://learn.microsoft.com/azure/aks/learn/quick-kubernetes-deploy-terraform). diff --git a/quickstart/101-aks-cluster/variables.tf b/quickstart/101-aks-cluster/variables.tf new file mode 100644 index 000000000..53f19a4e0 --- /dev/null +++ b/quickstart/101-aks-cluster/variables.tf @@ -0,0 +1,29 @@ +variable "resource_group_location" { + type = string + description = "Location for all resources." + default = "eastus" +} + +variable "agent_count" { + type = number + description = "The number of nodes for the cluster." + default = 3 +} + +variable "agent_vm_size" { + type = string + description = "The size of the Virtual Machine." + default = "standard_d2s_v3" +} + +variable "os_disk_size_gb" { + type = number + description = "Disk size (in GB) to provision for each of the agent pool nodes. This value ranges from 0 to 1023. Specifying 0 will apply the default disk size for that agentVMSize." + default = 50 +} + +variable "linux_admin_username" { + type = string + description = "User name for the Linux Virtual Machines." + default = "azureadmin" +} \ No newline at end of file From 042e78f2af4bf151f57fcfc253a8e54f89dc4599 Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Fri, 12 May 2023 09:41:54 -0700 Subject: [PATCH 2/3] Making var naming consistent --- quickstart/101-aks-cluster/main.tf | 2 +- quickstart/101-aks-cluster/outputs.tf | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/quickstart/101-aks-cluster/main.tf b/quickstart/101-aks-cluster/main.tf index 7bd0f1645..f57c4bd24 100644 --- a/quickstart/101-aks-cluster/main.tf +++ b/quickstart/101-aks-cluster/main.tf @@ -32,7 +32,7 @@ resource "random_pet" "azurerm_kubernetes_cluster_name" { prefix = "cluster" } -resource "random_pet" "azurerm_kubernetes_dns_prefix" { +resource "random_pet" "azurerm_kubernetes_cluster_dns_prefix" { prefix = "dns" } diff --git a/quickstart/101-aks-cluster/outputs.tf b/quickstart/101-aks-cluster/outputs.tf index ede4f9e49..94cf71723 100644 --- a/quickstart/101-aks-cluster/outputs.tf +++ b/quickstart/101-aks-cluster/outputs.tf @@ -10,7 +10,7 @@ output "azurerm_kubernetes_cluster_name" { value = azurerm_kubernetes_cluster.aks.name } -output "azurerm_kubernetes_dns_prefix" { +output "azurerm_kubernetes_cluster_dns_prefix" { value = azurerm_kubernetes_cluster.aks.dns_prefix } From 7ae7a2616c1c274572b9902a9bb2aabc891f66a2 Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Thu, 18 May 2023 08:34:54 -0700 Subject: [PATCH 3/3] Added ssh.tf file --- quickstart/101-aks-cluster/main.tf | 23 +---------------------- quickstart/101-aks-cluster/ssh.tf | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 22 deletions(-) create mode 100644 quickstart/101-aks-cluster/ssh.tf diff --git a/quickstart/101-aks-cluster/main.tf b/quickstart/101-aks-cluster/main.tf index f57c4bd24..396bf17d5 100644 --- a/quickstart/101-aks-cluster/main.tf +++ b/quickstart/101-aks-cluster/main.tf @@ -7,27 +7,6 @@ resource "azurerm_resource_group" "rg" { location = var.resource_group_location } -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"] -} - resource "random_pet" "azurerm_kubernetes_cluster_name" { prefix = "cluster" } @@ -40,7 +19,7 @@ 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_dns_prefix.id + dns_prefix = random_pet.azurerm_kubernetes_cluster_dns_prefix.id identity { type = "SystemAssigned" diff --git a/quickstart/101-aks-cluster/ssh.tf b/quickstart/101-aks-cluster/ssh.tf new file mode 100644 index 000000000..6e142ef3d --- /dev/null +++ b/quickstart/101-aks-cluster/ssh.tf @@ -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 +} \ No newline at end of file