DEV Community

Arun Kumar for AWS Community Builders

Posted on

Orphaned CloudFormation Stacks — HouseKeeping

Scenario

  • There could be some stacks missed out during teardown process due to some issues and this might leave those stacks orphaned.
  • Also when App teams create a new stack without deleting their previous stack, this will leave the previous stack orphaned.

Solution

  • List out all the stacks based on the state in the corresponding account using below python script. Filter the suspecting orphaned stacks from the list.
# Function: EvaluateOrphanedStacks
# Purpose: List out stacks based on the state and accounts
import boto3
import json
from datetime import datetime
from datetime import date
cfn_client=boto3.client('cloudformation')
def list_stacks():
paginator = cfn_client.get_paginator('list_stacks')
response_iterator = paginator.paginate(
StackStatusFilter=[
'CREATE_IN_PROGRESS',
'CREATE_FAILED',
'CREATE_COMPLETE',
'ROLLBACK_IN_PROGRESS',
'ROLLBACK_FAILED',
'ROLLBACK_COMPLETE',
'DELETE_IN_PROGRESS',
'DELETE_FAILED',
'UPDATE_IN_PROGRESS',
'UPDATE_COMPLETE_CLEANUP_IN_PROGRESS',
'UPDATE_COMPLETE',
'UPDATE_ROLLBACK_IN_PROGRESS',
'UPDATE_ROLLBACK_FAILED',
'UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS',
'UPDATE_ROLLBACK_COMPLETE',
'REVIEW_IN_PROGRESS',
'IMPORT_IN_PROGRESS',
'IMPORT_COMPLETE',
'IMPORT_ROLLBACK_IN_PROGRESS',
'IMPORT_ROLLBACK_FAILED',
'IMPORT_ROLLBACK_COMPLETE'
]
)
for page in response_iterator:
for stack in page['StackSummaries']:
print(stack['StackName'])
if __name__ == '__main__':
list_stacks()
Enter fullscreen mode Exit fullscreen mode

Note:
Its ALWAYS recommended and good practice to reduce the orphaned stacks and unwanted resources !

Top comments (0)