def does_stack_exist()

in aws_codeseeder/services/cfn.py [0:0]


def does_stack_exist(stack_name: str) -> Tuple[bool, Dict[str, str]]:
    """Checks for existence of a CloudFormation Stack while also returning Stack Outputs if it does exist

    Parameters
    ----------
    stack_name : str
        Name of the CloudFormation Stack to query

    Returns
    -------
    Tuple[bool, Dict[str, str]]
        Tuple2 with a boolean indicating Stack existence and a dict of any Stack Outputs
    """
    client = boto3_client("cloudformation")
    try:
        resp = client.describe_stacks(StackName=stack_name)
        if len(resp["Stacks"]) < 1:
            return (False, {})
        else:
            return (True, {o["OutputKey"]: o["OutputValue"] for o in resp["Stacks"][0].get("Outputs", [])})
    except botocore.exceptions.ClientError as ex:
        error: Dict[str, Any] = ex.response["Error"]
        if error["Code"] == "ValidationError" and f"Stack with id {stack_name} does not exist" in error["Message"]:
            return (False, {})
        raise