Skip to content

AWS CloudFormation

Install rboto[cloudformation] and import CloudFormationClient from rboto or rboto_cloudformation. List matching stacks across all result pages:

#!/usr/bin/env python3
import asyncio

from rboto import CloudFormationClient


async def main() -> None:
    async with CloudFormationClient(region="eu-west-1") as client:
        async for page in client.paginators.list_stacks(
            stack_status_filter=["CREATE_COMPLETE", "UPDATE_COMPLETE"],
        ):
            for stack in page.stack_summaries or []:
                print(stack.stack_name, stack.stack_status)


if __name__ == "__main__":
    asyncio.run(main())

Stack operations can return before CloudFormation finishes changing resources. Use a waiter with a polling timeout when later operations depend on the result. Pass the stack ID returned by the create call:

from rboto import CloudFormationClient, WaiterConfig


async def deploy(template_body: str) -> None:
    async with CloudFormationClient(region="eu-west-1") as client:
        created = await client.create_stack(
            stack_name="example", template_body=template_body,
        )
        if created.stack_id is None:
            raise RuntimeError("CloudFormation did not return a stack ID")
        result = await client.waiters.stack_create_complete(
            stack_name=created.stack_id,
            waiter_config=WaiterConfig(timeout=600),
        )
        for stack in result.stacks or []:
            for output in stack.outputs or []:
                print(output.output_key, output.output_value)

Review change sets before executing them. Handle operation errors separately from waiter failures: for example, ChangeSetNotFoundError is available in rboto_cloudformation.exceptions, while a stack that rolls back can cause the waiter to raise WaiterFailedError. The CloudFormation reference lists operation parameters, response types, paginators, and waiters.