diff --git a/quickstart/101-aks-cluster/main.tf b/quickstart/101-aks-cluster/main.tf new file mode 100644 index 000000000..396bf17d5 --- /dev/null +++ b/quickstart/101-aks-cluster/main.tf @@ -0,0 +1,42 @@ +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" "azurerm_kubernetes_cluster_name" { + prefix = "cluster" +} + +resource "random_pet" "azurerm_kubernetes_cluster_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_cluster_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..94cf71723 --- /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_cluster_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/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 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