How to ensure you can revert changes to function apps

As I’ve been playing around with Azure Functions I’ve slowly outgrown the web-based editor. It’s not that it’s not useful, it’s just that I miss intellisense (I’ll come back to this in a later post), and I accidentally deployed a change which broke one of my functions. I’d made dozens of tiny changes, but I simply could not figure out which one it was. Not having a version history, I was kinda screwed.

I had seen the “Configure Continuous Integration” option before, but never really looked at it. I keep my source code in private GitHub repos, so it was relatively trivial to set up a new repo tied to this function app. After reading the setup instructions, however, I was a little confused by what exactly to do to put my existing functions in to repo, but it was actually much simpler than I thought. It turns out one of the best features is the ability to roll back to a previous commit with a single click:

azure-roll-back-ci-deployment

First, I created a new private GitHub repo and cloned it to my local machine. I chose not to use branching – but I guess you could map different function apps to different branches to support a separation between “dev”, “test”, “production” etc. In the root of my repo, I created a folder for each of the functions I wished to deploy, named exactly the same as the existing functions (I assume they’re not case sensitive but I kept to the same case).

Then, I needed to put the actual code in there. Under the visual editor for each of the functions is a “view files” link: view-files. Clicking this, I was able to see the function.json and run.csx files within each function. I simply cut and pasted the code from there to a file of the same name in the relevant folder.

Next, I needed to find the host.json file. That’s a bit more tricky. In the end, I figured the easiest way was to use the Dev Console. Navigate to Function App Settings, and select “Open dev console”. After a few seconds, the dev console appears:

azure-dev-console

This appears to be a Linux shell. You should start in the d:\home\site\wwwroot folder – that’s where host.json lives. Just type cat host.json to see the contents. It turns out mine was empty (just an open and close curly brace):

D:\home\site\wwwroot

> ls
D:\home\site\wwwroot
QueueTriggerCSharp1
fetchDepartureBoardOrchestrator
host.json
postToTwitter
> cat host.json
D:\home\site\wwwroot
{}
>

I created this in the root of my repo, then committed the changes and pushed them back to GitHub. Within a few seconds, I was able to see the change by clicking “Configure continuous integrations” in Function App Settings. My changes deployed immediately. And when I next screw up, because I’m forced to push changes via GIT, I know I’ll be able to roll back to a known-good configuration.

National Rail LDBWS to Twitter

I’ve been playing around with my Nextion and a Particle Photon for a while. The idea is to pull data from a variety of services and have it available on a display by the front door – things like the weather, the outside temperature (from my Netatmo), and the next 3 trains to Seven Sisters from our station. Living, as we do, at the end of the Enfield Town branch line, it can be a bit hit and miss as to whether or not you make the train, or if it’s even running.

Anyway, I got the first part of this working. I regularly poll the National Rail SOAP API, parsing the content in to a simple minified JSON format using a Power App. Took me a while.

Then I discovered that someone has written a JSON proxy for the LDBWS. I’ll try that next.

Logic Apps are so expensive!!

I started writing EnfieldTownBot using Azure Logic Apps. It’s pretty easy, but i soon hit a challenge – it’s so expensive! My app was pretty simple – a trigger, a “for…each”, a condition and a http callout to my Twitter Poster Function App:

enfieldtownbotlogicapp

So – if there are no delays, that’s (recurrence + httprequest + foreach + 3 x (condition)) = 6 actions. There could be up to 9 if the postToTwitter action also triggers. I want to run this function 2x (once for trains FROM Enfield Town, once for trains TO Enfield Town), so that’s 12-18 actions per request. And I want to run it every 15-30 seconds to get the latest information published ASAP. So that’s 48-72 actions per minute. Over a day, that’s 69,120 to 103,680 actions. Over a (31 day) month, that’s 2,142,720 to 3,214,080 actions. Taking a mean of these (2,678,400), and looking at the current pricing, it would cost me £450 a month to run this app. Wow. I don’t care about late trains THAT much…

So – I rewrote the whole lot as a function app (actually 2 or 3 function apps, as some of the items, such as parsing the XML from National Rail to JSON are reusable elsewhere). Each execution takes about 300ms, and it executes (24 hours x 60 minutes per hour x 4 times per minute) = 5,760 per day. Over a month that’s 178,560 executions. That’s well within the “permanent free grant” provided for Functions. In fact, I’d have to run it something like 30,000,000 times per month, or about 600 times per second to even hit a cost of £1.

Quick and easy way to tweet from a function app

After my last post, I spent some time looking through this. Eventually, I found a really lightweight class which does what i need.

After spending some time adding some error handling to the api.request() method, I then wrapped a webrequest around it and created a function app. You can find it here: https://github.com/mnbf9rca/TwitterFunctionApp

the app takes a simple JSON:

{
  “oauth_token“: “<your oAuth token>”,
  “oauth_token_secret“: “<your oAuth token secret>”,
  “oauth_consumer_key“: “<your consumer key>”,
  “oauth_consumer_secret“: “<your consumer secret>”,
  “tweet”: “<the message to send>”
}

You can obtain the oAuth token and oAuth secret by following the instructions on the Twitter Developer site.

The Function App will return a JSON with the data returned by the Twitter API, or an error. Note that as long as it gets SOME response from the Twitter API it’ll return a 200 code. In future I’ll work on making this more granular (e.g. pass through 50x errors).

Twitter API without libraries – for posting as yourself (e.g. a bot)

I have to say that the twitter API documentation is absolutely abysmal. It’s impossible to navigate – calls make reference to other calls but the major problem is that there are almost no examples – they almost all recommend that you use a library. So how on earth are you supposed to learn how the API works? How do you write a bot which tweets as itself (such as my https://twitter.com/EnfieldTownBot)?

anyway – i found a StackExchange article outlining how to use Twitter’s API from Postman – which handles the hashing for you.

Once I got a good grip on the API itself – which isn’t too bad – I now have to figure out how to create an Azure Function App to create and send the tweets – as OAuth 1.0 requires all messages to be signed (hashed). Twitter has a pretty good set of instructions on how to create the hash: https://dev.twitter.com/oauth/overview/creating-signatures

But – oh my gosh. This is a very complicated process. Here are some samples of libraries or code which i’m looking through to try to implement…

http://developer.pearson.com/learningstudio/oauth-1-sample-code

https://code.msdn.microsoft.com/windowsapps/LinkedIn-OAuth-Example-c06d64f5

https://github.com/bittercoder/DevDefined.OAuth-Examples

https://github.com/bittercoder/DevDefined.OAuth

https://www.devexpress.com/Support/Center/CodeCentral/ViewExample.aspx?exampleId=E20020

anyway, I didnt want to lose these links. I’ll come back to this later.

How to: Manage Honeywell Evohome with Azure Logic Apps

When we bought our house a few years ago, we totally gutted it and one of the things we installed was an evohome heating system. Honeywell has an iphone app for the evohome, so recently, I decided to explore the API. Unfortunately, Honeywell doesn’t seem to offer a public API, so I spent a bit of time deconstructing the app with the help of the excellent Charles Proxy.

I’ve published my logic app here on github  https://github.com/mnbf9rca/AzureEvoHome

I also included some logic app recipes for connecting to thethings.io, thingspeak.com and ubidots.com.

Enjoy!