Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boto: What is the best way to check if a CloudFormation stack is exists?

What is the best way to check if a CloudFormation stack exists and is not in a broken state using Boto? By broken I mean failed and rollback states.

I don't want to use a try/except solution, because boto logs it as an error, and in my scenario it's going to send the exception log to an alarm system.


At the moment I have the following solutions:

1) Use boto.cloudformation.connection.CloudFormationConnection.describe_stacks()

valid_states = '''\
CREATE_IN_PROGRESS
CREATE_COMPLETE
UPDATE_IN_PROGRESS
UPDATE_COMPLETE_CLEANUP_IN_PROGRESS
UPDATE_COMPLETE'''.splitlines()

def describe_stacks():
    result = []
    resp = cf_conn.describe_stacks()
    result.extend(resp)
    while resp.next_token:
        resp = cf_conn.describe_stacks(next_token=resp.next_token)
        result.extend(resp)
    return result


stacks = [stack for stack in describe_stacks() if stack.stack_name == STACK_NAME and stack.stack_status in valid_states]
exists = len(stacks) >= 1

This is slow because I have lots of stacks.


2) Use boto.cloudformation.connection.CloudFormationConnection.list_stacks()

def list_stacks(filters):
    result = []
    resp = cf_conn.list_stacks(filters)
    result.extend(resp)
    while resp.next_token:
        resp = cf_conn.list_stacks(filters, next_token=resp.next_token)
        result.extend(resp)
    return result

stacks = [stack for stack in list_stacks(valid_states) if stack.stack_name == STACK_NAME]
exists = len(stacks) >= 1

This takes forever, because summaries are kept for 90 days and I have lots of stacks.


Question: What is the ideal solution to check if a given stack exists and is not in a failure or rollback state?

like image 515
Hugo Tavares Avatar asked Apr 11 '14 17:04

Hugo Tavares


People also ask

How do I check my CloudFormation stack?

To view information about your CloudFormation stack On the Stacks page of the CloudFormation console, select the stack name. CloudFormation displays the stack details for the selected stack. Select a stack details pane to view the related information about your stack.

Which command line commands list all current stacks in your CloudFormation service?

The aws cloudformation list-stacks command enables you to get a list of any of the stacks you have created (even those which have been deleted up to 90 days). You can use an option to filter results by stack status, such as CREATE_COMPLETE and DELETE_COMPLETE .

Where do you see CloudFormation?

Clouds occur most frequently in this portion of the troposphere, though fog and clouds that… The formation of cloud droplets and cloud ice crystals is associated with suspended aerosols, which are produced by natural processes as well as human activities and are ubiquitous in Earth's atmosphere.


1 Answers

I implemented the following which works:

import boto3
from botocore.exceptions import ClientError

client = boto3.client('cloudformation')

def stack_exists(name, required_status = 'CREATE_COMPLETE'):
    try:
        data = client.describe_stacks(StackName = name)
    except ClientError:
        return False
    return data['Stacks'][0]['StackStatus'] == required_status

I didn't find any of the previous solutions complete, nor is there any quick way of doing it with boto3 so I created the above.

like image 83
Justin Barton Avatar answered Oct 03 '22 01:10

Justin Barton