def attach_go_to_state()

in gotostate.py [0:0]


def attach_go_to_state(failed_state_name, state_machine_arn):
    """
    Given a state machine arn and the name of a state in that state machine, create a new state machine
    that starts at a new choice state called the 'GoToState'. The "GoToState" will branch to the named
    state, and send the input of the state machine to that state, when a variable called "resuming" is
    set to True
    Input   failedStateName - string with the name of the failed state
            stateMachineArn - string with the Arn of the state machine
    Output  response from the create_state_machine call, which is the API call that creates a new state machine
    """
    try:
        response = client.describe_state_machine(
            stateMachineArn=state_machine_arn
        )
    except Exception as cause:
        raise Exception('Could not get ASL definition of state machine', cause)
    role_arn = response['roleArn']
    state_machine = json.loads(response['definition'])
    # Create a name for the new state machine
    new_name = response['name'] + '-with-GoToState'
    # Get the StartAt state for the original state machine, because we will point the 'GoToState' to this state
    original_start_at = state_machine['StartAt']
    '''
    Create the GoToState with the variable $.resuming
    If new state machine is executed with $.resuming = True, then the state machine will skip to the failed state
    Otherwise, it will execute the state machine from the original start state
    '''
    go_to_state = {
        'Type': 'Choice',
        'Choices': [{'Variable': '$.resuming', 'BooleanEquals': False, 'Next': original_start_at}],
        'Default': failed_state_name
    }
    # Add GoToState to the set of states in the new state machine
    state_machine['States']['GoToState'] = go_to_state
    # Add StartAt
    state_machine['StartAt'] = 'GoToState'
    # Create new state machine
    try:
        response = client.create_state_machine(
            name=new_name,
            definition=json.dumps(state_machine),
            roleArn=role_arn
        )
    except Exception as cause:
        raise Exception('Failed to create new state machine with GoToState', cause)
    return response