|
| 1 | +import os |
| 2 | +from boto import cloudformation |
| 3 | +import requests |
| 4 | +import json |
| 5 | + |
| 6 | +AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID') |
| 7 | +AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY') |
| 8 | + |
| 9 | +REGIONS = ['us-west-1', 'us-west-2', 'us-east-1', |
| 10 | + 'eu-west-1', 'eu-central-1', 'ap-southeast-1', |
| 11 | + 'ap-northeast-1', 'ap-southeast-2', 'ap-northeast-2', |
| 12 | + 'sa-east-1', 'ap-south-1', 'us-east-2'] |
| 13 | + |
| 14 | + |
| 15 | +def find_stacks(): |
| 16 | + running_stacks = {} |
| 17 | + |
| 18 | + # get the list of running stacks on all regions. |
| 19 | + for region in REGIONS: |
| 20 | + print(u"region={}".format(region)) |
| 21 | + stack_names = [] |
| 22 | + connection = cloudformation.connect_to_region(region) |
| 23 | + stacks = connection.describe_stacks() |
| 24 | + for stack in stacks: |
| 25 | + print(stack.stack_name) |
| 26 | + stack_names.append(stack.stack_name) |
| 27 | + |
| 28 | + if len(stack_names) > 0: |
| 29 | + running_stacks[region] = stack_names |
| 30 | + |
| 31 | + return running_stacks |
| 32 | + |
| 33 | + |
| 34 | +SLACK_INCOMING_WEB_HOOK = "https://hooks.slack.com/services/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" |
| 35 | + |
| 36 | +payload = { |
| 37 | + "text": "Nightly Stack Audit: Currently running stacks", |
| 38 | + "channel": "#editions-dev", |
| 39 | + "attachments": [] |
| 40 | +} |
| 41 | + |
| 42 | + |
| 43 | +def results(title, running_stacks): |
| 44 | + fields = [] |
| 45 | + |
| 46 | + for region, stacks in running_stacks.iteritems(): |
| 47 | + stack_str = ", ".join(stacks) |
| 48 | + fields.append({"title": region, "value": stack_str, "short": False}) |
| 49 | + |
| 50 | + attachment = { |
| 51 | + "fallback": title, |
| 52 | + "text": "Here is the list of stacks that are running. If they are yours and you don't need them anymore, please delete.", |
| 53 | + "title": title, |
| 54 | + "color": "green", |
| 55 | + "fields": fields |
| 56 | + } |
| 57 | + |
| 58 | + return attachment |
| 59 | + |
| 60 | +# find the stacks that are running. |
| 61 | +running_stacks = find_stacks() |
| 62 | +print(running_stacks) |
| 63 | + |
| 64 | +# send notification to slack. |
| 65 | +the_results = results("Currently Running Stacks on AWS", running_stacks) |
| 66 | +payload['attachments'] = [the_results] |
| 67 | + |
| 68 | +# send message |
| 69 | +response = requests.post(SLACK_INCOMING_WEB_HOOK, |
| 70 | + json.dumps(payload), headers={'content-type': 'application/json'}) |
| 71 | + |
| 72 | +print(response) |
| 73 | +print(response.text) |
| 74 | +print("done") |
0 commit comments