Skip to content

Instantly share code, notes, and snippets.

@joatmon08
Last active March 13, 2024 10:40
Show Gist options
  • Save joatmon08/f773521e739570974d736d82be184e9d to your computer and use it in GitHub Desktop.
Save joatmon08/f773521e739570974d736d82be184e9d to your computer and use it in GitHub Desktop.
Terraform Example for Azure App Gateway & App Service
locals {
app_services = [
{
kind = "Linux"
sku = {
tier = "Standard"
size = "S1"
}
},
{
kind = "Windows"
sku = {
tier = "Basic"
size = "B1"
}
}
]
}
resource "azurerm_app_service_plan" "example" {
count = length(local.app_services)
name = "${lower(local.app_services[count.index].kind)}-asp"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
kind = local.app_services[count.index].kind
reserved = true
sku {
tier = local.app_services[count.index].sku.tier
size = local.app_services[count.index].sku.size
}
}
resource "azurerm_app_service" "example" {
count = length(local.app_services)
name = "${lower(local.app_services[count.index].kind)}-appservice"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
app_service_plan_id = azurerm_app_service_plan.example[count.index].id
site_config {
dotnet_framework_version = "v4.0"
remote_debugging_enabled = true
remote_debugging_version = "VS2015"
}
}
locals {
backend_probe_name = "${azurerm_virtual_network.example.name}-probe"
http_setting_name = "${azurerm_virtual_network.example.name}-be-htst"
public_ip_name = "${azurerm_virtual_network.example.name}-pip"
}
resource "azurerm_public_ip" "example" {
name = local.public_ip_name
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
allocation_method = "Dynamic"
}
resource "azurerm_application_gateway" "network" {
depends_on = [azurerm_public_ip.example]
name = "example-appgateway"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
sku {
name = "Standard_Small"
tier = "Standard"
capacity = 2
}
gateway_ip_configuration {
name = "my-gateway-ip-configuration"
subnet_id = azurerm_subnet.example.0.id
}
dynamic "frontend_port" {
for_each = azurerm_app_service.example
content {
name = "${azurerm_virtual_network.example.name}-${frontend_port.value.name}-feport"
port = "808${frontend_port.key}"
}
}
frontend_ip_configuration {
name = "${azurerm_virtual_network.example.name}-feip"
public_ip_address_id = azurerm_public_ip.example.id
}
dynamic "backend_address_pool" {
for_each = azurerm_app_service.example
content {
name = "${azurerm_virtual_network.example.name}-${backend_address_pool.value.name}-beap"
fqdns = [backend_address_pool.value.default_site_hostname]
}
}
probe {
name = local.backend_probe_name
protocol = "Http"
path = "/"
interval = 30
timeout = 120
unhealthy_threshold = 3
pick_host_name_from_backend_http_settings = true
match {
body = "Welcome"
status_code = [200, 399]
}
}
backend_http_settings {
name = local.http_setting_name
probe_name = local.backend_probe_name
cookie_based_affinity = "Disabled"
path = "/"
port = 80
protocol = "Http"
request_timeout = 120
pick_host_name_from_backend_address = true
}
dynamic "http_listener" {
for_each = azurerm_app_service.example
content {
name = "${azurerm_virtual_network.example.name}-${http_listener.value.name}-httplstn"
frontend_ip_configuration_name = "${azurerm_virtual_network.example.name}-feip"
frontend_port_name = "${azurerm_virtual_network.example.name}-${http_listener.value.name}-feport"
protocol = "Http"
}
}
dynamic "request_routing_rule" {
for_each = azurerm_app_service.example
content {
name = "${azurerm_virtual_network.example.name}-${request_routing_rule.value.name}-rqrt"
rule_type = "Basic"
http_listener_name = "${azurerm_virtual_network.example.name}-${request_routing_rule.value.name}-httplstn"
backend_address_pool_name = "${azurerm_virtual_network.example.name}-${request_routing_rule.value.name}-beap"
backend_http_settings_name = local.http_setting_name
}
}
}
locals {
cidr_block = "10.254.0.0/16"
subnets = {
frontend = cidrsubnet(local.cidr_block, 8, 0)
}
}
resource "azurerm_resource_group" "example" {
name = "example-resources"
location = "West US"
}
resource "azurerm_virtual_network" "example" {
name = "example-network"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
address_space = [local.cidr_block]
}
resource "azurerm_subnet" "example" {
count = length(keys(local.subnets))
name = keys(local.subnets)[count.index]
resource_group_name = azurerm_resource_group.example.name
virtual_network_name = azurerm_virtual_network.example.name
address_prefix = local.subnets[keys(local.subnets)[count.index]]
}
terraform {
required_version = "~> 0.12"
backend "remote" {}
}
provider "azurerm" {
version = "~> 1.30"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment