Skip to content

Instantly share code, notes, and snippets.

@thoughtful-solutions
Last active September 2, 2020 15:28
Show Gist options
  • Select an option

  • Save thoughtful-solutions/ef2829c4899460e9269776220199c7fc to your computer and use it in GitHub Desktop.

Select an option

Save thoughtful-solutions/ef2829c4899460e9269776220199c7fc to your computer and use it in GitHub Desktop.
FaaS-on-Azure-II

Blob Triggered Functions

Create the Azure Resources

  • mti-example provides a function to load JSON files into a storage container satrigger000sc FaaS-on-Azure.md gives an example
  • mti-satrigger provides a function which is triggered every time the storage container is updated
  az account set --subscription "Free Trial"
  az storage account list -o table
  az group create --name satrigger000rg --location westeurope
  az storage account create --name satrigger000sa --resource-group satrigger000rg --location westeurope --kind StorageV2 --sku Standard_LRS
  az storage container create --auth-mode login --account-name satrigger000sa --name satrigger000sc
  az functionapp plan create --name satrigger000sp --resource-group  satrigger000rg --location westeurope --sku B1
  az functionapp create --runtime node --runtime-version 10 --functions-version 2 --resource-group satrigger000rg --storage-account satrigger000sa --name mti-satrigger --plan  satrigger000sp
  az functionapp create --runtime node --runtime-version 10 --functions-version 2 --resource-group satrigger000rg --storage-account satrigger000sa  --name mti-example --plan  satrigger000sp

Create the Blob Storage Trigger

func init satrigger --javascript
cd satrigger
func new --name mti-satrigger --template "Azure Blob Storage trigger"

function.js

The path must contain the name of the storage container which is to be monitored for change.

{
  "bindings": [
    {
      "name": "myBlob",
      "type": "blobTrigger",
      "direction": "in",
      "path": "satrigger000sc",
      "connection": ""
    }
  ]
}

index.js

This is a very simple function it writes to the log file 'D:\home\LogFiles\Application\Functions\Function\' which can be seen in the Diagnostic Console

module.exports = async function (context, myBlob) {
    context.log("JavaScript blob trigger function processed blob \n Blob:", context.bindingData.blobTrigger, "\n Blob Size:", myBlob.length, "Bytes");
};

Publish the function

  func azure functionapp publish mti-satrigger --force --javascript

Diagnostic Console

From the Azure Portal it is possible to open the url

    https://<function-name>.scm.azurewebsites.net/DebugConsole/?shell=powershell`

e.g.

   https://mti-satrigger.scm.azurewebsites.net/DebugConsole/?shell=powershell

at this point it is possible to select Powershell and then enter

   cd  D:\home\LogFiles\Application\Functions\Function\mti-satrigger
PS D:\home\LogFiles\Application\Functions\Function\mti-satrigger> dir
    Directory: D:\home\LogFiles\Application\Functions\Function\mti-satrigger
Mode                LastWriteTime         Length Name                          
----                -------------         ------ ----                          
-a----        8/28/2020   3:17 PM           1538 2020-08-28T15-15-26Z-1a57a3cf72.log                         

This log file can then be monitored for output from the function

Have the trigger use the FaaS and have the function make call a REST API

Having a trigger executing when an update occurs to the storage container is more useful when it can cause other things to happen. In this case we want to to call another REST API function

This is a simple change. Update the index.js to continue the new code

index.js updated to support call to REST API

const https = require("https")
module.exports = async function (context, myBlob) {
    context.log("JavaScript blob trigger function processed blob \n Blob:", context.bindingData.blobTrigger, "\n Blob Size:", myBlob.length, "Bytes");
//---
let body = JSON.stringify({
  name:  "mtidev000ixr",  
  dataSourceName: "mtidev000ds",  
  targetIndexName: "mti-results2"
})

let options = {
  hostname: "mtidev000ss.search.windows.net",
  path: "/indexers/mtidev000ixr/run?api-version=2020-06-30",
  port: "443",
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "api-key": "262EXXXXXXXXXXXXXXXXXXXXXXXCD4",
    "Content-Length": Buffer.byteLength(body)
  }
}

https
  .request(options, res => {
    let data = ""
    res.on("data", d => {
      data += d
    })
    res.on("end", () => {
      console.log(data)
    })
  })
  .on("error", console.error)
  .end(body)
//--

};

Publish the function

  func azure functionapp publish mti-satrigger --force --javascript
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment