-1

I am trying to get the StackId through list_stacks() of the last deleted stack with the name BastionInfraStack (the most recent one). The list_stacks() command returns the following:

{
    "StackSummaries": [
        {
            "StackId": "arn:aws:cloudformation:us-east-2:111111111:stack/Dev-BastionInfraStack/34deb540-9c2f-11ed-90a7-022b01b65a64",
            "StackName": "Dev-BastionInfraStack",
            "CreationTime": "2023-01-24T21:36:48.468000+00:00",
            "LastUpdatedTime": "2023-01-24T21:37:26.782000+00:00",
            "DeletionTime": "2023-01-24T22:19:03.403000+00:00",
            "StackStatus": "DELETE_COMPLETE",
            "DriftInformation": {
                "StackDriftStatus": "NOT_CHECKED"
            }
        },
        {
            "StackId": "arn:aws:cloudformation:us-east-2:111111111:stack/TestInfraStack/cf6d58d0-9b8c-11ed-a58b-02446413ef88",
            "StackName": "Dev-BastionInfraStack",
            "CreationTime": "2023-01-24T16:53:12.552000+00:00",
            "LastUpdatedTime": "2023-01-24T20:45:36.941000+00:00",
            "DeletionTime": "2023-01-24T21:15:49.673000+00:00",
            "StackStatus": "DELETE_COMPLETE",
            "DriftInformation": {
                "StackDriftStatus": "NOT_CHECKED"
            }
        },
        {
            "StackId": "arn:aws:cloudformation:us-east-2:111111111:stack/Dev-BastionInfraStack/54bcf7c0-9b51-11ed-bae1-02f106ea059a",
            "StackName": "Dev-BastionInfraStack",
            "CreationTime": "2023-01-23T19:08:33.666000+00:00",
            "LastUpdatedTime": "2023-01-23T19:09:06.759000+00:00",
            "DeletionTime": "2023-01-23T20:59:59.677000+00:00",
            "StackStatus": "DELETE_COMPLETE",
            "DriftInformation": {
                "StackDriftStatus": "NOT_CHECKED"
            }
        }
    ]
}

For now, the way I am getting the list of stacks is like this:

      deleted_stacks = cfn.list_stacks(
        StackStatusFilter=['DELETE_COMPLETE']
      )

How can I go through that list of objects, check that the stack I need has name BastionInfraStack (maybe with regex or wildcards) and take the StackId and assign it to a variable for later use in another function?

For example in this scenario I would like to get the StackId arn:aws:cloudformation:us-east-2:111111111:stack/Dev-BastionInfraStack/34deb540-9c2f-11ed-90a7-022b01b65a64

  • 1
    None of the stacks shown has the name `BastionInfraStack`. – jarmod Jan 24 '23 at 23:22
  • 1
    have you tried sorting on the "DeletionTime" which looks to be a `datetime` object according to the documentation? Then maybe grab the first in the list and reference the "StackId" key from the list? If you are talking "BastionInfraStack" as a pattern then read up on regex for pattern matching. – ptierno Jan 24 '23 at 23:24
  • @jarmod Yes, you can check the **"StackName": "Dev-BastionInfraStack"** in 2 of them. I thinking filtering using wildcards or something like that in python *BastionInfraStack* – msaavedra91 Jan 24 '23 at 23:25
  • @ptierno I thought about it but I don't know how to do it in python. I don't know how to go through the list of objects, verify that the stack has the name BastionInfraStack, sort and take the first stackId value. – msaavedra91 Jan 24 '23 at 23:27
  • @msaavedra91 and such is the life of someone who writes code. dont expect to get a fully functional program out of this site. take the advice in the comments and do your due diligence. – ptierno Jan 24 '23 at 23:28
  • @ptierno Well, I don't expect a functional program but ideas in code (examples on how to do it), as well as the millions of answers out there in StackOverflow. It is easier sometimes to see a code example than something written in words. Thank you very much for your idea anyway! – msaavedra91 Jan 24 '23 at 23:31
  • 1
    Combine [filter list of dict](https://stackoverflow.com/questions/29051573/python-filter-list-of-dictionaries-based-on-key-value) to get the sub-list that [includes the desired string](https://realpython.com/python-string-contains-substring/) and [sort list of dict](https://stackoverflow.com/questions/72899/how-do-i-sort-a-list-of-dictionaries-by-a-value-of-the-dictionary) to sort by date descending. The result you want will then be the first dict in the list. – jarmod Jan 24 '23 at 23:45

1 Answers1

2

You have to loop through the list and pick the elements matching your criteria.

Python way of writing this is

 mystacks=[d['StackId'] for d in deleted_stacks['StackSummaries'] if d['StackName']=="Dev-BastionInfraStack"]

Change the condition to look for a substring if you only want to look for "BastionInfraStack"

SidJ
  • 669
  • 12
  • 29
  • 1
    This doesnt come close to answering the questions the poster is asking. – ptierno Jan 24 '23 at 23:29
  • 1
    In fact, this helped me a lot. I was able to filter by the substring and take the first value which is always the one I need since list_stack always stores the most recent deleted stack in first position. Thanks! – msaavedra91 Jan 25 '23 at 00:27