0

When you create an Azure resource, you must specify the properties the newly created resource must have. Once created, many of those properties can be changed; the tags of a resource for example.

I'd like to be able to programmatically (i.e. not via the Portal) retrieve the properties of a resource as they were at initial deployment time of that resource, even in case the resource no longer exists. I don't have a need for a long history, a day or two retention for the creation record is more than sufficient.

How can I achieve this?

Things I've tried:

  • Querying the resourcechanges table in the Resource Graph. I do get the creation record, but the record does not contain the properties of the resource. This is documented behavior: The changes property dictionary is only included when changeType is Update.
  • Looking in Change Analysis. That does give me the deletion event (and properties as they were at deletion time), but it does not give me the creation event.
  • Resource Creation events in Event Grid. Unfortunately, those also don't contain the properties. (If all else fails I could set something up that would, upon reception of the creation event, immediately query the resource, but if at all possible I'd like to avoid this.)

When looking at the Event Log of the resource group, the creation record looks like this (I have randomized the GUIDs):

{
    "targetResourceType": "microsoft.managedidentity/userassignedidentities",
    "changeAttributes": {
        "previousResourceSnapshotId": null,
        "newResourceSnapshotId": "08585057886968675807_6dad72a1-ddf9-4bf8-95b6-9e20644861f1_1719005433_1695818188",
        "correlationId": "c7a2848b-748a-471b-9fac-622028b717b7",
        "changesCount": 0,
        "timestamp": "2023-09-27T12:36:28.6100000Z"
    },
    "targetResourceId": "/subscriptions/e26fef86-e3b2-4558-98b9-8d8553db6ec1/resourceGroups/rg-jo-test/providers/Microsoft.ManagedIdentity/userAssignedIdentities/changetrackingtest",
    "changeType": "Create",
    "changes": {}
}

That newResourceSnapshotId sure looks like it could contain what I need, but I wouldn't know if it's possible to retrieve it, let alone how.

How can I obtain the properties of a resource how they were at deployment time, even when the resource has been deleted at the time of the query?

2
  • I'd also reccomend looking deploying your infrastructure using Infrastructure as Code (Bicep, Terrafrom, Pulumi etc.) that way your configuration is defined up front in a file, so you will always know what the deployment configuration was
    – Sam Cogan
    Oct 3 at 8:27
  • @SamCogan this is for a scenario where the team that does the deploying is not the team that wants to know the properties of the resource at deployment time. I'm primarily interested in the tags and their values as they were at deployment time.
    – Jurjen
    Oct 12 at 12:18

1 Answer 1

0

By querying the deployment logs using PowerShell:

https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/deployment-history

But keep in mind that there is a limit of 600 deployments, if you have more than that azure automatically deletes them.

Another option would be: Azure Monitor provides a feature called "Azure Resource Logs" (formerly known as Activity Logs) that captures the deployment history and other activities in your Azure subscription. You can configure these logs to be sent to Log Analytics for analysis and monitoring.You can query them then using KQL queries, here is an example query:

AzureActivity
| where ResourceGroupName == "<YourResourceGroupName>"
| where OperationName == "Microsoft.Resources/deployments/write"
| project ActivityName, Caller, ResourceId, Resource, ResourceGroup, OperationName, Status, EventTimestamp
| order by EventTimestamp desc

Here is another query

// Define a function to get the resource snapshot by ID
let get_resource_snapshot = (snapshotId:string) {
    ResourceSnapshots
    | where id == snapshotId
    | project properties
};

// Query the resourcechanges table for the creation events
resourcechanges
| where changeType == "Create"
| extend targetResourceId = tostring(properties.targetResourceId),
         newResourceSnapshotId = tostring(properties.changeAttributes.newResourceSnapshotId)
| join kind=leftouter (Resources | extend targetResourceId = id) on targetResourceId // Join with the Resources table to get the current properties
| project targetResourceId, changeType, changeAttributes.timestamp, currentProperties = properties, newResourceSnapshotId // Select the relevant columns
| extend initialProperties = get_resource_snapshot(newResourceSnapshotId) // Invoke the function to get the initial properties from the snapshot ID
| project-away newResourceSnapshotId // Remove the snapshot ID column
| order by changeAttributes.timestamp desc // Order by the creation timestamp in descending order`
4
  • I'm afraid this is not correct, as not all resources are created via a deployment so those won't end up in the deployment logs. For the resources that are created via a deployment, I wasn't able to find the properties of the resource in the deployment log. Your query did not work; the AzureActivity table has a "ResourceGroup" column (not ResourceGroupName), an "OperationNameValue" column (not "OperationName"), and no "ActivityName" column. Am I using it incorrectly?
    – Jurjen
    Oct 12 at 12:41
  • What I suggest going forward is to create your resources using a Git repo and a pipeline where you have the initial configuration. For example Azure Devops with Bicep and yaml pipeline. With Git you can also revert easily to earlier change (commit) and redeploy.
    – Ace
    Oct 17 at 13:15
  • I edited my post with another query for you to try
    – Ace
    Oct 17 at 13:19
  • Every resource that's gets created in Azure, whether it the portal, powershell or template invokes the Azure Resource Manager, and that logs all the same way. But when you don't stream those events to log analytics or another way, the events are not long term kept,and will be overwritten quite quickly. So i forgot to tell that a prerequisite is that you for example create an azure policy to send those logs for the subscription you desire to log analytics. @Jurjen
    – Ace
    Oct 18 at 3:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .