def create_infrastructure()

in sagemaker_run_notebook/create_infrastructure.py [0:0]


def create_infrastructure(session=None, update=False, wait=True):
    with open(cfn_template_file, mode="r") as f:
        cfn_template = f.read()
    session = ensure_session(session)
    client = session.client("cloudformation")

    try:
        if not update:
            response = client.create_stack(
                StackName="sagemaker-run-notebook",
                TemplateBody=cfn_template,
                Capabilities=["CAPABILITY_NAMED_IAM"],
            )
        else:
            response = client.update_stack(
                StackName="sagemaker-run-notebook",
                TemplateBody=cfn_template,
                Capabilities=["CAPABILITY_NAMED_IAM"],
            )
    except botocore.exceptions.ClientError as ce:
        if ce.response["Error"]["Code"] == "AlreadyExistsException":
            print(
                "The infrastructure has already been created. Use update to update it."
            )
            return
        elif ce.response["Error"][
            "Code"
        ] == "ValidationError" and "No updates are to be performed" in str(ce):
            print("The infrastructure is already up-to-date. No work to do.")
            return
        raise

    stack_id = response["StackId"]
    print(f"Creating cloudformation stack {stack_id}")
    if not wait:
        return
    status, reason = wait_for_infrastructure(stack_id, session=session)
    if status not in ["CREATE_COMPLETE", "UPDATE_COMPLETE"]:
        print(f"Unexpected result state {status}. Reason is {reason}")
    else:
        print("Stack successfully created")