Creating Azure function: In this section, a new function app and function will be created. The purpose of the Azure function within the solution is to send notification messages to users regarding the expiry of secrets in the key vault. A single function will be responsible for sending both emails and SMS messages. This could have been divided into two separate functions. The first step is to create a new function app and host a function within it:
- As we have done before, navigate to your resource group, click on the +Add button in the top menu, and search for the function app resource. Click on the Create button to get the function app form:
- Fill up the function app form and click on the Create button. The name of the function app must be unique across Azure:
- Once the function app is provisioned, create a new function called SMSandEMailFunction by clicking on the + button next to the Functions item in the left-hand menu, selecting In-portal from the central dashboard, and selecting more templates:
- Select HTTP trigger and name it SMSandEMailFunction. Click on the Create button. The authorization level could be any value:
- Remove the default code, replace it with the code shown in the following listing, and click on the Save button in the top menu:
#r "SendGrid"
#r "Newtonsoft.Json"
#r "Twilio.Api"
using System.Net;
using System;
using SendGrid.Helpers.Mail;
using Microsoft.Azure.WebJobs.Host;
using Newtonsoft.Json;
using Twilio;
using System.Configuration;
public static HttpResponseMessage Run(HttpRequestMessage req, TraceWriter log, out Mail message,out SMSMessage sms)
{
log.Info("C# HTTP trigger function processed a request.");
string alldata = req.Content.ReadAsStringAsync().GetAwaiter().GetResult();
message = new Mail();
var personalization = new Personalization();
personalization.AddBcc(new Email(ConfigurationManager.AppSettings["bccStakeholdersEmail"]));
personalization.AddTo(new Email(ConfigurationManager.AppSettings["toStakeholdersEmail"]));
var messageContent = new Content("text/html", alldata);
message.AddContent(messageContent);
message.AddPersonalization(personalization);
message.Subject = "Key Vault assets Expiring soon..";
message.From = new Email(ConfigurationManager.AppSettings["serviceEmail"]);
string msg = alldata;
sms = new SMSMessage();
sms.Body = msg;
sms.To = ConfigurationManager.AppSettings["adminPhone"];
sms.From = ConfigurationManager.AppSettings["servicePhone"];
return req.CreateResponse(HttpStatusCode.OK, "Hello ");
}
- Click on the function app name in the left-hand menu and click again on the Application settings link in the main window:
- Navigate to the Application settings section as shown in the previous screenshot and add a few entries by clicking on + Add new setting for each entry.
Note that the entries are in the form of key-value pairs, and the values should be actual real-time values. Both adminPhone and servicePhone should already be configured on the Twilio website. servicePhone is the phone number generated by Twilio used for sending SMS messages, and adminPhone is the phone number of the administrator to whom the SMS should be sent.
Also note that Twilio expects the destination phone number to be in a particular format depending on the country (for India, the format is +91 xxxxx xxxxx). Note the spaces and country code in the number.
We also need to add the keys for both SendGrid and Twilio within the application settings. These settings are mentioned in the following list. Readers may already have these values handy because of activities performed in the earlier steps:
- The value of SendGridAPIKeyAsAppSetting is the key for SendGrid.
- TwilioAccountSid is the system identifier for the Twilio account. This value was already copied and stored in a transient place in an earlier step.
- TwilioAuthToken is the token for the Twilio account. This value was already copied and stored in a temporary place in an earlier step.
- Save the settings by clicking on the Save button in the top menu:
- Click on the Integrate link in the left menu just below the name of the function, and click on + New Output. This is to add an output for the SendGrid service:
- Select SendGrid. It might ask you to install the SendGrid extension. Install the extension, which will take a couple of minutes:
- After installing the extension, the output configuration form appears. The important configuration items in this form are Message parameter name and SendGridAPIKeyAppSetting. Leave the default value for Message parameter name and click on the drop-down list to select SendGridAPIKeyAsAppSetting as the API app setting key. This was already configured in a previous step within the app settings configuration. The form should be configured as shown in the following screenshot, and then click on the Save button:
- Click on + New Output again. This is to add an output for the Twilio service.
- Select Twilio SMS. It might ask you to install the Twilio SMS extension. Install the extension, which will take a couple of minutes.
- After installing the extension, the output configuration form appears. The important configuration items in this form are Message parameter name, Account SID setting, and Auth Token Setting. Change the default value for the Message parameter name to sms. This is done because the message parameter is already used for the SendGrid service parameter. Ensure that the value of Account SID setting is TwilioAccountSid and the value of the Auth Token Setting is TwilioAuthToken. These values were already configured in a previous step within the app settings configuration. The form should be configured as shown in the following screenshot, and then click on Save: