Azure User Group Nepal

Learn and understand the cloud technology using Microsoft Azure platform to adopt it

Highly Scalable Application : Logic App, Cosmos DB and APIM.

Author: Sakaldeep

March 05, 2019

In this post, we will create a scalable registration application using Logic App and will store the data into the NoSQL database (Cosmos DB), after that will send the email notification. The application will be secure by Azure Active Directory (AAD) authentication.

High-level step

  1. Create Logic App
  2. Create Cosmos DB and Collection
  3. Configure Logic App to write data to Cosmos DB.
  4. Configure Logic App to send email notianfication.
  5. Create APIM – multi reason
  6. Import Logic App to APIM

    Create Logic App

    Logic App is an Azure app service that helps to design and automate a business process, workflow, system integration, enterprise integration for both cloud and on-premises.  Here, we are going to create a Logic App application that will accept an http request from the user.

    1. Click All Services and search for Logic Apps, and click on it. In the new window click on Add Logic Apps.


    2.  Specify the following parameters and click on Create.

    • Name: Demo-logicapp
    • Resource group name: Demo-RG
    • Location: West Europe

    3.  Select the newly created Logic App and click on the Logic app designer, and then click on Blank Logic App.

    4. Search for ‘HTTP request‘ then click on When an HTTP request is received under Triggers.

    5. Click on Use sample payload to generate a schema.

    6. Paste below code and click on Done to generate JSON.

    {
    "id":101,
    "firstname":"Sakaldeep",
    "lastname":"Yadav",
    "email":"abc@demo.com",
    "org":"Demo",
    "source":"erb"
    }
    

    7. The JSON code has generated as below. Click on New Step.

    8. Search for ‘response‘ and select Response under Actions.

    9. Select the below parameters.

    10. Verify the headers and click on Save.

    Now Let’s test the Logic App is receiving the HTTP request and responding to it. We can many tools to use this such as postman. Here we use Azure CLI Cloud Shell.

    11. Click on CLoud Shell as shown below and run the below commands.

    Copy the HTTP POST URL as shown below, this will need to run the Curl command. First, assign the URL to the variable called endpoint and then run the curl command.

    endpoint='https://prod-05.westeurope.logic.azure.com:443/workflows/382def929d2a4f00805683ff2598dcdf/triggers/manual/paths/invoke?api-version=2016-10-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=fZrJvbfmHVipEPojMoGpkZoto6JwpdnsOPhhv2V8sYY'

    curl -X POST $endpoint   --include   --header 'Content-Type: application/json'   --data '{
        "id": 101,
       "firstname": "Sakaldeep",
      "lastname": "Yadav",
      "email": "abc@demo.com",
      "org": "Demo",
      "source": "web"
    }'

    12. Click on Overview and we can see the logic app has run successfully. Click on Succeeded.

    13. We can see the logic app has received the HTTP request.

    14. It has responded as specified.

    The logic is working as expected. Now we need to store the data to the database permanently. Now we will create Cosmos DB and then write data into.

    Create Cosmos DB and Collection

    15. Click All Services and search for Cosmos DB, and click on it. In the new window click on Add.

    16. Select Basic and specify the following parameters and click on Create.

    • Account Name: skldemocosmosdb
    • API: Core(SQL)

    17. The Cosmos DB has created, now we have to create  Database and Collection to store the data.

    18. We will create a database using Azure CLI. Navigate to the CLI and run the below command.

    az cosmosdb database create    --name "skldemocosmosdb"    --db-name "myDatabase"    --resource-group "Demo-RG"
    
    

    19.  Run the below command to create a collection.

    az cosmosdb collection create    --collection-name "Container01"    --name "skldemocosmosdb"    --db-name "myDatabase"    --resource-group "Demo-RG"    --throughput 400
    

    20. The database and collection have created.

    21. Copy the URI of cosmos DB, we will need this URI in the logic app to integrate the logic app to the cosmos db.

    22. Again switch to Logic App and click on New Step and search for Condition. Select Condition under Actions.

    23. Add the condition as shown below. Here, we are putting a condition on ’email’. Every email address must contain ‘@’ so we are putting the condition on this. If the email address contains ‘@’, it means the registration has done and write it to the database.

    24. Search for Cosmos DB and click on Crate and Update Document under Actions.

    25. Paste the URI in Connection Name, select CosmosDB Account and click on Create.

    26. Select Database ID and Collection ID from dropdown. Specify the parameters in Documents.

    27. Switch to Cosmos DB and verify the data has written to the collection.

    Cosmos DB configuration has completed, now we will need to send an email notification.

    Configure Logic App to send an email notification

    28. Click on New Step to configure email notification.

    29. Select Office 365 Outlook. You can use any account as such Gmail.

    30. Click on Send an Email under Actions.

    31. Sign in to the Office 365 account.

    32. Specifies parameters as shown below.

    33. The email has received.

    Save the logic App and run the curl command again.

    34. Data has written to the database again.

    Create APIM (API Management Services) and Import Logic App

    35. Click All Services and search for API Management Services, and click on it. Click on Add to create APIM instance.

    36. Specify the following parameters and click on Create.

    • Name: SKL-Demo-APIM
    • Organization name: Sakaldeep
    • Pricing Tier: Developer (NO SLA)

    37. Deployment is progress.

    38. Deployment completed.

    39. Select APIs and click on Logic App.

    40. Click on Browse and select the logic app.

    41. Click on demo-logicapp that we created earlier.

    42.  Click on Create.

    43. The logic app has imported into APIM and we can access it from https://xxx.azure-api.net.

    Hope this post is informative.

    Total visits : 152