How to send a email to the subscription’s admin in Logic Apps

 

Logic Apps provide a way to simplify and implement scalable integrations and workflows in the cloud. Using Logic Apps and Azure Service Management REST API (https://msdn.microsoft.com/en-us/library/azure/ee460799.aspx), you can create an application to manage your service’s resources programmatically. In this article, we will demonstrate how to get the email address of Azure subscription and send an email in Logic Apps dynamically.

Pre works:

1. Create a management certificate

Please follow the steps in this document to create your management certificate:

/en-us/azure/cloud-services/cloud-services-certs-create

2. Upload your management certificate through Azure classic portal

Then you need to upload your management certificate to your subscription (public certificate .cer file) so that it is authorized to perform management operations on your behalf. Please follow this documentation for step-by-step details:

/en-us/azure/azure-api-management-certs

3. Find the API you needed.

In this sample, we will call an REST API to get the User Accounts information:

GET https://management.core.windows.net/\<subscription-id>/principals

https://msdn.microsoft.com/en-us/library/azure/dn469420.aspx

Logic Apps:

Part A: Create a Logic App to get the email of Admin (Parent Logic App).

1. Create a request trigger.

We created a request trigger here to start our workflow. But you can use any other triggers.

More information you can find here: /en-us/azure/logic-apps/logic-apps-http-endpoint

2. Create an HTTP action.

In this action, we will call Azure REST API using client certificate (pfx + password).

Note: You need to base64 encode the pfx file content and embed in the pfx textbox.

clip_image002

3. The format of the response body is a xml file as follows:

<?xml version="1.0" encoding="utf-8"?>

<Principal xmlns=”https://schemas.microsoft.com/windowsazure”>

<Role>role-names-for-user-account</Role>

<Email>email-address-for-user-account</Email>

</Principal>

In Logic Apps, it is more convenient to pass the data as a JSON file between different actions or apps.

You can use @JSON() function to convert the XML content easily in Logic Apps as below:

clip_image004

In real word, you can define any actions after you got the response message.

Here we defined a child logic apps to parse the response and send email -- sendEmail.

Part B: Create a Logic App to parse the response and send email to Admin (Child Logic Apps).

The sendEmail app takes an array of subscription admins and uses Send email action inside a ForEach loop

1. Create a request trigger to accept the response.

Here is a sample of the JSON format playload:

{

"Principals": {

"Principal": [

{

"Role": "ServiceAdministrator;AccountAdministrator",

"Email": "user1@microsoft.com"

},

{

"Role": "CoAdministrator",

"Email": "user2@microsoft.com"

}

]

}

}

Put the above JSON data to https://jsonschema.net to get the its JSON schema.

Paste the schema definition in request trigger. With the help of JSON schema, Logic App could automatically tokenize all properties e.g. Principals, EmailId etc.

clip_image006

2. Create a ForEach action – Loop over all principals

a. Send Email action – Send email using Office 365 send email action.

clip_image008

3. Response action – Child workflows should have response action to be callable from another logic app use native child workflow action.

 

Special thanks to Vinay Singh, Xiaodong Zhu.

 

Ray Wang from DSI team.