Skip to content

Instantly share code, notes, and snippets.

@akki-s
Last active August 11, 2018 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akki-s/7489de67aea29a574a4ba890f297c03f to your computer and use it in GitHub Desktop.
Save akki-s/7489de67aea29a574a4ba890f297c03f to your computer and use it in GitHub Desktop.
An Http Triggered Javascript Azure Function to process event grid events. If it's a validation event, send back the validationCode to event grid.
module.exports = function (context, req) {
for (var events in req.body) {
var body = req.body[events];
if (body.data && body.eventType == "Microsoft.EventGrid.SubscriptionValidationEvent") {
context.log("event grid validation event, validation code: " + body.data.validationCode);
//validate header
if(req.headers['aeg-event-type'] && req.headers['aeg-event-type'] == 'SubscriptionValidation')
{
// Do any additional validation (as required) and then return back the below response
var code = body.data.validationCode;
context.res = { status: 200, body: { "ValidationResponse": code } };
}
else{
context.res = { status: 400, body: "Validation header is missing." };
}
}
else{
//for all other events, do the processing you need to do, such as call another azure function
// or a logic app, send alerts, or emails etc.
// create a response body, with the properties you're interested in, or just send the event in response.
let responseBody = {
"eventType": body.eventType,
"operationName": body.data.operationName,
"resourceProvider": body.data.resourceProvider,
"resourceUri": body.data.resourceUri,
"status": body.data.status,
"subject": body.subject
"subscriptionId": body.data.subscriptionId,
"tenantId": body.data.tenantId,
};
context.log("Resoonse body", responseBody);
context.res = { status: 200, body: responseBody };
}
}
context.done();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment