How to connect to AWS RDS external database (not for the core WordPress db)

I was able to get a working connection using the following code:

function test_connect_to_db() {

    $servername="thedatabase.0000000000.location-1.rds.amazonaws.com";
    $username="username";
    $password = 'password';
    $dbname="database";

    // Create connection
    $conn = new mysqli( $servername, $username, $password, $dbname );

    // Check connection
    if ( $conn->connect_error ) {
        die( 'Connection failed: ' . $conn->connect_error );
    } 

    $sql="SELECT id, firstname, lastname FROM table";
    $result = $conn->query( $sql );

    if ( $result->num_rows > 0 ) {
    // output data of each row
        while ( $row = $result->fetch_assoc() ) {
            echo '<br> id: '. $row['id']. ' - Name: '. $row['firstname']. ' ' . $row['lastname'] . '<br>';
        }
    } else {
        echo '0 results';
    }

    $conn->close();

}
add_action( 'init', 'test_connect_to_db' );