0

I am developing an Azure Logic App to handle notifications for several events (near expiry, expired) on Azure Key Vaults. My setup involves an Event Grid for each Key Vault, all sending their events to the same Logic App.

When Event Grid sends events to the Logic App, they come as an array of events. I am trying to use a 'For each' loop in my Logic App to process each of these events individually. Here's the relevant portion of my Logic App:

I have a 'When a HTTP request is received' trigger, Followed by a 'For each' loop with triggerBody() as the input, Inside the loop, I have a 'Send an email (V2)' action. In the 'Send an email (V2)' action, I'm trying to access the properties of each event using the items() function like this: enter image description here

Event Type: @{items()?['eventType']}
Key Vault Name: @{items()?['data']?['VaultName']}
Object Type: @{items()?['data']?['ObjectType']}
Object Name: @{items()?['data']?['ObjectName']}
Expiry Date: @{items()?['data']?['EXP']}

But when I try to save the Logic App, I get this error message:

Failed to save logic app. The template validation failed: 'The template action 'Send_an_email_(V2)' at line '1' and column '1679' is not valid: "The template language function 'items' must have at least one parameter.".'.

I have reviewed the documentation and understand that items() doesn't require a parameter when used inside a 'For each' loop, as it automatically refers to the current item of the array that 'For each' is looping through.

What am I doing wrong? How can I set up my Logic App to correctly process each event individually using a 'For each' loop and the items() function?

Below is my schema input:

{
    "schema": {
        "properties": {
            "$schema": {
                "type": "string"
            },
            "properties": {
                "properties": {
                    "data": {
                        "properties": {
                            "properties": {
                                "properties": {
                                    "EXP": {
                                        "properties": {
                                            "type": {
                                                "type": "string"
                                            }
                                        },
                                        "type": "object"
                                    },
                                    "Id": {
                                        "properties": {
                                            "type": {
                                                "type": "string"
                                            }
                                        },
                                        "type": "object"
                                    },
                                    "NBF": {
                                        "properties": {
                                            "type": {
                                                "type": "string"
                                            }
                                        },
                                        "type": "object"
                                    },
                                    "ObjectName": {
                                        "properties": {
                                            "type": {
                                                "type": "string"
                                            }
                                        },
                                        "type": "object"
                                    },
                                    "ObjectType": {
                                        "properties": {
                                            "type": {
                                                "type": "string"
                                            }
                                        },
                                        "type": "object"
                                    },
                                    "VaultName": {
                                        "properties": {
                                            "type": {
                                                "type": "string"
                                            }
                                        },
                                        "type": "object"
                                    },
                                    "Version": {
                                        "properties": {
                                            "type": {
                                                "type": "string"
                                            }
                                        },
                                        "type": "object"
                                    }
                                },
                                "type": "object"
                            },
                            "required": {
                                "items": {
                                    "type": "string"
                                },
                                "type": "array"
                            },
                            "type": {
                                "type": "string"
                            }
                        },
                        "type": "object"
                    },
                    "dataVersion": {
                        "properties": {
                            "type": {
                                "type": "string"
                            }
                        },
                        "type": "object"
                    },
                    "eventTime": {
                        "properties": {
                            "type": {
                                "type": "string"
                            }
                        },
                        "type": "object"
                    },
                    "eventType": {
                        "properties": {
                            "type": {
                                "type": "string"
                            }
                        },
                        "type": "object"
                    },
                    "id": {
                        "properties": {
                            "type": {
                                "type": "string"
                            }
                        },
                        "type": "object"
                    },
                    "metadataVersion": {
                        "properties": {
                            "type": {
                                "type": "string"
                            }
                        },
                        "type": "object"
                    },
                    "subject": {
                        "properties": {
                            "type": {
                                "type": "string"
                            }
                        },
                        "type": "object"
                    },
                    "topic": {
                        "properties": {
                            "type": {
                                "type": "string"
                            }
                        },
                        "type": "object"
                    }
                },
                "type": "object"
            },
            "required": {
                "items": {
                    "type": "string"
                },
                "type": "array"
            },
            "type": {
                "type": "string"
            }
        },
        "type": "object"
    }
}

sample of the output:

[
    {
        "id": "5a",
        "topic": "/subscriptions/8",
        "subject": "kv",
        "eventType": "Microsoft.KeyVault.SecretNearExpiry",
        "data": {
            "Id": "https://kv",
            "VaultName": "kv-v",
            "ObjectType": "Secret",
            "ObjectName": "kv",
            "Version": "",
            "NBF": null,
            "EXP": "2023-06-13T00:00:00Z"
        },
        "dataVersion": "1",
        "metadataVersion": "1",
        "eventTime": "2023-06-12T00:00:00Z"
    }
]
Auto geek
  • 464
  • 1
  • 6
  • 21

1 Answers1

1

I have tried to reproduce the issue by using one logic app for 2 key vaults and individual event grids for each key vaults subscribing multiple event types-

enter image description here

enter image description here

enter image description here

These two key vaults publishing events to single logic app-

enter image description here

Here in "Select an output from Previous steps", I am taking triggerBody().

enter image description here

Event Id: @{items('For_each')?['id']}, 
Event Type: @{items('For_each')?['eventType']}, 
Subject: @{items('For_each')?['subject']}, 
Key Vault Name: @{items('For_each')?['data']?['VaultName']}, 
Object Type: @{items('For_each')?['data']?['ObjectType']}, 
Object Name: @{items('For_each')?['data']?['ObjectName']}

Request Body-

{
"items": {
"properties": {
"data": {
"properties": {
"EXP": {
"type": "string"
},
"Id": {
"type": "string"
},
"NBF": {
"type": "string"
},
"ObjectName": {
"type": "string"
},
"ObjectType": {
"type": "string"
},
"VaultName": {
"type": "string"
},
"Version": {
"type": "string"
}
},
"type": "object"
},
"dataVersion": {
"type": "string"
},
"eventTime": {
"type": "string"
},
"eventType": {
"type": "string"
},
"id": {
"type": "string"
},
"metadataVersion": {
"type": "string"
},
"subject": {
"type": "string"
},
"topic": {
"type": "string"
}
},
"type": "object"
},
"type": "array"
}

Output which I am getting is-

[
{
"id": "0XXXXXXXXXXXXXXXX",
"topic": "/subscriptions/xxxxxxxxx/resourceGroups/iafrin-xxxx/providers/Microsoft.KeyVault/vaults/afreen-key-002",
"subject": "afreenSecret001",
"eventType": "Microsoft.KeyVault.SecretNearExpiry",
"data": {
"Id": "https://afreen-key-002.vault.azure.net/secrets/afreenSecret001/xxxxxxxxxx",
"VaultName": "afreen-key-002",
"ObjectType": "Secret",
"ObjectName": "afreenSecret001",
"Version": "yyyyyyyyyyy",
"NBF": null,
"EXP": "2023-06-14T05:20:00Z"
},
"dataVersion": "1",
"metadataVersion": "1",
"eventTime": "2023-06-14T05:19:56.9159226Z"
}
]

I am getting email notification for all the subscribed event types-

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

Ikhtesam Afrin
  • 897
  • 1
  • 1
  • 6