|
| 1 | +resource "random_id" "prefix" { |
| 2 | + byte_length = 8 |
| 3 | +} |
| 4 | + |
| 5 | +resource "random_id" "name" { |
| 6 | + byte_length = 8 |
| 7 | +} |
| 8 | + |
| 9 | +resource "azurerm_resource_group" "main" { |
| 10 | + count = var.create_resource_group ? 1 : 0 |
| 11 | + |
| 12 | + location = var.location |
| 13 | + name = coalesce(var.resource_group_name, "${random_id.prefix.hex}-rg") |
| 14 | +} |
| 15 | + |
| 16 | +locals { |
| 17 | + resource_group = { |
| 18 | + name = var.create_resource_group ? azurerm_resource_group.main[0].name : var.resource_group_name |
| 19 | + location = var.location |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +resource "azurerm_virtual_network" "test" { |
| 24 | + count = var.use_existing_application_gateway ? 1 : 0 |
| 25 | + address_space = ["10.52.0.0/16"] |
| 26 | + location = local.resource_group.location |
| 27 | + name = "${random_id.prefix.hex}-vn" |
| 28 | + resource_group_name = local.resource_group.name |
| 29 | +} |
| 30 | + |
| 31 | +resource "azurerm_subnet" "test" { |
| 32 | + count = var.use_existing_application_gateway ? 1 : 0 |
| 33 | + address_prefixes = ["10.52.0.0/24"] |
| 34 | + name = "${random_id.prefix.hex}-sn" |
| 35 | + resource_group_name = local.resource_group.name |
| 36 | + virtual_network_name = azurerm_virtual_network.test[0].name |
| 37 | +} |
| 38 | + |
| 39 | +resource "azurerm_subnet" "appgw" { |
| 40 | + count = var.use_existing_application_gateway ? 1 : 0 |
| 41 | + |
| 42 | + address_prefixes = ["10.52.1.0/24"] |
| 43 | + name = "${random_id.prefix.hex}-gw" |
| 44 | + resource_group_name = local.resource_group.name |
| 45 | + virtual_network_name = azurerm_virtual_network.test[0].name |
| 46 | +} |
| 47 | + |
| 48 | +# Locals block for hardcoded names |
| 49 | +locals { |
| 50 | + backend_address_pool_name = try("${azurerm_virtual_network.test[0].name}-beap", "") |
| 51 | + frontend_ip_configuration_name = try("${azurerm_virtual_network.test[0].name}-feip", "") |
| 52 | + frontend_port_name = try("${azurerm_virtual_network.test[0].name}-feport", "") |
| 53 | + http_setting_name = try("${azurerm_virtual_network.test[0].name}-be-htst", "") |
| 54 | + listener_name = try("${azurerm_virtual_network.test[0].name}-httplstn", "") |
| 55 | + request_routing_rule_name = try("${azurerm_virtual_network.test[0].name}-rqrt", "") |
| 56 | +} |
| 57 | + |
| 58 | +resource "azurerm_public_ip" "pip" { |
| 59 | + count = var.use_existing_application_gateway ? 1 : 0 |
| 60 | + |
| 61 | + allocation_method = "Static" |
| 62 | + location = local.resource_group.location |
| 63 | + name = "appgw-pip" |
| 64 | + resource_group_name = local.resource_group.name |
| 65 | + sku = "Standard" |
| 66 | +} |
| 67 | + |
| 68 | +resource "azurerm_application_gateway" "appgw" { |
| 69 | + count = var.use_existing_application_gateway ? 1 : 0 |
| 70 | + |
| 71 | + location = local.resource_group.location |
| 72 | + #checkov:skip=CKV_AZURE_120:We don't need the WAF for this simple example |
| 73 | + name = "ingress" |
| 74 | + resource_group_name = local.resource_group.name |
| 75 | + |
| 76 | + backend_address_pool { |
| 77 | + name = local.backend_address_pool_name |
| 78 | + } |
| 79 | + backend_http_settings { |
| 80 | + cookie_based_affinity = "Disabled" |
| 81 | + name = local.http_setting_name |
| 82 | + port = 80 |
| 83 | + protocol = "Http" |
| 84 | + request_timeout = 1 |
| 85 | + } |
| 86 | + frontend_ip_configuration { |
| 87 | + name = local.frontend_ip_configuration_name |
| 88 | + public_ip_address_id = azurerm_public_ip.pip[0].id |
| 89 | + } |
| 90 | + frontend_port { |
| 91 | + name = local.frontend_port_name |
| 92 | + port = 80 |
| 93 | + } |
| 94 | + gateway_ip_configuration { |
| 95 | + name = "appGatewayIpConfig" |
| 96 | + subnet_id = azurerm_subnet.appgw[0].id |
| 97 | + } |
| 98 | + http_listener { |
| 99 | + frontend_ip_configuration_name = local.frontend_ip_configuration_name |
| 100 | + frontend_port_name = local.frontend_port_name |
| 101 | + name = local.listener_name |
| 102 | + protocol = "Http" |
| 103 | + } |
| 104 | + request_routing_rule { |
| 105 | + http_listener_name = local.listener_name |
| 106 | + name = local.request_routing_rule_name |
| 107 | + rule_type = "Basic" |
| 108 | + backend_address_pool_name = local.backend_address_pool_name |
| 109 | + backend_http_settings_name = local.http_setting_name |
| 110 | + priority = 1 |
| 111 | + } |
| 112 | + sku { |
| 113 | + name = "Standard_v2" |
| 114 | + tier = "Standard_v2" |
| 115 | + capacity = 1 |
| 116 | + } |
| 117 | + |
| 118 | + lifecycle { |
| 119 | + ignore_changes = [ |
| 120 | + tags, |
| 121 | + backend_address_pool, |
| 122 | + backend_http_settings, |
| 123 | + http_listener, |
| 124 | + probe, |
| 125 | + request_routing_rule, |
| 126 | + url_path_map, |
| 127 | + ] |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +module "aks" { |
| 132 | + #checkov:skip=CKV_AZURE_141:We enable admin account here so we can provision K8s resources directly in this simple example |
| 133 | + source = "../.." |
| 134 | + |
| 135 | + prefix = random_id.name.hex |
| 136 | + resource_group_name = local.resource_group.name |
| 137 | + kubernetes_version = "1.26" # don't specify the patch version! |
| 138 | + automatic_channel_upgrade = "patch" |
| 139 | + agents_availability_zones = ["1", "2"] |
| 140 | + agents_count = null |
| 141 | + agents_max_count = 2 |
| 142 | + agents_max_pods = 100 |
| 143 | + agents_min_count = 1 |
| 144 | + agents_pool_name = "testnodepool" |
| 145 | + agents_pool_linux_os_configs = [ |
| 146 | + { |
| 147 | + transparent_huge_page_enabled = "always" |
| 148 | + sysctl_configs = [ |
| 149 | + { |
| 150 | + fs_aio_max_nr = 65536 |
| 151 | + fs_file_max = 100000 |
| 152 | + fs_inotify_max_user_watches = 1000000 |
| 153 | + } |
| 154 | + ] |
| 155 | + } |
| 156 | + ] |
| 157 | + agents_type = "VirtualMachineScaleSets" |
| 158 | + azure_policy_enabled = true |
| 159 | + enable_auto_scaling = true |
| 160 | + enable_host_encryption = true |
| 161 | + http_application_routing_enabled = true |
| 162 | + application_gateway_for_ingress = { |
| 163 | + new_gw = var.use_existing_application_gateway ? null : { |
| 164 | + name = "ingress" |
| 165 | + subnet_cidr = "10.225.0.0/16" |
| 166 | + } |
| 167 | + existing_gw = var.use_existing_application_gateway ? { |
| 168 | + id = azurerm_application_gateway.appgw[0].id |
| 169 | + subnet_id = azurerm_subnet.appgw[0].id |
| 170 | + } : null |
| 171 | + } |
| 172 | + local_account_disabled = false |
| 173 | + log_analytics_workspace_enabled = false |
| 174 | + net_profile_dns_service_ip = "10.0.0.10" |
| 175 | + net_profile_service_cidr = "10.0.0.0/16" |
| 176 | + network_plugin = "azure" |
| 177 | + network_policy = "azure" |
| 178 | + os_disk_size_gb = 60 |
| 179 | + private_cluster_enabled = false |
| 180 | + public_network_access_enabled = true |
| 181 | + rbac_aad = true |
| 182 | + rbac_aad_managed = true |
| 183 | + role_based_access_control_enabled = true |
| 184 | + sku_tier = "Standard" |
| 185 | + vnet_subnet_id = var.use_existing_application_gateway ? azurerm_subnet.test[0].id : null |
| 186 | + depends_on = [ |
| 187 | + azurerm_subnet.test, |
| 188 | + ] |
| 189 | +} |
0 commit comments