Azure Logic Apps – Storing Variables Between Runs

It can be useful to be able to store data between runs of a Logic App in Azure, one of the simpler ways to do this is using Blob Storage. This short post will cover an example showing you how to store simple data to Blob storage, read the saved data, compare it and then update if required.

Assumed Configuration

All examples within this post will assume the following structure for the blob Storage –

  • Storage Account called “environmentdata”
  • Container called “environment-container”
  • .txt file called “variables.txt”

Prerequisites

You’ll need the following configured for this to work –

  • A Storage Account
  • A Container within that Storage Account
  • A .txt file uploaded within the container with it’s Content-Type set to ‘text/plain’
  • A Logic App

Reading Data

You can read data from the text file by creating a “Get blob content” action, setting the Blob field to the full directory of the file (“/environment-container/variables.txt” if using the assumed setup) and setting “Infer Content Type” to “Yes”.

This data will then be available as Dynamic Content called “File Content”.

Storing/Updating Data

You can store data using the “Create blob” action, which will either create or overwrite an existing file. The example below shows how to store the current runtime to a file called “variables.txt” located within the “environment-container” container.

Example Application

The image below shows a Logic App which has been configured to trigger when a Service Queue receives a message from an IoT Hub, in my case it’s to alert when a Raspberry Pi connected to temperature and humidity sensors triggers an alert.

In my case, this Logic App when triggered is likely to continue triggering until the issue is resolved and it’s less than ideal to send an email every single time the Logic App is triggered. So by storing the last run time I can then compare that to the current time and only trigger an email if it’s been at least 10 minutes since the last one.

The shown Logic App is not used in a production environment and is simply a proof of concept, you may wish to properly handle what should happen if the blob file doesn’t exist at minimum.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.