< Previous Challenge - Home - Next Challenge >
In this challenge, you will create two new Azure Functions written in Node.js, using VS Code. These will be triggered by Event Grid and output to Azure Cosmos DB to save the results of license plate processing done earlier by the ProcessImage
function. The code for these functions is provided in the challenge instructions below.
Two notes about the code you will use:
- The code uses the Azure Node.js Functions Programming Model v4 which has significant differences from previous one, especially around bindings.
- Do not confuse with the Azure Functions runtime, which currently is at 4.x
- Create a new folder to contain your event processing functions, call it "Events" for instance- Create a new Project from the "Workspace (local)" dropdown in the Azure Extension tab of VSCode. Choose the events folder. Select Javascript. Model V4, Event Grid trigger, name
savePlateData
- Replace the code
savePlateData.js
with the following:
const { app, output } = require('@azure/functions');
const cosmosOutput = output.cosmosDB({
databaseName: 'LicensePlates',
containerName: 'Processed',
createIfNotExists: true,
connection: 'cosmosDBConnectionString',
});
app.eventGrid('savePlateData', {
return: cosmosOutput,
handler: (event, context) => {
context.log('Event grid function processed event:', event);
return {
fileName: event.data['fileName'],
licensePlateText: event.data['licensePlateText'],
timeStamp: event.data['timeStamp'],
exported: false,
id: event.id
};
},
});
- Create a new Azure Function App, from the Azure "Resources" section, in the region of your choice
- Deploy the
savePlateData
function to that Azure Function App, from the Azure "Workspace" Local Project you just created. You are asked if you want to Stream logs, click on it. If it disconnects after a while, stop and start again (from the Function App menu in VS Code) - Add a new Application Setting
cosmosDBConnectionString
with the connection string (or the corresponding KeyVault Secret reference) to Cosmos DBLicensePlates
- From the Azure Portal, open the Function app, function
savePlateData
, and go to Integration. From the Event Grid Trigger, add an event grid subscription- Name should contain "
SAVE
" - Event Schema: Event Grid Schema.
- Topic Type: Event Grid Topics.
- Resource: your recently created Event Grid.
- Event Type : Add
savePlateData
- Endpoint : Select
SavePlateData
Function.
- Name should contain "
Now let's repeat the same steps for the second event function:
- From VSCode, create another function in the same project that is triggered by event grid, name
QueuePlateForManualCheckup
. The JS file should appear next to the other one. - Replace the code with the following:
const { app, output } = require('@azure/functions');
const cosmosOutput = output.cosmosDB({
databaseName: 'LicensePlates',
containerName: 'NeedsManualReview',
createIfNotExists: true,
connection: 'cosmosDBConnectionString',
});
app.eventGrid('queuePlateForManualCheckup', {
return: cosmosOutput,
handler: (event, context) => {
context.log('Event grid function processed event:', event);
return {
fileName: event.data['fileName'],
licensePlateText: '',
timeStamp: event.data['timeStamp'],
resolved: false,
id: event.id
};
},
});
- From the portal, go the
queuePlateForManualCheckup
function, add an event grid subscription- Name should contain "
QUEUE
" - Event Schema: Event Grid Schema.
- Topic Type: Event Grid Topics.
- Resource: your recently created Event Grid.
- Add Event Type
queuePlateForManualCheckup
- Endpoint: Select
QueuePlateForManualCheckup
Function.
- Name should contain "
- Your folder structure looks similar to this:
events/
host.json
package.json
events/src/functions/
savePlateData.js
queuePlateForManualCheckup.js
- (other files)
- Both functions do not have any syntax errors
- Both functions have event grid subscriptions
- Both functions have the proper Cosmos DB settings as their outputs
- Open the Function's
LiveStream
, upload a License Plate JPG to the storage account, and validate the event functions are called with the license plate filename, timestamp and text (if successful). - In your CosmosDB, verify the containers contain the information for every scanned license plate.