title: Boto: What is the best way to check if a CloudFormation stack is exists?
body: <p>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.</p>
  
  <p>I don't want to use a <code>try/except</code> solution, because boto logs it as an error, and in my scenario it's going to send the exception log to an alarm system.</p>
  
  <hr>
  
  <p>At the moment I have the following solutions:</p>
  
  <p>1) Use <a href="http://boto.readthedocs.org/en/latest/ref/cloudformation.html#boto.cloudformation.connection.CloudFormationConnection.describe_stacks" rel="noreferrer"><code>boto.cloudformation.connection.CloudFormationConnection.describe_stacks()</code></a></p>
  
  <pre><code>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) &gt;= 1
  </code></pre>
  
  <p>This is slow because I have lots of stacks.</p>
  
  <hr>
  
  <p>2) Use <a href="http://boto.readthedocs.org/en/latest/ref/cloudformation.html#boto.cloudformation.connection.CloudFormationConnection.list_stacks" rel="noreferrer"><code>boto.cloudformation.connection.CloudFormationConnection.list_stacks()</code></a></p>
  
  <pre><code>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) &gt;= 1
  </code></pre>
  
  <p>This takes forever, because summaries are kept for 90 days and I have lots of stacks.</p>
  
  <hr>
  
  <p>Question: What is the <strong>ideal</strong> solution to check if a given stack exists and is not in a failure or rollback state?</p>