Last active
May 18, 2020 07:47
-
-
Save cm-fujii/b9cec68696c261bac1b5e15330a0a0a9 to your computer and use it in GitHub Desktop.
AWS CloudFormation check Stack and Resource number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
cfn = boto3.client('cloudformation') | |
def main(): | |
# スタック一覧を取得する | |
stacks = get_stacks() | |
# 各スタックのリソース数を調べる | |
result = [] | |
for stack in stacks: | |
stack_name = stack['StackName'] | |
resources = get_stack_resources(stack_name) | |
result.append({ | |
'StackName': stack_name, | |
'ResourceCount': len(resources) | |
}) | |
# 結果を表示する | |
display(stacks, result) | |
def get_stacks(token=None): | |
option = { | |
'StackStatusFilter': ['CREATE_COMPLETE', 'UPDATE_COMPLETE'] | |
} | |
if token is not None: | |
option['NextToken'] = token | |
res = cfn.list_stacks(**option) | |
stacks = res.get('StackSummaries', []) | |
if 'NextToken' in res: | |
stacks += get_stacks(res['NextToken']) | |
return stacks | |
def get_stack_resources(stack_name, token=None): | |
option = { | |
'StackName': stack_name | |
} | |
if token is not None: | |
option['NextToken'] = token | |
res = cfn.list_stack_resources(**option) | |
resources = res.get('StackResourceSummaries', []) | |
if 'NextToken' in res: | |
resources += get_stack_resources(res['NextToken']) | |
return resources | |
def display(stacks, result): | |
# リソース数が多い順に表示する | |
for item in sorted(result, key=lambda x:x['ResourceCount'], reverse=True): | |
stack_name = item['StackName'] | |
resource_count = item['ResourceCount'] | |
print(f'{stack_name}({resource_count})') | |
print('----------------------------') | |
print(f'total stack: {len(stacks)}') | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment