How to start/stop Azure Container Apps using Logic Apps

I have a few demo apps running in Azure Container Apps. Container Apps are a great platform for getting “just enough Kubernetes”.

However, when I’m not doing a demo, I don’t want to be charged for the Container Apps running. I wanted to automate starting & stopping Container Apps.

Luckily, the Azure REST API makes it easy to start & stop the Container Apps programmatically with a POST command.

To issue the correct command, Logic Apps includes a connector for making calls to the Azure REST API.

I need to set up a managed identity for the Logic App to run as. I will also need to grant it enough RBAC permissions to invoke the start/stop command.

Here are the steps of the workflow.

  1. When a HTTP request is received
  2. List resources by resource group
  3. Parse JSON
  4. For each
  5. Compose
  6. Invoke resource operation

When a HTTP request is received

First, I need to trigger my Logic App. I can do this via the Azure portal. I want to pass in a single body argument; which action I want to take (start, stop). Here is the definition.

List resources by resource group

I need to loop over all the Container Apps in my resource group. Therefore, I need to get a list of all the Container Apps. I will use the “List resources by resource group” action & filter on “resourceType eq ‘Microsoft.App/containerApps'”. I will also set up this action to use my managed identity to issue the query. This data comes from the Azure REST API also.

Parse JSON

Now that I have the output of the previous step with the list of Container Apps, I need to parse the result so I can use it easily in the next steps.

To make it easier to configure this step, I ran the Logic App and copied the result of the “List resources” step. I can use this output and the “Use sample payload to generate schema” command to help write the “Schema”. The most important field is the “name”, which I will need in the next step.

For each

I will now loop over each item from the body of the previous step.

Compose

I only need the “name” field from the output of the “Parse JSON” step, so let’s extract that with a “Compose” action.

Invoke resource operation

Now we can run the REST API call to start or stop each Container App.

  • We want to run this command against the “Microsoft.App” resource provider
  • The resource ID will be the string “containerApps/<name>” from the “Compose” step
  • We can select the “2023-05-01” API version based upon the docs
  • The “action” comes from the body of the original HTTP request to the Logic App. This lets us easily use the same Logic App to start & stop the system

Run the Logic App

We can execute the Logic App from the Azure portal or via a HTTP request.

We can see the results in the “Run History” blade.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *