Schedule Start/Stop of Azure Container Instances

10/05/2020

I really like Azure Container Instances (ACI), it is the simplest way to run containers in Azure. However I find it pretty strange that it don’t have built-in scheduled start/stop of the container groups, especially considering that ACI is not really cost effective if you run the containers 24×7. If you need your containers running 24×7 then you are better off using Azure Web Apps or Kubernetes. In the past I have used build containers in Azure Container Registry as they can be scheduled, but they are even more expensive to run that using ACI so not that is not really recommended.

Well enough ranting, lets see how we can fix it ourselves. Obviously we can write some code that we can execute, e.g. from an Azure function that starts or stops our container at specific intervals by invoking the ARM API. But that is a bit too much work, it would preferable to have a no code solution, like Logic App schedule when our container should start and stop.

If you look inside Logic App for ACI actions you were until recently not in much luck as there was only a few ACI specific actions, none of which could start or stop a container group. Luckily those have been added now, together with a few other useful actions for ACI.

Before these actions where added you could use the Azure Resource Manager action Invoke Resource Operation. That is a pretty useful action as it will allow you to call any ARM rest API, that might not otherwise have native Logic App actions.

As an example I can show you how I used it to schedule start and stop of my Minecraft server, that I have blogged about before.

My Logic App for starting the server looks like this

As you can see it is setup with a Recurrence trigger, which ensures that the server is started everyday at 14.00. I have another Logic App setup in a similar way which turns off the server at 21.00.

When the triggers fire, they run the Invoke Resource operation action, which basically calls the ARM API for starting or stopping the container https://docs.microsoft.com/en-us/rest/api/container-instances/containergroups/start.

The magic here is to know that this action exists, then you just need to find the ARM REST API that you are interested in using and check the documentation, for which api-version to use.