Save costs by shutting down AKS Clusters on dev environments

With Your Azure Coach, I am regularly organizing trainings on Azure Kubernetes Service.  To reduce the Azure hosting costs of these expensive clusters, I’ve implemented a Logic App that shuts down every evening all running AKS clusters.  This is how it looks:

Nightly schedule

This is the most simple part to implement.  Just configure the Recurrence trigger like this:

Get all running AKS Clusters

The most performing way to query a potential large amount of Azure resources is Azure Resource Graph.  Via this query, I am able to identify the running AKS clusters:

resources
| where type =~ 'Microsoft.ContainerService/managedClusters' and properties.powerState.code == 'Running'

There is no native way to call Azure Resource Graph in Logic Apps, but we can always rely on the extremely powerful combination of the HTTP connector with Managed Identity!  All the details can be found in this excellent example.

Stop AKS Cluster

Once we know which AKS clusters are still running, we can easily loop through them.  Via the Azure Management REST API, we can easily stop the appropriate AKS cluster.  Again, Managed Identity is used to avoid secret management.

Conclusion

A simple way to save costs by shutting down AKS clusters automatically.  In many scenarios, you want to restrict the scope to certain resource groups or subscriptions; maybe you want to make it more dynamic by leveraging tags that indicate whether an auto-shutdown is desired or not.  This can be easily achieved by adjusting the Azure Resource Graph query.

Cheers
Toon

ABOUT

MEET THE YOUR AZURE COACH TEAM

Your Azure Coach is specialized in organizing Azure trainings that are infused with real-life experience. All our coaches are active consultants, who are very passionate and who love to share their Azure expertise with you.