Skip to content

Instantly share code, notes, and snippets.

@ninjarobot
Last active June 18, 2021 03:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ninjarobot/668bd5e3d2368391d51a08f357bd4d05 to your computer and use it in GitHub Desktop.
Save ninjarobot/668bd5e3d2368391d51a08f357bd4d05 to your computer and use it in GitHub Desktop.
Farmer application with multiple resource groups and traffic manager
#r "nuget: Farmer, 1.6.0"
open Farmer
open Farmer.Builders
open Farmer.Builders.ContainerGroups
open Farmer.Builders.TrafficManager
open Farmer.TrafficManager
let msi = userAssignedIdentity {
name "my-app"
}
let containerApp =
containerGroup {
name "my-containers"
add_identity msi
public_dns "my-container-app-1234" [ TCP, 80us ]
add_instances [
containerInstance {
name "web"
image "nginx"
add_public_ports [ 80us ]
probes [
liveliness {
http "http://localhost:80/"
initial_delay_seconds 15
}
]
}
]
}
arm {
add_resources [
resourceGroup {
name "global-rg"
location Location.EastUS
add_resources [
trafficManager {
name "tm-app-global"
routing_method RoutingMethod.Performance
add_endpoints [
endpoint {
name "eastus-app"
weight 1
priority 1
target_external "my-container-app-1234.eastus.azurecontainer.io" Location.EastUS
}
endpoint {
name "westus-app"
weight 1
priority 2
target_external "my-container-app-1234.westus.azurecontainer.io" Location.WestUS
}
endpoint {
name "westeu-app"
weight 1
priority 3
target_external "my-container-app-1234.westeurope.azurecontainer.io" Location.WestEurope
}
endpoint {
name "india-app"
weight 1
priority 4
target_external "my-container-app-1234.centralindia.azurecontainer.io" Location.CentralIndia
}
endpoint {
name "asia-app"
weight 1
priority 5
target_external "my-container-app-1234.eastasia.azurecontainer.io" Location.EastAsia
}
]
}
]
}
resourceGroup {
name "app-westus-rg"
location Location.WestUS
add_resources [
msi
containerApp
]
}
resourceGroup {
name "app-eastus-rg"
location Location.EastUS
add_resources [
msi
containerApp
]
}
resourceGroup {
name "app-westeu-rg"
location Location.WestEurope
add_resources [
msi
containerApp
]
}
resourceGroup {
name "app-asia-rg"
location Location.EastAsia
add_resources [
msi
containerApp
]
}
resourceGroup {
name "app-india-rg"
location Location.CentralIndia
add_resources [
msi
containerApp
]
}
]
} |> Writer.quickWrite "global-app"
@ninjarobot
Copy link
Author

ninjarobot commented Jun 11, 2021

The ARM template JSON:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "outputs": {},
  "parameters": {},
  "resources": [
    {
      "apiVersion": "2020-10-01",
      "dependsOn": [],
      "name": "global-rg",
      "properties": {
        "expressionEvaluationOptions": {
          "scope": "Inner"
        },
        "mode": "Incremental",
        "parameters": {},
        "template": {
          "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "outputs": {},
          "parameters": {},
          "resources": [
            {
              "apiVersion": "2018-04-01",
              "dependsOn": [],
              "location": "global",
              "name": "tm-app-global",
              "properties": {
                "dnsConfig": {
                  "relativeName": "tm-app-global",
                  "ttl": 30
                },
                "endpoints": [
                  {
                    "name": "eastus-app",
                    "properties": {
                      "endpointLocation": "eastus",
                      "endpointStatus": "Enabled",
                      "priority": 1,
                      "target": "my-container-app-1234.eastus.azurecontainer.io",
                      "weight": 1
                    },
                    "type": "Microsoft.Network/trafficManagerProfiles/externalEndpoints"
                  },
                  {
                    "name": "westus-app",
                    "properties": {
                      "endpointLocation": "westus",
                      "endpointStatus": "Enabled",
                      "priority": 2,
                      "target": "my-container-app-1234.westus.azurecontainer.io",
                      "weight": 1
                    },
                    "type": "Microsoft.Network/trafficManagerProfiles/externalEndpoints"
                  },
                  {
                    "name": "westeu-app",
                    "properties": {
                      "endpointLocation": "westeurope",
                      "endpointStatus": "Enabled",
                      "priority": 3,
                      "target": "my-container-app-1234.westeurope.azurecontainer.io",
                      "weight": 1
                    },
                    "type": "Microsoft.Network/trafficManagerProfiles/externalEndpoints"
                  },
                  {
                    "name": "india-app",
                    "properties": {
                      "endpointLocation": "centralindia",
                      "endpointStatus": "Enabled",
                      "priority": 4,
                      "target": "my-container-app-1234.centralindia.azurecontainer.io",
                      "weight": 1
                    },
                    "type": "Microsoft.Network/trafficManagerProfiles/externalEndpoints"
                  },
                  {
                    "name": "asia-app",
                    "properties": {
                      "endpointLocation": "eastasia",
                      "endpointStatus": "Enabled",
                      "priority": 5,
                      "target": "my-container-app-1234.eastasia.azurecontainer.io",
                      "weight": 1
                    },
                    "type": "Microsoft.Network/trafficManagerProfiles/externalEndpoints"
                  }
                ],
                "monitorConfig": {
                  "intervalInSeconds": 30,
                  "path": "/",
                  "port": 80,
                  "protocol": "HTTP",
                  "timeoutInSeconds": 10,
                  "toleratedNumberOfFailures": 3
                },
                "profileStatus": "Enabled",
                "trafficRoutingMethod": "Performance",
                "trafficViewEnrollmentStatus": "Disabled"
              },
              "tags": {},
              "type": "Microsoft.Network/trafficManagerProfiles"
            }
          ]
        }
      },
      "resourceGroup": "global-rg",
      "tags": {},
      "type": "Microsoft.Resources/deployments"
    },
    {
      "apiVersion": "2020-10-01",
      "dependsOn": [],
      "name": "app-westus-rg",
      "properties": {
        "expressionEvaluationOptions": {
          "scope": "Inner"
        },
        "mode": "Incremental",
        "parameters": {},
        "template": {
          "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "outputs": {},
          "parameters": {},
          "resources": [
            {
              "apiVersion": "2018-11-30",
              "dependsOn": [],
              "location": "westus",
              "name": "my-app",
              "tags": {},
              "type": "Microsoft.ManagedIdentity/userAssignedIdentities"
            },
            {
              "apiVersion": "2019-12-01",
              "dependsOn": [
                "[resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', 'my-app')]"
              ],
              "identity": {
                "type": "UserAssigned",
                "userAssignedIdentities": {
                  "[resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', 'my-app')]": {}
                }
              },
              "location": "westus",
              "name": "my-containers",
              "properties": {
                "containers": [
                  {
                    "name": "web",
                    "properties": {
                      "command": [],
                      "environmentVariables": [],
                      "image": "nginx",
                      "livenessProbe": {
                        "httpGet": {
                          "path": "/",
                          "port": 80,
                          "scheme": "http"
                        },
                        "initialDelaySeconds": 15
                      },
                      "ports": [
                        {
                          "port": 80
                        }
                      ],
                      "resources": {
                        "requests": {
                          "cpu": 1,
                          "memoryInGB": 1.5
                        }
                      },
                      "volumeMounts": []
                    }
                  }
                ],
                "imageRegistryCredentials": [],
                "initContainers": [],
                "ipAddress": {
                  "dnsNameLabel": "my-container-app-1234",
                  "ports": [
                    {
                      "port": 80,
                      "protocol": "TCP"
                    }
                  ],
                  "type": "Public"
                },
                "osType": "Linux",
                "restartPolicy": "Always",
                "volumes": []
              },
              "tags": {},
              "type": "Microsoft.ContainerInstance/containerGroups"
            }
          ]
        }
      },
      "resourceGroup": "app-westus-rg",
      "tags": {},
      "type": "Microsoft.Resources/deployments"
    },
    {
      "apiVersion": "2020-10-01",
      "dependsOn": [],
      "name": "app-eastus-rg",
      "properties": {
        "expressionEvaluationOptions": {
          "scope": "Inner"
        },
        "mode": "Incremental",
        "parameters": {},
        "template": {
          "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "outputs": {},
          "parameters": {},
          "resources": [
            {
              "apiVersion": "2018-11-30",
              "dependsOn": [],
              "location": "eastus",
              "name": "my-app",
              "tags": {},
              "type": "Microsoft.ManagedIdentity/userAssignedIdentities"
            },
            {
              "apiVersion": "2019-12-01",
              "dependsOn": [
                "[resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', 'my-app')]"
              ],
              "identity": {
                "type": "UserAssigned",
                "userAssignedIdentities": {
                  "[resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', 'my-app')]": {}
                }
              },
              "location": "eastus",
              "name": "my-containers",
              "properties": {
                "containers": [
                  {
                    "name": "web",
                    "properties": {
                      "command": [],
                      "environmentVariables": [],
                      "image": "nginx",
                      "livenessProbe": {
                        "httpGet": {
                          "path": "/",
                          "port": 80,
                          "scheme": "http"
                        },
                        "initialDelaySeconds": 15
                      },
                      "ports": [
                        {
                          "port": 80
                        }
                      ],
                      "resources": {
                        "requests": {
                          "cpu": 1,
                          "memoryInGB": 1.5
                        }
                      },
                      "volumeMounts": []
                    }
                  }
                ],
                "imageRegistryCredentials": [],
                "initContainers": [],
                "ipAddress": {
                  "dnsNameLabel": "my-container-app-1234",
                  "ports": [
                    {
                      "port": 80,
                      "protocol": "TCP"
                    }
                  ],
                  "type": "Public"
                },
                "osType": "Linux",
                "restartPolicy": "Always",
                "volumes": []
              },
              "tags": {},
              "type": "Microsoft.ContainerInstance/containerGroups"
            }
          ]
        }
      },
      "resourceGroup": "app-eastus-rg",
      "tags": {},
      "type": "Microsoft.Resources/deployments"
    },
    {
      "apiVersion": "2020-10-01",
      "dependsOn": [],
      "name": "app-westeu-rg",
      "properties": {
        "expressionEvaluationOptions": {
          "scope": "Inner"
        },
        "mode": "Incremental",
        "parameters": {},
        "template": {
          "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "outputs": {},
          "parameters": {},
          "resources": [
            {
              "apiVersion": "2018-11-30",
              "dependsOn": [],
              "location": "westeurope",
              "name": "my-app",
              "tags": {},
              "type": "Microsoft.ManagedIdentity/userAssignedIdentities"
            },
            {
              "apiVersion": "2019-12-01",
              "dependsOn": [
                "[resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', 'my-app')]"
              ],
              "identity": {
                "type": "UserAssigned",
                "userAssignedIdentities": {
                  "[resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', 'my-app')]": {}
                }
              },
              "location": "westeurope",
              "name": "my-containers",
              "properties": {
                "containers": [
                  {
                    "name": "web",
                    "properties": {
                      "command": [],
                      "environmentVariables": [],
                      "image": "nginx",
                      "livenessProbe": {
                        "httpGet": {
                          "path": "/",
                          "port": 80,
                          "scheme": "http"
                        },
                        "initialDelaySeconds": 15
                      },
                      "ports": [
                        {
                          "port": 80
                        }
                      ],
                      "resources": {
                        "requests": {
                          "cpu": 1,
                          "memoryInGB": 1.5
                        }
                      },
                      "volumeMounts": []
                    }
                  }
                ],
                "imageRegistryCredentials": [],
                "initContainers": [],
                "ipAddress": {
                  "dnsNameLabel": "my-container-app-1234",
                  "ports": [
                    {
                      "port": 80,
                      "protocol": "TCP"
                    }
                  ],
                  "type": "Public"
                },
                "osType": "Linux",
                "restartPolicy": "Always",
                "volumes": []
              },
              "tags": {},
              "type": "Microsoft.ContainerInstance/containerGroups"
            }
          ]
        }
      },
      "resourceGroup": "app-westeu-rg",
      "tags": {},
      "type": "Microsoft.Resources/deployments"
    },
    {
      "apiVersion": "2020-10-01",
      "dependsOn": [],
      "name": "app-asia-rg",
      "properties": {
        "expressionEvaluationOptions": {
          "scope": "Inner"
        },
        "mode": "Incremental",
        "parameters": {},
        "template": {
          "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "outputs": {},
          "parameters": {},
          "resources": [
            {
              "apiVersion": "2018-11-30",
              "dependsOn": [],
              "location": "eastasia",
              "name": "my-app",
              "tags": {},
              "type": "Microsoft.ManagedIdentity/userAssignedIdentities"
            },
            {
              "apiVersion": "2019-12-01",
              "dependsOn": [
                "[resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', 'my-app')]"
              ],
              "identity": {
                "type": "UserAssigned",
                "userAssignedIdentities": {
                  "[resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', 'my-app')]": {}
                }
              },
              "location": "eastasia",
              "name": "my-containers",
              "properties": {
                "containers": [
                  {
                    "name": "web",
                    "properties": {
                      "command": [],
                      "environmentVariables": [],
                      "image": "nginx",
                      "livenessProbe": {
                        "httpGet": {
                          "path": "/",
                          "port": 80,
                          "scheme": "http"
                        },
                        "initialDelaySeconds": 15
                      },
                      "ports": [
                        {
                          "port": 80
                        }
                      ],
                      "resources": {
                        "requests": {
                          "cpu": 1,
                          "memoryInGB": 1.5
                        }
                      },
                      "volumeMounts": []
                    }
                  }
                ],
                "imageRegistryCredentials": [],
                "initContainers": [],
                "ipAddress": {
                  "dnsNameLabel": "my-container-app-1234",
                  "ports": [
                    {
                      "port": 80,
                      "protocol": "TCP"
                    }
                  ],
                  "type": "Public"
                },
                "osType": "Linux",
                "restartPolicy": "Always",
                "volumes": []
              },
              "tags": {},
              "type": "Microsoft.ContainerInstance/containerGroups"
            }
          ]
        }
      },
      "resourceGroup": "app-asia-rg",
      "tags": {},
      "type": "Microsoft.Resources/deployments"
    },
    {
      "apiVersion": "2020-10-01",
      "dependsOn": [],
      "name": "app-india-rg",
      "properties": {
        "expressionEvaluationOptions": {
          "scope": "Inner"
        },
        "mode": "Incremental",
        "parameters": {},
        "template": {
          "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "outputs": {},
          "parameters": {},
          "resources": [
            {
              "apiVersion": "2018-11-30",
              "dependsOn": [],
              "location": "centralindia",
              "name": "my-app",
              "tags": {},
              "type": "Microsoft.ManagedIdentity/userAssignedIdentities"
            },
            {
              "apiVersion": "2019-12-01",
              "dependsOn": [
                "[resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', 'my-app')]"
              ],
              "identity": {
                "type": "UserAssigned",
                "userAssignedIdentities": {
                  "[resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', 'my-app')]": {}
                }
              },
              "location": "centralindia",
              "name": "my-containers",
              "properties": {
                "containers": [
                  {
                    "name": "web",
                    "properties": {
                      "command": [],
                      "environmentVariables": [],
                      "image": "nginx",
                      "livenessProbe": {
                        "httpGet": {
                          "path": "/",
                          "port": 80,
                          "scheme": "http"
                        },
                        "initialDelaySeconds": 15
                      },
                      "ports": [
                        {
                          "port": 80
                        }
                      ],
                      "resources": {
                        "requests": {
                          "cpu": 1,
                          "memoryInGB": 1.5
                        }
                      },
                      "volumeMounts": []
                    }
                  }
                ],
                "imageRegistryCredentials": [],
                "initContainers": [],
                "ipAddress": {
                  "dnsNameLabel": "my-container-app-1234",
                  "ports": [
                    {
                      "port": 80,
                      "protocol": "TCP"
                    }
                  ],
                  "type": "Public"
                },
                "osType": "Linux",
                "restartPolicy": "Always",
                "volumes": []
              },
              "tags": {},
              "type": "Microsoft.ContainerInstance/containerGroups"
            }
          ]
        }
      },
      "resourceGroup": "app-india-rg",
      "tags": {},
      "type": "Microsoft.Resources/deployments"
    }
  ]
}

@ninjarobot
Copy link
Author

ninjarobot commented Jun 11, 2021

Create the resource groups in different regions, then run the global deployment, which deploys Traffic Manager with optimized routing and failover to traffic in different regions:

az group create -g global-rg --location EastUS
az group create -g app-eastus-rg --location EastUS
az group create -g app-westus-rg --location WestUS
az group create -g app-westeu-rg --location WestEurope
az group create -g app-india-rg --location CentralIndia
az group create -g app-asia-rg --location EastAsia
az deployment sub create --location EastUS --template-file global-app.json

@ninjarobot
Copy link
Author

Cleanup

az group delete -g global-rg -y --no-wait
az group delete -g app-eastus-rg -y --no-wait
az group delete -g app-westus-rg -y --no-wait
az group delete -g app-westeu-rg -y --no-wait
az group delete -g app-india-rg -y --no-wait
az group delete -g app-asia-rg -y --no-wait

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment