def deploy_template()

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


def deploy_template(stack_name: str, filename: str, seedkit_tag: str, s3_bucket: Optional[str] = None) -> None:
    """Deploy a local CloudFormation Template

    The function will automatically calculate a ChangeSet if the Stack already exists and update accordingly. If the
    local template file is too large, it will be uploaded to the optional ``s3_buckeet`` and deployed from there.

    Parameters
    ----------
    stack_name : str
        Name of the CloudFormation Stack to deploy
    filename : str
        Name of the local CloudFormation template file
    seedkit_tag : str
        Name of the Seedkit to Tag resources in the Stack with
    s3_bucket : Optional[str], optional
        S3 Bucket to upload the template file to if it is too large (> 51200), by default None

    Raises
    ------
    FileNotFoundError
        If the local template file is not found
    ValueError
        If the S3 Bucket is not found
    """
    LOGGER.debug("Deploying template %s", filename)
    if not os.path.isfile(filename):
        raise FileNotFoundError(f"CloudFormation template not found at {filename}")
    template_size = os.path.getsize(filename)
    if template_size > 51_200:
        if s3_bucket is None:
            raise ValueError("s3_bucket: %s", s3_bucket)
        LOGGER.info(f"The CloudFormation template ({filename}) is too big to be deployed, using s3 bucket.")
        local_template_path = filename
        s3_file_name = filename.split("/")[-1]
        key = f"cli/remote/demo/{s3_file_name}"
        s3_template_path = f"https://s3.amazonaws.com/{s3_bucket}/{key}"
        LOGGER.debug("s3_template_path: %s", s3_template_path)
        s3.upload_file(src=local_template_path, bucket=s3_bucket, key=key)
        changeset_id, changeset_type = _create_changeset(
            stack_name=stack_name, template_str="", seedkit_tag=seedkit_tag, template_path=s3_template_path
        )
    else:
        with open(filename, "r") as handle:
            template_str = handle.read()
        changeset_id, changeset_type = _create_changeset(
            stack_name=stack_name, template_str=template_str, seedkit_tag=seedkit_tag
        )
    has_changes = _wait_for_changeset(changeset_id, stack_name)
    if has_changes:
        _execute_changeset(changeset_id=changeset_id, stack_name=stack_name)
        _wait_for_execute(stack_name=stack_name, changeset_type=changeset_type)