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
CodePudding user response:
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 to condition to look for a substring if you only want to look for "BastionInfraStack"