2

I attempted to "expand" creationTime on a resource in a resource group (python api) so I could find it's age and if > max_age_days I would have deleted the resource within the resource group.

But creationTime doesn't seem to be available on resources.

Is there another way to delete resources within a resource group based on their age ?

Creation Date and Created By are probably the most basic requirements of resource management.

dparkar
  • 1,934
  • 2
  • 22
  • 52
  • it is not a straight forward option. For example for VMs you can check the log of "Microsoft.Compute/virtualMachines/write" as "created" and that will be your creation date. But I think it is not persisted beyond 90 days. In that case you can check the managed disk creation time . It is going to be cumbersome process I feel. – Aravind Nov 05 '19 at 09:38

2 Answers2

2

Azure stores a createdTime and changedTime property on all resources that you can query by adding the $expand parameter to the URL.

The expand parameter isn't documented in the Resource Groups API docs, but it is documented in the List Resources API docs (https://learn.microsoft.com/en-us/rest/api/resources/resources/list) and it works the same way.

I don't know offhand how to add this parameter using the Python API, but here is an example of the REST API itself (using the Try It button from the docs at https://learn.microsoft.com/en-us/rest/api/resources/resourcegroups/list#code-try-0).

GET https://management.azure.com/subscriptions/1237f4d2-3dce-4b96-ad95-677f764e7123/resourcegroups?api-version=2019-08-01&%24expand=createdTime

{
  "value": [
    {
      "id": "/subscriptions/1237f4d2-3dce-4b96-ad95-677f764e7123/resourceGroups/test2rg-3-backup",
      "name": "test2rg-3-backup",
      "type": "Microsoft.Resources/resourceGroups",
      "location": "eastus",
      "createdTime": "2018-11-12T18:08:38.667582Z",
      "properties": {
        "provisioningState": "Succeeded"
      }
    },
    {
      "id": "/subscriptions/1237f4d2-3dce-4b96-ad95-677f764e7123/resourceGroups/proddeploy",
      "name": "proddeploy",
      "type": "Microsoft.Resources/resourceGroups",
      "location": "eastus",
      "createdTime": "2019-01-10T21:28:48.8057057Z",
      "tags": {
        "a": "b"
      },
      "properties": {
        "provisioningState": "Succeeded"
      }
    },
...
kwill
  • 10,867
  • 1
  • 28
  • 26
  • I attempted to list resources for a resource group with expanding createdTime, but didn't work. Can you share a sample of the URL that returns a createdTime for resources within a resource group please. – dparkar Jan 30 '20 at 06:00
  • got it ! GET https://management.azure.com/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}/resources?$expand=createdTime&api-version=2019-10-01 – dparkar Jan 30 '20 at 06:36
0

No, azure resource group api doesn't offer createdate or something similar, you need to store that information in tags (when you create the resource) or pull it from azure api (when new resource appears) and store it somewhere externally.

dparkar
  • 1,934
  • 2
  • 22
  • 52
4c74356b41
  • 69,186
  • 6
  • 100
  • 141
  • can you explain a little more about "pull it from azure api (when new resource appears)" – dparkar Nov 05 '19 at 18:41
  • using something like [Event Grid](https://azure.microsoft.com/en-us/services/event-grid/) or process Azure Activities from Event Hub or OMS you could achieve that – 4c74356b41 Nov 06 '19 at 07:26
  • FYI, see the other answer for a way to get the createdTime for Azure resources and resource groups. – kwill Nov 13 '19 at 04:13