Skip to content
This repository was archived by the owner on Mar 3, 2025. It is now read-only.

Latest commit

 

History

History
161 lines (102 loc) · 6.72 KB

File metadata and controls

161 lines (102 loc) · 6.72 KB

Create Azure function Application to process events from Azure Event Grid

Use Visual Studio Code to create a JavaScript function.

Prerequisites

  1. An Azure account with an active subscription. Create an account for free.
  2. Node.js 16.x or Node.js 18.x (preview). Use the node --version command to check your version.
  3. Visual Studio Code on one of the supported platforms.
  4. The Azure Functions extension for Visual Studio Code.
  5. Azure Functions Core Tools 4.x.

1. Create Azure Function App

  1. Login to Microsoft Azure Portal and choose Resource group.

  2. Choose Create.

  3. Enter Function App in the Search the Marketplace input and choose Function App.

  4. Choose Create.

  5. In the Basics tab,

    • In the Subscription dropdown field, select your Azure subscription.
    • In the Resource Group dropdown field, select the name of the resource group you have created.
    • In the Function App name field, enter a name of your choice.
    • In the Publish field, select Code as option.
    • In the Runtime stack dropdown menu, choose Node.js and select 16LTS in the Version dropdown menu or select your node version.
    • In the Region dropdown field, select your region. Refer Azure geographies that's near you or near other services that your functions can access.
  6. Choose Next : Hosting.

  7. In the Hosting tab, choose the storage account you created for the Storage account dropdown menu.

  8. Choose Review + create to review the app configuration selections.

  9. In the Review + create tab, review your settings and then choose Create to provision and deploy the function app.

    After the Notifications icon in the upper-right corner of the portal indicated Deployment succeeded, choose Go to resource to view your new function app.

2. Create your local project

  1. In your Visual Studio, choose Azure icon in the Activity bar.

  2. Choose Create Function.

  3. When prompted, choose Create new project. Choose the directory location for your project workspace and choose Select.

  4. Provide the following information at the prompts:

    • Select a language for your function project : Choose JavaScript.
    • Select a template for your project's first function : Choose Azure Queue Storage trigger
    • Provide a function name : Enter a name of your choice.
    • Select Setting from local.setting.json : Choose Create new local app setting.
    • Select subscription : Choose Azure subscription.
    • Name the queue from which the message will be read : Enter the Query Name which you have already created in your Resource Group.

    Using this information, Visual Studio Code generates an Azure Functions project with an Azure Queue Storage trigger. You can view the local project files in the Explorer.

  5. Update index.js and add the below code.

    const { EmailClient } = require("@azure/communication-email");
    const connectionString = process.env.COMMUNICATION_SERVICES_CONNECTION_STRING;
    
    module.exports = async function (context, myQueueItem) {
        const prId = myQueueItem.data.PurchaseRequisition;
        context.log('Purchase Requisition :'+ prId);
        const client = new EmailClient(connectionString);
    
        const emailMessage = {
            sender: process.env.SENDER,
            content: {
                subject: `S4Hana System Notification`,
                plainText: `Purchase Requisition ${prId} has been Created.`,
            },
            recipients: {
                to: [
                {
                    email: process.env.RECIPIENT,
                },
                ],
            },
            };
    
            var response = await client.send(emailMessage);
            context.log(response);
    };
    

  6. Open the local.settings.json file. In this file, you need to update and update the values for COMMUNICATION_SERVICES_CONNECTION_STRING, SENDER and RECIPIENT from the communication service instance created in the resource group.

    • For updating the SENDER parameter, open the communication service instance and choose Domains > Domain name.

      From the Azure Managed Domain, copy the value of MailFrom and update this value for SENDER parameter in local.settings.json file. For RECIPIENT parameter, enter an email address of your choice.

    • For updating the COMMUNICATION_SERVICES_CONNECTION_STRING parameter, choose Keys and copy the value of Connection String.

    Your local.settings.json file should look below screenshot.

  7. Install requied node package.

    npm install @azure/communication-email
    

3. Deploy Azure Function App to Microsoft Azure

  1. Choose Azure in the Activity bar and in the Workspace area, select your project folder and choose Deploy button.

  2. Select Subscription.

  3. Select a Resource.

  4. After deployment completes, choose Upload settings.

  5. Choose View Output to view the creation and deployment results.

    Output shows deployment and settings uploads is successful.