Max
Max

Reputation: 9889

Start container instance on web request to FQDN

Let's say we have a (containerized) backend which is only sparely used. Maybe once every couple of days or so, a (static) web front-end calls an API endpoint of that backend.

The backend conveniently happens to be stateless. No data store or anything.

We want to minimize the hosting cost for it, and ideally would like per-second billing. It's only gonna be running for a few minutes every month, and we only want to be charged for that usage. Basically, we want Function as a Service (FaaS), but for a whole backend and not just a single function.

Azure Container Instances appears to be great fit for this scenario. It can spin up the backend in a container when needed. The backend then can shut itself down again after a certain period of non-usage.

So, let's create a container instance...

az container create \
    --resource-group myResourceGroup \
    --name mycontainer \
    --image mycontainerimage \
    --restart-policy Never
    --dns-name-label mybackend123 
    --ports 80

Great, our backend is live at its FQDN http://mybackend123.eastus.azurecontainer.io!

As stated above, it'll shut itself down after a period of non-usage. Thanks to --restart-policy Never, ACI won't restart the container but keep it around in status Stopped.

My question is: is there any way to automatically start the container again if a web request to the FQDN arrives?

Sure, we can wake it up ourselves by running...

az container start --resource-group myResourceGroup --name mycontainer

... or with an equivalent API call. But then the service that does that needs to be running all the time! Ideally, I'd like the container to start itself whenever a request comes in.

Upvotes: 4

Views: 1156

Answers (1)

Brandon Olin
Brandon Olin

Reputation: 412

Azure Container Instances don't have a wehbook or HTTP trigger that will start them. However, you could use an Azure Function or Logic App that would effectively run az container start for you and then call THAT with HTTP. With either of those approaches, you'd have to setup some IAM permissions to give the Function or Logic App permissions to the ACI resource to start it.

One approach would be to:

  1. Create an Azure Function with an HTTP trigger and a managed identity
  2. Give the Managed identity contributor access to ACI container group
  3. Run az container start or the equivalent REST call inside the function to start the ACI container
  4. Call the Azure function (using the function token) to start the container.

Upvotes: 3

Related Questions